Presentation is loading. Please wait.

Presentation is loading. Please wait.

BUILDING WINDOWS APPLICATION

Similar presentations


Presentation on theme: "BUILDING WINDOWS APPLICATION"— Presentation transcript:

1 BUILDING WINDOWS APPLICATION

2 GENERAL WINDOWS CONTROLS FOR THE WINDOWS APPLICATION

3 SLNO CONTROL NAME 1. Button 2. Checkbox 3. RadioButton 4. Label 5. Textbox 6. ComboBox 7. ListBox 8. ListView SLNO CONTROL NAME 9. PictureBox 10. Progressbar 11. Treeview 12. DataGridView 13. ToolTip 14. Timer

4 WINDOWS COMMON CONTROL PROPERTIES

5 SLNO PROPERITE NAME PROPERTY VALUE DESCRIPTION 1. Name “Form1” Indicates the name used in code to identify the object or control 2. Autosize True or False Specifies whether a control will automatically size iteselft to fit its contents 3. BackColor Color Name The background color of the component 4. BackgroundImage Image File path The background image used for the control 5. Enabled Indicates whether the control is enabled 6. ForeColor The foreground color of this component, which is used to display text. 7. Font Font Face, Font Style, Font size, Font Color The font used to display text in the control. 8. Location X, Y axis The coordinates of the upper – left corner of the control relative to the upper – left corner of its container 9. Locked The Locked property determines if we can move or resize the control 10. Size Width and Height The size of the control in pixels 11. Text “Text of the Particular Control” The text associated with the control 12. Visible Determines whether the control is visible or hidden 13. TabIndex Index value to the control Determines the index in the TAB order that this control will occupy. 14. TabStop Indicates whether the user can use the TAB key to give focus to the control. 15. TextAlign Left, Center, Rigth The alignment of the text that will be displayed on the control. 16. ToolTip Text Tool Tip Text on the Control Determines the tool tip shown when the mouse hovers over the control. 17. UseMnemonic If true, the first character proceeded by an ampersand (&) will be used as the button’s mnemonic key.

6 WINDOWS COMMON CONTROLS DEFAULT EVENT HANDLERS

7 HOW TO ADD THE DATA’S TO CONTROL
SLNO CONTROLS HOW TO ADD THE DATA’S TO CONTROL DEFAULT EVENT HANDLER 1. Form Form1.Text = “Chettinad”; private void Form1_Load(object sender, EventArgs e){ } 2. Button Button1.Text=”Click Here”; private void button1_Click(object sender, EventArgs e) { } Label Label.Text=”Enter the A Value”; private void label1_Click(object sender, EventArgs e) { } 3. Textbox Textbox1.Text=”Insert the Text Here”; private void textBox1_TextChanged(object sender, EventArgs e) { } 4. Checkbox Checkbox1.Text=”Addition”; private void checkBox1_CheckedChanged(object sender, EventArgs e) { } 5. Radiobutton Radiobutton1.Text=”Addition”; private void radioButton1_CheckedChanged(object sender, EventArgs e) { } 6. CheckListBox CheckListbox.Items.add(“Add”); CheckLIstbox.Items.add(“Sub”); private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) { } 7. ComboBox Combobox.Items.add(“Add”); Combobox.Items.add(“Sub”); private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } 8. ListBox Listbox1.Items.add(“Add”); Listbox1.Items.add(“Sub”); private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } 9. Picturebox Picturebox1.Load(“Picture File Path”) private void pictureBox1_Click(object sender, EventArgs e) { } 10. Progressbar NIL private void progressBar1_Click(object sender, EventArgs e) { } 11. Treeview Treeview1.Items.add(“Add”); TreeView1.Items.add(“Sub”); private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { } 12. Listview Listview1.Items.add(“Add”); Listview1.Items.add(“Sub”); private void listView1_SelectedIndexChanged(object sender, EventArgs e) { } 13. Groupbox Groupbox1.Controls.add(button1); private void groupBox1_Enter(object sender, EventArgs e) { } 14. Timer Nil private void timer1_Tick(object sender, EventArgs e) { }

8 EXTRA PROPERTIES FOR THE WINDOWS CONTROLS

9 SLNO CONTROL NAME PROPERITE NAME PROPERTY VALUE DESCRIPTION 1. PictureBox Image .jpg or .bmp or .gif The image display in the PictureBox. WaitOnLoad True or False Controls whether processing will stop until the image is loaded. 2. RadioButton Checked Indicates whether the component is in the checked state. 3. Checkbox CheckState Unchecked or Checked or Intermediate Indicates the state of the component. ThreeState Indicates whether the CheckBox will allow three check states rather than two. 4. TextBox HideSelection Indicates that the selection should be hidden when the edit control loses focus. MaxLength 32767 Specifies the maximum number of character that can be entered into the edit control. Multiline Controls whether the text of the edit control can span more than one line. PasswordChar ***** or &&&&& Indicates the character to display for password input for single – line edit controls. ScrollBars None, Horizontal, Vertical Indicates, for multiline edit contrls, which scroll bars, will be shown for this control. WordWrap Indicates if lines are automatically word – wrapped for multiline edit controls. ShortcutsEnabled Indicates whether shortcuts defined for the control are enabled. 5. Form ControlBox Determines whether a form has a Control / System menu box. Cursor Different Cursor Control The Cursor that appear when the pointer moves over the control. MaximizeBox Determines whether a form has a maximize box in the upper – right corner of its caption bar. MinimizeBox Determines whether a form has a minimize box in the upper – right corner of its caption bar. StartPosition Windows appears for the different location Determines the position of a form when it first appears. WindowState Normal, Maximize, Minimize 6. Timer Enabled Enables generation of Elapsed events Interval 1000 (milliseconds) The frequency of Elapsed events in milliseconds 7. ProgressBar Minimum The lower bound of the range this ProgressBar is working with. Maximum 1000 The upper bound of the range this ProgressBar is working with.

10 MessageBox Program using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form public Form1() InitializeComponent(); }

11 private void button1_Click(object sender, EventArgs e)
{ MessageBox.Show("Welcome to Chettinad College of Engineering and Technology"); } private void button2_Click(object sender, EventArgs e) MessageBox.Show("Welcome", "Chettinad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop); private void button3_Click(object sender, EventArgs e) DialogResult ans = MessageBox.Show("Arithmetic Operation", "Welcome", MessageBoxButtons.OKCancel); if (ans == DialogResult.OK ) MessageBox.Show("Click OK Button"); else if(ans == DialogResult.Cancel) MessageBox.Show("Click Cancel Button"); private void button4_Click(object sender, EventArgs e) Button_Control f2 = new Button_Control(); f2.Show(); } }

12 Addition of Two Numbers using Button, Label and Textbox
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Button_Control : Form public Button_Control() InitializeComponent(); } private void button1_Click(object sender, EventArgs e) int a, b, c; a = int.Parse(textBox1.Text); b = int.Parse(textBox2.Text); c = a + b; textBox3.Text = c.ToString(); } }

13 Arithmetic Operation using Checkboxes
private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked == true) checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; x = textBox1.Text; y = textBox2.Text; a = int.Parse(x); b = int.Parse(y); c = a + b; textBox3.Text = c.ToString(); } } namespace WindowsApplication1 { public partial class CheckBox_Control : Form public CheckBox_Control() InitializeComponent(); } int a, b, c; string x, y;

14 private void checkBox3_CheckedChanged(object sender, EventArgs e)
{ if (checkBox3.Checked == true) checkBox1.Checked = false; checkBox2.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; x = textBox1.Text; y = textBox2.Text; a = int.Parse(x); b = int.Parse(y); c = a * b; textBox3.Text = c.ToString(); } private void checkBox2_CheckedChanged(object sender, EventArgs e) { if (checkBox2.Checked == true) checkBox1.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; x = textBox1.Text; y = textBox2.Text; a = int.Parse(x); b = int.Parse(y); c = a - b; textBox3.Text = c.ToString(); }

15 private void checkBox4_CheckedChanged(object sender, EventArgs e)
{ if (checkBox4.Checked == true) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox5.Checked = false; x = textBox1.Text; y = textBox2.Text; a = int.Parse(x); b = int.Parse(y); c = a / b; textBox3.Text = c.ToString(); } private void checkBox5_CheckedChanged(object sender, EventArgs e) { if (checkBox5.Checked == true) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; }

16 Arithmetic Operation using Radio Buttons
private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked == true) radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; radioButton5.Checked = false; try a = int.Parse(textBox1.Text); b = int.Parse(textBox2.Text); } catch c = a + b; textBox3.Text = c.ToString(); using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Radio_Control : Form int a, b, c; public Radio_Control() InitializeComponent(); }

17 private void radioButton2_CheckedChanged(object sender, EventArgs e)
{ if (radioButton2.Checked == true) radioButton1.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; radioButton5.Checked = false; a = int.Parse(textBox1.Text); b = int.Parse(textBox2.Text); c = a - b; textBox3.Text = c.ToString(); } private void radioButton3_CheckedChanged(object sender, EventArgs e) { if (radioButton3.Checked == true) radioButton1.Checked = false; radioButton2.Checked = false; radioButton4.Checked = false; radioButton5.Checked = false; a = int.Parse(textBox1.Text); b = int.Parse(textBox2.Text); c = a * b; textBox3.Text = c.ToString(); }

18 private void radioButton4_CheckedChanged(object sender, EventArgs e)
{ if (radioButton4.Checked == true) radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton5.Checked = false; a = int.Parse(textBox1.Text); b = int.Parse(textBox2.Text); c = a / b; textBox3.Text = c.ToString(); } private void radioButton5_CheckedChanged(object sender, EventArgs e) { if (radioButton5.Checked == true) radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } private void Form6_Load(object sender, EventArgs e) radioButton5.Checked = false;

19 Arithmetic Operation using ComboBox and ListBox
private void Form5_Load(object sender, EventArgs e) { comboBox1.Items.Add("Addition"); comboBox1.Items.Add("Subtraction"); comboBox1.Items.Add("Multiplication"); comboBox1.Items.Add("Division"); comboBox1.Items.Add("Clear"); listBox1.Items.Add("Addition"); listBox1.Items.Add("Subtraction"); listBox1.Items.Add("Multiplication"); listBox1.Items.Add("Division"); listBox1.Items.Add("Clear"); } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form5 : Form int a, b, c; public Form5() InitializeComponent(); } 19

20 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { a = int.Parse(textBox1.Text); b = int.Parse(textBox2.Text); if (comboBox1.SelectedIndex == 0) c = a + b; textBox3.Text = c.ToString(); } if (comboBox1.SelectedIndex == 1) c = a - b; if (comboBox1.SelectedIndex == 2) c = a * b; if (comboBox1.SelectedIndex == 3) c = a / b; if (comboBox1.SelectedIndex == 4) textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { a = int.Parse(textBox1.Text); b = int.Parse(textBox2.Text); if (listBox1.SelectedIndex == 0) c = a + b; textBox3.Text = c.ToString(); } if (listBox1.SelectedIndex == 1) c = a - b; if (listBox1.SelectedIndex == 2) c = a * b; if (listBox1.SelectedIndex == 3) c = a / b; if (listBox1.SelectedIndex == 4) textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } }

21 Arithmetic Operation using CheckListBox
private void Checkboxlist_Control_Load(object sender, EventArgs e) { checkedListBox1.Items.Add("Addition"); checkedListBox1.Items.Add("Subtraction"); checkedListBox1.Items.Add("Multiplication"); checkedListBox1.Items.Add("Division"); checkedListBox1.Items.Add("Clear"); } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Checkboxlist_Control : Form public Checkboxlist_Control() InitializeComponent(); } int a, b, c;

22 private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{ a = int.Parse (textBox1 .Text ); b = int.Parse (textBox2 .Text ); if (checkedListBox1.SelectedIndex == 0) c = a + b; textBox3.Text = c.ToString(); } if (checkedListBox1.SelectedIndex == 1) c = a - b; if (checkedListBox1.SelectedIndex == 2) c = a * b; if (checkedListBox1.SelectedIndex == 3) c = a / b; if (checkedListBox1.SelectedIndex == 4) textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = "";

23 Program for the PictureBox
namespace WindowsApplication1 { public partial class Picturebox_Control : Form public Picturebox_Control() InitializeComponent(); and Settings\All Users\Documents\My Pictures\Sample Pictures\sunset.jpg"); } private void button1_Click(object sender, EventArgs e) pictureBox1 .Image = Image .FromFile and Settings\All Users\Documents\My Pictures\Sample Pictures\sunset.jpg"); pictureBox1.ImageLocation = " using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;

24 Program for the ProgressBar
private void Progress_Control_Load(object sender, EventArgs e) { progressBar1.Minimum = 0; progressBar1.Maximum = 1000; } private void button1_Click(object sender, EventArgs e) progressBar1.Increment(10); using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Progress_Control : Form public Progress_Control() InitializeComponent(); }

25 Program for the ProgressBar and Timer Control
private void Timer_Control_Load(object sender, EventArgs e) { progressBar1.Minimum = 0; progressBar1.Maximum = 1000; } private void timer1_Tick(object sender, EventArgs e) progressBar1.Increment(100); using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Timer_Control : Form public Timer_Control() InitializeComponent(); } NOTE In the Timer Control Properties change the Enabled properties is True and the set the Time Interval ( not Zero)

26 Program for the ListView
private void button1_Click(object sender, EventArgs e) { listView1.View = View.LargeIcon; } private void button2_Click(object sender, EventArgs e) listView1.View = View.SmallIcon; private void button3_Click(object sender, EventArgs e) listView1.View = View.List; private void button4_Click(object sender, EventArgs e) listView1.View = View.Details; private void button5_Click(object sender, EventArgs e) listView1.Clear(); } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Listview_Control : Form public Listview_Control() InitializeComponent(); }

27 Addition of Two Number using the Code File
(Creating your Own User Interface) using System; using System.Data; using System.Drawing; using System.Windows.Forms; public class Own : Form { Button b1; Label l1, l2, l3; TextBox t1, t2, t3; int a, b, c; public Own() l1 = new System.Windows.Forms.Label(); l1.Location = new System.Drawing.Point(0,0); l1.Size = new System.Drawing.Size(130,25); l1.Text = "Enter the A Value"; Controls.Add(l1); t1 = new System.Windows.Forms.TextBox (); t1.Location = new System.Drawing.Point(150,0); t1.Size = new System.Drawing.Size(50,25); t1.Text = ""; Controls.Add(t1); l2 = new System.Windows.Forms.Label(); l2.Location = new System.Drawing.Point(0,50); l2.Size = new System.Drawing.Size(130,25); l2.Text = "Enter the B Value"; Controls.Add(l2); t2 = new System.Windows.Forms.TextBox(); t2.Location = new System.Drawing.Point(150,50); t2.Size = new System.Drawing.Size(50,25); t2.Text = ""; Controls.Add(t2);

28 l3 = new System.Windows.Forms.Label();
l3.Location = new System.Drawing.Point(0,100); l3.Size = new System.Drawing.Size(150,25); l3.Text = "Addition of the Two Numbers"; Controls.Add(l3); t3 = new System.Windows.Forms.TextBox (); t3.Location = new System.Drawing.Point(160,50); t3.Size = new System.Drawing.Size(50,25); t3.Text = "Enter the A Value"; Controls.Add(t3); b1 = new System.Windows.Forms.Button(); b1.Location = new System.Drawing.Point(200,200); b1.Size = new System.Drawing.Size(100, 25); b1.Text = "Click Addition"; b1.Click += new System.EventHandler(b1_Click); Controls.Add(b1); } public void b1_Click(object sender, EventArgs ea) { a = int.Parse(t1.Text); b = int.Parse(t2.Text); c = a + b; t3.Text = c.ToString(); } } class Main_Own { static void Main() Application.Run(new Own()); }

29 Output of the Above program is

30 Creating Calculator Program using the Code File
(Creating your Own User Interface) //1. Include the Namespaces// using System; using System.Drawing; using System.Windows.Forms; //2. Create a class that inherit from Form class of Windows// public class ButtonDemo: Form { //3.Create the Controls // Button b0; Button b1; Button b2; Button b3; Button b4; Button b5; Button b6; Button b7; String s1=""; String s2=""; String op=""; TextBox t1; //4.Create the class Constructor methosd to invoke the components to be initialized// public ButtonDemo() //5. Invoke the method to initialize the components// InitializeComponent(); }

31 private void InitializeComponent()
{ // Initilaize the components properties and Events// t1=new TextBox(); t1.Name="textbox1"; t1.Location=new Point(50,80); t1.Size=new System.Drawing.Size(80,50); this.Controls.Add(t1); b0=new Button(); b0.Location=new Point(50,120); b0.Size=new System.Drawing.Size(25,25); b0.Text="0"; b0.Click+=new System.EventHandler(this.btnsubmit_Click); this.Controls.Add(b0); b1=new Button(); b1.Location=new Point(75,120); b1.Size=new System.Drawing.Size(25,25); b1.Text="1"; b1.Click += new System.EventHandler(this.btnsubmit_Click); this.Controls.Add(b1); b2 = new Button(); b2.Location = new Point(100, 120); b2.Size = new System.Drawing.Size(25, 25); b2.Text = "2"; b2.Click += new System.EventHandler(this.btnsubmit_Click); this.Controls.Add(b2);

32 b3 = new Button(); b3.Location = new Point(125, 120); b3.Size = new System.Drawing.Size(25, 25); b3.Text = "3"; b3.Click += new System.EventHandler(this.btnsubmit_Click); this.Controls.Add(b3); b4 = new Button(); b4.Location = new Point(50, 145); b4.Size = new System.Drawing.Size(25, 25); b4.Text = "4"; b4.Click += new System.EventHandler(this.btnsubmit_Click); this.Controls.Add(b4); b5 = new Button(); b5.Location = new Point(75, 145); b5.Size = new System.Drawing.Size(25, 25); b5.Text = "+"; b5.Click += new System.EventHandler(this.btnsubmit_Click); this.Controls.Add(b5); b6 = new Button(); b6.Location = new Point(100, 145); b6.Size = new System.Drawing.Size(25, 25); b6.Text = "-"; b6.Click += new System.EventHandler(this.btnsubmit_Click); this.Controls.Add(b6); b7 = new Button(); b7.Location = new Point(125, 145); b7.Size = new System.Drawing.Size(25, 25); b7.Text = "="; b7.Click += new System.EventHandler(this.btnsubmit_Click); this.Controls.Add(b7); this.Name="Button Demo"; this.Text="Button Demo"; }

33 else if(sender.Equals(b7)) { if(op.Equals("+"))
//Events handling Procedures// private void btnsubmit_Click(Object sender, EventArgs e) { if(sender.Equals(b0)) t1.Text += "0"; } else if (sender.Equals(b1)) t1.Text += "1"; else if (sender.Equals(b2)) t1.Text+="2"; else if (sender.Equals(b3)) t1.Text += "3"; else if (sender.Equals(b4)) t1.Text += "4"; else if (sender.Equals(b5)) s1=t1.Text; t1.Text=""; op=b5.Text; } else if(sender.Equals(b7)) { if(op.Equals("+")) MessageBox.Show((Int32.Parse(t1.Text)+Int32.Parse(s1)).ToString()); public class Demo static void Main() System.Windows.Forms.Application.Run(new ButtonDemo());

34 Output of the Above program is

35 Data Access with ADO.NET

36 ADO.NET relational data bases XML data other data sources
Is the .NET technology for accessing structured data Uniform object oriented interface for different data sources relational data bases XML data other data sources Designed for distributed and Web applications Provides 2 models for data access connection-oriented connectionless

37 Idea of Universal Data Access
Connection of (object-oriented) programming languages and relational data bases Uniform programming model and API Special implementations for data sources (providers)

38 Data Providers Microsoft’s layered architecture for data access

39 History of Universal Data Access (Microsoft)
ODBC OLE DB ADO (ActiveX Data Objects) ADO.NET ADO ADO.NET connection-oriented connection-oriented + connectionless sequential access sequential access + main-memory representation with direct access only one table supported more than one table supported COM-marshalling XML-marshalling

40 DIFFERENCES BETWEEN ADO AND ADO .NET
ADO has one main object that is used to reference data, called the Record set object. This object basically gives you a single table view of your data, although you can join tables to create a new set of records.   With ADO.NET, you have various objects that allow you to access data in various ways. The Dataset object will actually allow you to store the relational model of your database. This allows you to pull up customers and their orders, accessing/updating the data in each related table individually. ADO works with connected data. This means that when you access data, such as viewing and updating data, it is real-time, with a connection being used all the time. This is barring, of course, you programming special routines to pull all your data into temporary tables.   ADO.NET uses data in a disconnected fashion. When you access data, ADO.NET makes a copy of the data using XML. ADO.NET only holds the connection open long enough to either pull down the data or to make any requested updates. This makes ADO.NET efficient to use for Web applications. It's also decent for desktop applications. In ado user can move sequentially to database In ado.net user can move one data from one table to another table. ADO allows you to create client-side cursors only ADO.NET we don't have any cursor support in ADO.NET. ADO allows you to persist records in XML format ADO.NET allows you to manipulate your data using XML as the primary means. This is nice when you are working with other business applications and also helps when you are working with firewalls because data is passed as HTML and XML.   COM – marshalling XML – marshalling

41 Architecture of ADO.NET

42 Architecture represents connection to data source
DbConnection represents connection to data source DbCommand represents a SQL command DbTransaction represents a transaction commands can be executed within a transaction DataReader result of a data base query allows sequential reading of rows

43 DataTables and DataColumns
DataAdapter DataAdapter as a bridge between the DataSet and the data source, which is underlying database DataAdapter provides the Fill() method to retrieve data from the database and populate the dataset DataSet A DataSet represents a complete set of data including the tables that contain, order, and constrain the data, as well as the relationships between the tables. DataTables and DataColumns The DataTable has a number of public properties, including the columns collection, which returns the DataColumnCollection object ,which in trun consists of DataColumn objects. Each DataColumn object represents a column in a table DataRelations DataRelation represents a relationship between two tables through DataColumn objects. For example, in the Northwind database the Customers table is in a relationship with the Orders table through the CustomerID column DataRows - DataTable’s Rows collection returns a set of rows for that table. Use this collection to examine the results of queries against the database, iterating throught the rows to examine each record in trun.

44 Connection-oriented versus Connectionless
Keeps the connection to the data base alive Always up-to-date data Intended for applications with: short running transactions only a few parallel access operations Connectionless No permanent connection to the data source Data cached in main memory Changes in main memory may be in conflict with changes in data source many parallel and long lasting access operations (e.g.: Web applications)

45 Data Bound Controls The General categories for the Data Bound Controls is classified into two types: 1. Simple Data Bound Controls 2. Complex Data Bound Controls 1. Label Box 2. Text Box 1. Combo Box 2. List Box 3. DataGridView

46 IDbConnection: Property ConnectionString

47 ExecuteNonQuery Method

48 ExecuteScalar Method

49 ADO.NET and Transactions
2 transaction models local transactions: transactions for one connection provided by ADO.NET distributed transactions: transactions for several connections usage of Microsoft Distributed Transaction Component (MSDTC) namespace System.Transaction

50 DATABASE PROGRAMS

51 Access the Data in the Access Database using the OLEDB Access
private void Form1_Load(object sender, EventArgs e) { string connectionstring = "provider=Microsoft.JET.OLEDB.4.0; " + "data source = z:\\C#\\Data_Connect\\Data_Connect\\Mark.mdb"; string commandstring = "Select SLNO, NAME from Stud"; OleDbConnection conn= new OleDbConnection(connectionstring); OleDbDataAdapter DataAdapter = new OleDbDataAdapter(commandstring, conn); DataSet DataSet = new DataSet(); DataAdapter.Fill(DataSet, "Stud"); DataTable datatable = DataSet.Tables[0]; foreach (DataRow datarow in datatable.Rows) listBox1.Items.Add(datarow["SLNO"].ToString() + " " + datarow["NAME"].ToString() ); } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Data_Connect { public partial class OLEDBAccess : Form public OLEDBAccess() InitializeComponent(); }

52 The Output of the Above Program is

53 Access the Data in the Access Database using the OLEDB Access (DataGridView)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Data_Connect { public partial class OLEDB_Grid : Form private System.Data.OleDb.OleDbConnection myConnection; private System.Data.DataSet myDataset; private System.Data.OleDb.OleDbCommand myCommand; private System.Data.OleDb.OleDbDataAdapter DataAdapter;

54 public OLEDB_Grid() { InitializeComponent(); string connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source = z:\\C#\\Data_Connect\\Data_Connect\\Mark.mdb"; myConnection = new System.Data.OleDb.OleDbConnection(connectionString); myConnection.Open(); myCommand = new System.Data.OleDb.OleDbCommand(); myCommand.Connection = myConnection; myCommand .CommandText = "Select * from stud"; DataAdapter = new System.Data.OleDb.OleDbDataAdapter(); DataAdapter.SelectCommand = myCommand; myDataset = new System.Data.DataSet(); DataAdapter.TableMappings.Add("Table", "Stud"); DataAdapter.Fill(myDataset); dataGridView1.DataSource = myDataset.Tables["Stud"].DefaultView; }

55 The Output of the Above Program is

56 Access the Data in the Access Database using the OLEDB Access (Using Insert, Select, Update and Delete) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Text; using System.Windows.Forms;

57 namespace Data_Connect
{ public partial class OLEDB_Insert : Form public OleDbConnection con; public OleDbCommand com; public OleDbDataAdapter adp; public OleDbDataReader rs; public OLEDB_Insert() InitializeComponent(); con = new OleDbConnection(); con.ConnectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source = z:\\C#\\Data_Connect\\Data_Connect\\Mark.mdb"; con.Open(); com = new OleDbCommand(); com.Connection = con; adp = new OleDbDataAdapter(com); MessageBox.Show("Connectioin Sucessfully"); }

58 Insert Command private void Inserted_Click(object sender, EventArgs e)
{ try com.CommandText = "insert into STUD values(?,?)"; adp.InsertCommand = com; adp.InsertCommand.Parameters.Add("SLNO", textBox1.Text); adp.InsertCommand.Parameters.Add("NAME", textBox2.Text); adp.InsertCommand.ExecuteNonQuery(); MessageBox.Show("Records are Inserted Successfully"); } catch MessageBox.Show("Runtime Error at Inserted");

59 Select Command private void Selected_Click(object sender, EventArgs e)
{ try com.CommandText = "select * from STUD where SLNO=?"; com.Parameters.Add("SLNO", textBox1.Text); rs = com.ExecuteReader(); if (rs.Read()) textBox1.Text = rs.GetValue(0).ToString(); textBox2.Text = rs.GetValue(1).ToString(); } MessageBox.Show("Selected from the Table Successfully"); catch MessageBox.Show("Runtime error in the Select");

60 Update Command private void Updated_Click(object sender, EventArgs e)
{ try com.CommandText = "select * from STUD where SLNO=?"; com.Parameters.Add("SLNO", textBox1.Text); rs = com.ExecuteReader(); if (rs.Read()) textBox1.Text = rs.GetValue(0).ToString(); textBox2.Text = rs.GetValue(1).ToString(); } MessageBox.Show("Selected from the Table Successfully"); com.CommandText = "update STUD set SLNO=?,NAME=? where SLNO=?"; com.Parameters.Add("NAME", textBox2.Text); com.ExecuteNonQuery(); MessageBox.Show("Records Updated Successfully"); catch MessageBox.Show("Runtime error at the Update");

61 Delete Command Clear Command
private void Deleted_Click(object sender, EventArgs e) { try com.CommandText = "delete from STUD where SLNO=?"; com.Parameters.Add("SLNO", textBox1.Text); com.ExecuteNonQuery(); MessageBox.Show("Record Deleted Successfully"); } catch MessageBox.Show("Runtime error at Deleted"); Clear Command private void Clear_Data_Click(object sender, EventArgs e) { textBox1.Text = ""; textBox2.Text = ""; }

62 The Output of the Above Program is


Download ppt "BUILDING WINDOWS APPLICATION"

Similar presentations


Ads by Google