- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
c# code 39 checksum C HA PTER 7 XML IN ADO. NET in Visual C#
C HA PTER 7 XML IN ADO. NET Encode Code 3 Of 9 In C#.NET Using Barcode maker for Visual Studio .NET Control to generate, create USS Code 39 image in VS .NET applications. www.OnBarcode.comUSS Code 39 Recognizer In Visual C#.NET Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comWorking with DataSet and DataAdapter
Encoding Barcode In Visual C#.NET Using Barcode maker for .NET framework Control to generate, create Barcode image in .NET applications. www.OnBarcode.comEAN-13 Encoder In C# Using Barcode maker for VS .NET Control to generate, create EAN13 image in .NET applications. www.OnBarcode.comTo understand how DataSet and DataAdapter can be used to manipulate data, you will create an application like the one shown in Figure 7-5. Make 1D Barcode In Visual C# Using Barcode encoder for Visual Studio .NET Control to generate, create Linear 1D Barcode image in .NET framework applications. www.OnBarcode.comQR Code Printer In Visual C# Using Barcode generator for .NET framework Control to generate, create QR Code image in Visual Studio .NET applications. www.OnBarcode.comFigure 7-5. Application to illustrate DataSet functionality The application is a typical data-entry screen. The combo box shows a list of all employee IDs. After you select an employee ID, the details of that employee (first name, last name, home phone, and notes) are displayed in the text boxes. The Insert, Update, and Delete buttons perform the respective operations. All the operations INSERT, UPDATE, and DELETE are performed on the DataSet and not against the actual database table. After all the operations are completed, you can click the Save button to make all the changes in the actual database. The application uses the Employees table of the famous Northwind database. Now let s dissect the application step by step and see how DataSet and DataAdapter have been put to use. Code-39 Creator In Visual C#.NET Using Barcode printer for .NET framework Control to generate, create Code 3 of 9 image in Visual Studio .NET applications. www.OnBarcode.comPaint MSI Plessey In Visual C#.NET Using Barcode maker for .NET framework Control to generate, create MSI Plessey image in .NET framework applications. www.OnBarcode.comFilling a DataSet
Code 3/9 Decoder In None Using Barcode reader for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comScanning USS Code 39 In Visual Basic .NET Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comIf you see the source code of the preceding application, you will find a few variables declared at the form level. The declaration is shown in Listing 7-3. Listing 7-3. Form-Level Variables string strConn = @"data source=.\sqlexpress;initial catalog=northwind; integrated security=true"; DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(); SqlConnection cnn; GS1 DataBar Stacked Creation In Java Using Barcode printer for Java Control to generate, create GS1 DataBar Truncated image in Java applications. www.OnBarcode.comCode 39 Full ASCII Recognizer In .NET Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.comCHAPTER 7 XML IN A DO.NE T
Creating UCC - 12 In None Using Barcode encoder for Word Control to generate, create UPC-A Supplement 2 image in Microsoft Word applications. www.OnBarcode.comPrint Code 3 Of 9 In .NET Framework Using Barcode drawer for ASP.NET Control to generate, create USS Code 39 image in ASP.NET applications. www.OnBarcode.comThe strConn string variable stores the database connection string, which uses a local installation of SQL Server Express as indicated by the data source attribute. Then variables of type DataSet, SqlDataAdapter, and SqlConnection are declared. You must ensure that you have imported the System.Data and System.Data.SqlClient namespaces before you declare these variables. The Form_Load event handler of the form contains the code shown in Listing 7-4. Listing 7-4. Filling a DataSet private void Form1_Load(object sender, EventArgs e) { cnn = new SqlConnection(strConn); SqlCommand cmdEmployees = new SqlCommand(); cmdEmployees.CommandText = "SELECT * FROM employees"; cmdEmployees.Connection = cnn; da.SelectCommand = cmdEmployees; da.Fill(ds, "Employees"); FillEmployees(); } The code creates a SqlCommand object and sets its CommandText property to fetch all the records from the Employees table. The Connection property is set to the SqlConnection object created earlier. The SqlCommand object just created is assigned to the SelectCommand property of the SqlDataAdapter instance. The SelectCommand property determines the records to be populated in the DataSet later. Next, the Fill() method of the SqlDataAdapter is called. It takes two parameters: the DataSet to be filled and the name of the resultant DataTable. Notice that the code neither opens the connection nor closes it. This is so because the SqlDataAdapter class does that internally for us. Finally, a helper method, FillEmployees(), is called and fills the combo box with the list of employee IDs. The FillEmployees() method is discussed later. Code 3 Of 9 Drawer In None Using Barcode creation for Word Control to generate, create Code-39 image in Office Word applications. www.OnBarcode.comPDF-417 2d Barcode Creator In None Using Barcode encoder for Word Control to generate, create PDF 417 image in Word applications. www.OnBarcode.com Note The SqlDataAdapter class closes the connection automatically for us only if opened by
Code 128 Code Set B Creation In Java Using Barcode generation for Java Control to generate, create Code 128 image in Java applications. www.OnBarcode.comEncoding QR-Code In Java Using Barcode creator for Java Control to generate, create QR Code image in Java applications. www.OnBarcode.comSqlDataAdapter itself. If the connection is opened prior to calling the Fill() method, SqlDataAdapter Draw Denso QR Bar Code In None Using Barcode drawer for Microsoft Word Control to generate, create QR Code image in Office Word applications. www.OnBarcode.comPDF-417 2d Barcode Decoder In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comwill not close it automatically.
Accessing Data from DataSet
When you select an employee ID from the combo box, the employee details should be displayed in the other text boxes. The relevant code is written in the SelectedIndexChanged event of the combo box and is shown in Listing 7-5. C HA PTER 7 XML IN ADO. NET
Listing 7-5. Accessing Data from a DataSet private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string id = comboBox1.SelectedItem.ToString(); DataRow[] rows = ds.Tables["Employees"].Select("EmployeeID=" + id); textBox1.Text = rows[0]["firstname"].ToString(); textBox2.Text = rows[0]["lastname"].ToString(); textBox3.Text = rows[0]["homephone"].ToString(); textBox4.Text = rows[0]["notes"].ToString(); } The code first stores the selected employee ID in a string variable. To find the corresponding employee record from the DataSet, we use the Select() method of DataTable, which accepts the selection criteria and returns an array of DataRow objects matching those criteria. In our example, we need to select the employee whose EmployeeID column value matches the one selected in the combo box. EmployeeID is the primary column for the Employees table and hence we know that it will return only one DataRow. The DataRow can be accessed by using typical array notation. Notice how the column names are used to access the individual column values. Instead of column names, you could have used column indexes. The various column values are displayed in the respective text boxes.
|
|