Académique Documents
Professionnel Documents
Culture Documents
Eg. Comboboxname.Iteams.Clear();
IN FORM 1
Form2 f = new Form2();
this.AddOwnedForm(f); //FORM 1 BECOMES OWNER OF FORM 2
f.Show();//FORM 2 DISPLAYS
this.Hide();//FORM 1 HIDES
IN FORM 2
this.Owner.Controls["button1"].Enabled = true;//CHANGING THE PROPERTY
//OF BUTTON IN FORM1 FROM
//FORM2
wizardcup2009.com
1-30march registration
12 phases
Project Title & Description
Module Breakup
Data Dictionary
30 July last date for submission
9 aug presentation for 20 teams
15 aug prize distribution for 10 teams
First 3 Cash prize, Cup, Certificates
Next 7 Gift, Cup, Ceritification
Invitation to join Wizard HR Round + Presentation
Mentors+HOD+ Institutes gifts/certificate
What is .NET?
What is Framework?
What is .NET Framework?
What is Mono?
Technical Programmes
• Pursing .NET 2008 with Framework 2.0 and 3.5 using C# and VB from
Impeccable Educational Trainers Private Limited (An ISO 9001:2000 Certified
Company), Ghaziabad
Technical Knowledge
• .NET Technologies : Visual C#, Visual Basic, ASP.NET 2.0 & 3.5, ADO.NET,
Remoting, I18N, AJAX, Localization, GDI+
• Frameworks : .NET 2.0 & 3.5, Mono
• GUI Tools : Visual Studio.NET 2005 & 2008
• RDBMS : MS Access, SQL Express 2005, SQL Server 2005, Oracle
• Reporting Tools : Crystal Report 11
.NET is a technology from Microsoft. It is based on .NET Framework that provides a set
of APIs called assemblies or dynamic link libraries (DLL) for software development.
These assemblies provide a big set of namespaces and their classes, interfaces,
structures etc. for almost activities of development.
.NET Frameworks works with Windows OS only. To run the .NET applications on other
platforms Novell provides a new framework called Mono that allows to .NET
applications written with C# on any platform.
A framework is a set of rules and tools for .NET development. It is provided a free
distributed and must to run any .NET based application. Different .NET framework
versions are
• 1.0
• 1.1
• 2.0
• 3.0
• 3.5
Source file (.cs/.vb/.js etc.) compiler .exe/.dll (MSIL) CLR (class loaders
code verifier just-in-time compiler binary) Execution
Requirements
1. OS : Windows 2K+
2. Web Server : IIS 5.0+
3. .NET Framework 2.0+
4. Visual Studio.NET 2005+
a. Express (Free)
b. Standard Edition
c. Professional Edition
d. Team Suite
Terminologies in .NET
Assembly Smallest unit for deployment in the form of .DLL/.EXE created based on
.NET Framework. It contains a set of Namespaces
Example
MSCoreLib.dll Assembly
System namespace
Console class
static void Write()
static void WriteLine()
static String ReadLine()
CSC <programename>
Example
CSC First.cs First.exe
ILDASM <filename>
Example
ILDASM First.exe
11.01.2009
using System;
Console.WriteLine(“Hello”);
Example
using c=System.Console;
c.WriteLine(“Hello”);
Mode of operations
- Debug Mode
- Release Mode
Data Types in C#
1. Value Type (Structures)
2. Reference Type (Classes)
3. User Defined Data Types (UDT)
- Class, Structure, Enumerator etc.
Values Types
- Integrals
i. byte – 1 byte
ii. short – 2 byte
iii. int – 4 byte
iv. long – 8 byte
v. sbyte -1 byte
vi. ushort
vii. uint
viii. ulong
- Floats
i. float – 4 bytes (6/7 dp)
ii. double – 8 bytes (15/16 dp)
iii. decimal – 16 bytes (28 dp)
- Booleans
i. bool – 1 byte
- Characters
i. char – 2 byte
Reference Types
string
object
string @double;
Literals
- The values used directly without input are called as literals or literal values
- Integrals
i. Default is int
ii. Use l or L for long and u or U for unsigned as suffix
1. int n=6;
2. long k=7L;
- Floats
i. Default is double
ii. Use f or F for floats and m or M for decimals
1. double a=5.6;
2. float b=6.7f;
3. decimal p=5.9M;
- Boolean
i. Only true or false can byte used
ii. Default is false
iii. bool married=true;
- Characters
i. Same is C
- String literals
i. Use double quotes to enclose
ii. Each string is treated as object
iii. Use @ if escape sequence is used
1. string file=@”c:\kavita\abc”;
iv. Use @ to create pre-formatted strings as well
Output Styles
Java Style
Cstyle
int a = 4, b = 6;
System.Console.WriteLine("Sum of " + a + " and " + b + " is " + (a + b));
System.Console.WriteLine("Sum of {0} and {1} is {2}", a, b, a + b);
24.01.2009
Working with arrays
- An array a is a variable to hold multiple values of similar type
- Types of Arrays
i. Rectangular Arrays
ii. Jagged Array
Rectangular Array
- An array that has equal number of columns in each row
- Arrays are treated as object
- Each array provides Length property
- Use GetLength() method to know about specific dimension
- Types of Rectangular Array
i. Single Dimensional Array
1. int []num=new int[5];
ii. Double Dimensional
1. int [,]num=new int[3,4];
iii. Multi-Dimensional
1. int [,,]num=new int[3,4,5];
Jagged Array
- An within array is called as jagged array
num[0][2]=55;
int age=int.Parse(c.ReadLine());
Example
Get name and age of a person and check for validity of voter
25.01.2008
Class?
- A class is a set of specification or blueprint or template about an entity
- Defines the attributes and behavior to be applied on a kind of entity
- Use class keyword to define a class
class <classname>
{
//members
}
Categories of Members
1. field
a. To hold the value
i. Variable
ii. constant
1. const int num=56;
iii. readonly fields
1. Assigned the value like a constant or at the time of object
creation through constructor. Cannot be change thereafter
2. Use readonly keyword
2. Methods
a. General methods
b. Abstract Methods
i. A method having the signature but no body contents
ii. Can be declared inside a class or interface
iii. If declared inside a class use abstract keyword but no keyword is
required for interfaces
iv. If a class contains any abstract method, it must be declared as
abstract
c. Sealed Methods
i. The method that can never be overridden
ii. Use sealed keyword
3. Properties
4. Indexers
5. Delegates
31.01.2009
Using Pointers in C#
Properties
- A special procedures that behaves like a field used to work with some
value
- Property get created using get and set blocks
<returntype> <propertyname>
{
set
{
variable=value;
}
get
{
return variable;
}
}
Indexers
Example 1
class Indexer1
{
public static void Main()
{
string s="Rakesh";
System.Console.WriteLine("Second character is {0}",s[1]);
}
}
Delegates
Example
using c=System.Console;
class DlgTest
{
delegate void MyDlg(double x, double y);
MyDlg md;
int choice;
c.WriteLine("1: Add");
c.WriteLine("2: Product");
c.WriteLine("3: Both");
choice=int.Parse(c.ReadLine());
if(choice==1)
md=new MyDlg(Add);
else if (choice==2)
md=new MyDlg(Product);
else
{
md=new MyDlg(Add)+new MyDlg(Product);
}
md(n1,n2);
}
}
Encapsulation
- It defines that none of the members can be declared outside the class and
should not be accessible out side the class without permission
- All members are private by default
class <classname>
{
//members
}
Abstraction
- Defines the accessibility level of the members and the assembly
components (class, interface, structure etc.)
- C# provides five level of accessibility
i. private
1. Within the class
ii. internal
1. Within the assembly
iii. protected
1. Used for inheritance only in same or different assembly
iv. public
1. Anywhere access
v. protected internal
1. Can be access within the assembly and can be inherited in
other assembly
Example
- Create a new project as Console Application
- Add a new project in same solution from Project Add New
Project…
- To see both the projects open Solution Explorer from View menu
- Create a class in first project and give reference in second project and
check the accessibility levels
Polymorphism
void area(double r)
void area(int len, int width)
Operator Overloading
- When an operator performs more than one operations, is called as operator
overloading
- An operator can be overloaded using operator keyword
- It is made of anonymous method
08
Inheritance
Method Hiding
- When a method in parent class used with same signature in child class
with different body contents is called as method hiding
- Use new keyword to explicitly define the hiding
Example
new public int greatest() //method hiding
{
return base.greatest()>c?base.greatest():c;
}
Method Overriding
- When a method in parent class, rewritten in child class with same
signature and different body contents
- To allow the overriding the method must be abstract or virtual
- While overriding a method use override keyword if the method is virtual
in parent class
- Overridden method can be access though the reference of parent class or
interface. It gives the concept of Dynamic Polymorphism or Dynamic
Method Dispatch (DMD)
Example
class A
{
virtual public double Volume(double r)
{
return 3.14*r*r;
}
}
class B : A
{
override public double Volume(double r)
{
return System.Math.PI*r*r;
}
}
class ERP
{
public void Calc(A x)
{
System.Console.WriteLine(x.Volume(5));
}
}
class Test
{
public static void Main()
{
ERP x=new ERP();
x.Calc(new A());
x.Calc(new B());
}
}
Interface
- A user defined data type similar to class which contains all abstract
methods
- All methods are public and abstract by default
- Use I prefix with interface names for identification
Sample Case
Create classes and interfaces for students (current,alumni) and faculty (permanent and
visiting) to manage their personal and professional information. Also create the
constructors and methods to input data and some processing on data like fee deposit, pay
salary, print salary sheet etc.
14.02.2009
Exception Handling
- A system to trap the runtime error from the place the error has occurred to
the place the method get called
- Each exception is a class inherited from Exception class of System
namespace
- Every exception kind of class returns a message for the kind of error using
i. string Message property
ii. string ToString() method
- C# defines four keywords for exception handling
i. try
ii. catch
iii. throw
iv. finally
- try-catch is block of statements to do some activity and trap the exception
try
{
//statements
}catch(classname ref)
{
//messaging
}
- finally is again a block provided with try in the last to execute some code
irrespective of exception
Example
class LowBalanceException : Exception
{
public override string Message
{
get
{
return "Sorry! Low Balance in your account. Check it";
}
}
public override string ToString()
{
return "LowBalanceException: Sorry! Low Balance Found.
Check it";
}
}
balance -= amount;
}
public int Balance
{
get
{
return balance;
}
}
}
}
}
Assignment
Create a class having a number and a method called Factorial(). Create another class as
exception class named as NoNumberException.
If no data is provided for Factorial then it must throw an exception called as
NoNumberException
String Handling
- .NET provides two classes for string handling
i. System.String (string)
ii. System.Text.StringBuilder
- String class is immutable (fixed size) where any + operation wastes the
one memory for Garbage Collection
- StringBuilder class is mutable where we can concatenate the string at
same place
String Methods
o string ToUpper()
o string ToLower()
o bool StartsWith(string s)
o bool EndsWith(string s)
o Trim()
o Length
o Format() – to format a string with place holders
15.02.2009
Visual C#
- Programming for Windows in GUI Mode
- Create a new project as Windows Application
- Every windows Application must have at least one Window Form
- A window form is a special class inherited from Form class of
System.Windows.Forms namespace
- A form works as container for windows applications
- It can be used to place different component to develop a windows
application
- Every component and container provided some fixed set of
i. Properties
ii. Methods
iii. Events
- Properties deal with value. Can be of two types
i. Design Time Properties
1. Provides in Properties Window (F4)
ii. Runtime Windows
1. Provided using coding in Code Window
2. Use a control and its event to define the code in code
window
a. Controlname.propertyname=value;
- An event is the moment generated using keyboard, mouse or system
activity
i. Click
ii. DoubleClick
iii. MouseOver
iv. MouseOut
Etc.
20.02.2009
Form control
- Works like a container for Windows Applications
- Must have at least one window form
i. Text
ii. StartPosition
iii. MinimumBox
iv. MaximumBox
v. WindowState
vi. Icon
vii. BackgroundImage
viii. BackgroundImageLayout
ix. FormBorderLayout
Label control
- To provide fixed text
i. Text
ii. Image
iii. TextAlign
iv. ImageAlign
v. AutoSize
vi. TabIndex
- Use & to make the hot key
TextBox control
- To create single line or multiline text and password field
i. Text
ii. PasswordChar
iii. MaxLength
iv. Multiline
v. ScrollBars
vi. WordWrap
vii. Doc
viii. Cut()
ix. Copy()
x. Paste()
xi. Focus()
xii. Append()
-
Button class
- to create the push buttons
i. Text
ii. Image
iii. TextAlign
iv. ImageAlign
PictureBox control
- To show an image
i. Image
ii. SizeMode
iii. BorderStyle
- To load an image dynamically use Image class with FromFile() method
Note: Use … (ellipses) to indicate that some dialog will appear on a button or Menu Item
when clicked
DialogResult MessageBox.Show(“message”,”caption”,MessageBoxButtons b,
MessageBoxIcon ic);
OpenFileDialog control
- Allows to select a file
i. FileName
ii. Filter
Example
openFileDialog1.Filter = "All Files|*.*|Image Files|*.jpg;*.gif;*.png";
openFileDialog1.ShowDialog();
this.Text = openFileDialog1.FileName;
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
ColorDialog control
- To get a color
i. Color
ii. FullOpen
Example
colorDialog1.FullOpen = true;
colorDialog1.ShowDialog();
textBox3.ForeColor = colorDialog1.Color;
FontDialog control
- to get a font with name,size and style
i. Font
ii. Color
iii. ShowColor=true/false
22.02.2009
Checkbox control
- To select none or all
i. Text
ii. Image
iii. TextAlign
iv. ImageAlign
v. Checked
Example
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
button1.Enabled = true;
else
button1.Enabled = false;
}
RadioButton control
- To select only one out of all
i. Text
ii. Image
iii. TextAlign
iv. ImageAlign
v. Checked
- Use GroupBox or Panel control to make the groups
ComboBox control
- To select only one item from list of items
- It provides Items collection
- Use it to Add and remove the items
i. SelectedItem
ii. SelectedIndex
iii. DropDownStyle//event handler
Example
private void Form4_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("-Select-");
for (int i = 100; i <= 200; i++)
comboBox1.Items.Add(i);
comboBox1.SelectedIndex = 0;
}
ListBox class
- Allows to select one or more items
- It also provided Items collection
i. SelectedItem
ii. SelectedIndex
iii. SelectionMode
iv. bool GetSelected(int index)
Example
private void Form4_Load(object sender, EventArgs e)
{
DateTimePicker control
- Allows to select date, time or both
i. Format
ii. CustomFormat
iii. ShowUpDown
iv. ShowCheckBox
v. Checked
vi. Value
- Working with Collections
- A collection is a set of classes provided inside
System.Collections namespace that can manage dynamic set of
data
i. List
ii. ArrayList
iii. LinkedList
- Each collection type class provides
i. Add(object x)
ii. Remove(object x)
iii. RemoveAt(int index)
iv. Count
Note: While passing the values to the collections, they get converted
to objects, called as boxing. While reading back the values from object
cast them to values types, called as unboxing.
- Value to object conversion is called as boxing
object x=56;
- Reference to value conversion though casting is calling as
unboxing
int num=(int) x;
Example
ArrayList ar = new ArrayList();
ar.Add(56);//boxing
ar.Add(33);
ar.Add(77);
int sum = 0;
for (int i = 0; i < ar.Count; i++)
sum = sum + (int)ar[i]; //unboxing
Console.WriteLine("Sum is " + sum);
Generics
Creating Menus
- Menus can be of two types
i. Dropdown Menu
ii. Context Menu
- Two controls are used
i. MenuStrip
ii. ContextMenuStrip
- We can define the hot key with &
- Shortcut key, Image and ToolTip
RichTextBox control
- Provides additional properties on text box
i. SelectionFont
ii. SelectionColor
iii. LoadFile
iv. SaveFile
Timer Control
- To execute some activity after every given interval of time
- Provided under Components section
i. Interval – time in milliseconds
ii. Enabled
ToolBar control
- To create the toolbars
- Use ToolStrip control
Progressbar control
- Minimum
- Maximum
- Value
- A technology under .NET class library and provides classes for database
operations
- Namespaces used
i. System.Data – common classes for all databases
ii. System.Data.Odbc – Using DSN for any database
iii. System.Data.OleDb – Using Provider for specific database
iv. System.Data.Oracle – Only for Oracle
v. System.Data.SqlClient – Only for MS Sql Server
- These namespaces are provided in two assemblies
i. System.Data.dll
ii. System.Data.OracleClient.dll
Example
batch02access
Creating a Connection
- Create an object of Connection type class
i. OdbcConnection cn=new OdbcConnection();
- Provide the ConnectionString property with following information
i. DSN=<dsnname>
ii. User Id or uid=<login name>
iii. Password or pwd=<password>
iv. Example
1. cn.ConnectionString=”dsn=batch02access”;
- Open the connection
i. cn.Open();
Create the command type object and provide the connection and commandtext
OdbcCommand cmd=new OdbcCommand();
cmd.Connection=cn;
cmd.CommandText=sql;
Execute the methods of command type class depending on type of SQL statement
- void ExecuteNonQuery()
i. For all command except SELECT
- object ExecuteScaler()
- OdbcDataReader ExecuteReader()
- ConnectionString fields
i. Data Source=<servername>
ii. User Id or uid=<login>
iii. Password or pwd=<password>
Example
<configuration>
<appSettings>
<add key="cs_sql" value="Data
Source=.;uid=b02;pwd=pass;database=batch02"/>
</appSettings>
</configuration>
Example 2 – MS Access
string sql = "INSERT INTO employee VALUES(?,?,?)";
OleDbCommand cmd = new OleDbCommand(sql,cn);
cmd.Parameters.AddWithValue("empid", txtEmpId.Text);
cmd.Parameters.AddWithValue("name", txtName.Text);
cmd.Parameters.AddWithValue("email", txtEmail.Text);
cmd.ExecuteNonQuery();
Saving Images to Database
- To image to database use datatype
i. Sql Server Image
ii. MS Access OLE Object
iii. Oracle BLOB
- First we must an image to be saved
- To save to on disc it must be byte type array
- Use MemoryStream class of System.IO namespace for interface
Example
string sql = "INSERT INTO imagetest VALUES(@name,@photo)";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.Add(p);
cmd.ExecuteNonQuery();
cmd.Parameters.Add(p);
cmd.ExecuteNonQuery();
Working with connection as One way entry, one way exit
- Open the connection on Load and close the connection on FormClosing