[ Pobierz całość w formacie PDF ]
.Estimated time to complete this lab: 60 minutes Module 8: Using ADO.NET 57Exercise 1Retrieving DataIn this exercise, you will create a Windows Forms application to display a listof customers and their details.You will create a DataSet object in a classmodule and use this data set to obtain the customer details for the selectedcustomer.To create the project1.Open Visual Studio.NET.2.On the File menu, point to New, and then click Project.3.In the Project Types box, click Visual Basic Projects.4.In the Templates box, click Windows Application.5.Change the name of the project to CustomerProject , set the location toinstall folder\Labs\Lab081\Ex01, and then click OK.To create the Windows Form1.In Solution Explorer, rename the default form to frmCustomer.vb.2.In the Properties window, change the Text property of the form toCustomer Details, and change the Name property to frmCustomer.3.Open the Project Property Pages dialog box, and change the Startup objectto frmCustomer.4.Add controls to the form, as shown in the following illustration.58 Module 8: Using ADO.NET5.Set the properties of the controls as shown in the following table.Control Property name Property valueListBox1 Name lstCustomersLabel1 Name lblIDText IDTextBox1 Name txtIDTextLabel2 Name lblCompanyText CompanyTextBox2 Name txtCompanyTextLabel3 Name lblEmailText EmailTextBox3 Name txtEmailTextLabel4 Name lblAddressText AddressTextBox4 Name txtAddressTextMultiline TrueSize.Height 326.Save the project.To create the Cargo class1.On the Project menu, click Add Class.2.Rename the class Cargo.vb, and then click Open.3.At the top of the class, add an Imports statement to reference theSystem.Data.SqlClient namespace.4.In the Cargo class, create private class-level variables, using theinformation in the following table.Variable name TypedstCargo DataSetconCargo SqlConnection Module 8: Using ADO.NET 59To create the connection1.Create a constructor for the class.2.Instantiate the conCargo Connection object.3.Using the information in the following table, set the ConnectionStringproperty of conCargo.Name ValueData Source localhostInitial Catalog CargoIntegrated Security TrueTo create the GetCustomers method1.Create a method called GetCustomers for the class.The method takes noarguments and has no return value.2.In this method, declare and instantiate a SqlDataAdapter object calledadpCust.Pass the following information to the constructor.Argument ValueselectCommandText Select CustomerID, FirstName, LastName,CompanyName, Email, Address from CustomersselectConnection conCargo3.Instantiate the DataSet object.4.Use the Fill method of adpCust to pass the following information.Argument ValuedataSet dstCargosrcTable CustTableTo create the CustList property1.Define a read-only property called CustList that returns a DataTable.2.In the Get clause, return the CustTable table from dstCargo.3.Save and build the project.60 Module 8: Using ADO.NETTo populate the list box1.Open the frmCustomers Code Editor.2.Declare and instantiate a private class-level variable called objCargo of typeCargo.3.Create the frmCustomers_Load event handler.4.Call the GetCustomers method of the objCargo object.5.Add the following code to loop through the customers and populate the listbox:Dim CurRows( ) As DataRow, CurRow As DataRowCurRows = objCargo.CustList.Select( )For Each CurRow In CurRowslstCustomers.Items.Add(CurRow("FirstName").ToString & _" " & CurRow("LastName").ToString)Next6.Call the SetSelected method of lstCustomer to select the first item in the list.To populate the text boxes1.Create the lstCustomers_SelectedIndexChanged event handler.2.Declare an Integer variable called RowNum, and then store theSelectedIndex property of the list box in the variable.3.Declare a DataRow variable called CurRow, and then store the rowidentified by RowNum in it by using the following line of code:objCargo.CustList.Rows(rowNum)4.Using the information in the following table, populate the text boxes withdata from this row.Text box Column nametxtID CustomerIDtxtCompany CompanyNametxtEmail EmailtxtAddress Address5.Save the project.To test the application1.On the Build menu, click Build, and resolve any build errors.2.On the Debug menu, click Start.You should see that the list box and textboxes are populated with data.3.Click on a different customer in the list and verify that the text boxesdisplay the relevant data.4.Quit the application.5.Quit Visual Studio.NET. Module 8: Using ADO.NET 61Exercise 2Updating DataIn this exercise, you will extend the class created in the previous exercise toallow editing of data.The form in the starter file has been modified to make thetext boxes read-only by default and to include a button that you will code to testthe functionality.To prepare the form1.Open Visual Studio.NET.2.On the File menu, point to Open, and then click Project.3.Set the location to install folder\Labs\Lab081\Ex02\Starter, clickCustomerProject.sln, and then click Open.4.In the frmCustomer.vb code window, locate the btnEdit_Click procedureand write code to set the ReadOnly property of the following textboxes toFalse." txtCompany" txtEmail" txtAddressTo update the DataSet1.Open the code window for Cargo.vb.2.Create a new public method called EditDetails that takes the followingparameters by value and has no return value.Parameter name TypeRowNum IntegerID IntegerstrCompany StringstrEmail StringstrAddress String3.Using the information in the following table, declare and initialize a variable.Name Type Initialization ValuedstCargo.Tables( custtable ).Rows(RowNum)editRow DataRow4.Call the BeginEdit method of the editRow object.62 Module 8: Using ADO.NET5.Update the data in the DataSet with the arguments passed to the procedure.Use the information in the following table.editRow item ArgumentCompanyName strCompanyEmail strEmailAddress strAddress6.Call the EndEdit method of the editRow object.To update the underlying data1.Using the information in the following table, declare and instantiate avariableName Type Construction Value"Update customers setcommUpdate SqlCommandCompanyName= @company,email=@email, address=@addresswhere customerid=@id", conCargo2.Use the Add method of the command s Parameters collection to createparameters for this command.Use the information in the following table
[ Pobierz całość w formacie PDF ]