Creating a Sample ADO.NET Application XML

To create an ADO.NET application in Visual Studio .NET, perform the following steps:

  1. Launch Visual Studio .NET from the Programs menu.
  2. Click on the File menu. In the list that is displayed, point to the New menu and then select the Project option. The New Project dialog box is displayed.
  3. In the Project Types pane, select the Visual Basic Projects option.
  4. In the Templates pane, select the ASP.NET Web Application option.
  5. In the Location text box, type the address of the Web server on which you will develop the Web application.
  6. Type the name of the application as SampleWebApplication.
  7. Click on the OK button to create the Web application. Visual Studio .NET creates a blank form for you.
  8. From the Web Forms Toolbox, add a DataGrid control and a Button control to the form.

Tip?/td>
If the Toolbox is invisible, click on the Toolbox option on the View menu.

The previous steps create the interface for Sample Web Application. To make the application functional, you need to add the programming logic to the DataGrid and Button controls.

Adding the Programming Logic to the DataGrid Control

A DataGrid control displays or modifies the data in a dataset. The data from the dataset is displayed in the form of rows and columns. However, before displaying the data, you need to connect to a database by using ADO.NET. Visual Studio .NET provides you with several data adapters that you can use to connect to a database.

As discussed earlier, the data from a database is first stored in the dataset. A data adapter acts as an interface between a database and its corresponding dataset. You can use a data adapter to transfer data between a database and a dataset.

A data adapter is a control in the Data toolbox that is used to connect to a database. Visual Studio .NET provides you with data adapters, such as OleDbDataAdapter and SqlDataAdapter. You can use SqlDataAdapter to connect to an SQL database. However, to connect to a database other than SQL, you need to use OleDbDataAdapter.

Note?/td>
In this chapter, we will use SqlDataAdapter to connect to the SQL database, Employees.

To create a connection to a database, perform the following steps:

  1. Drag SqlDataAdapter from the Data toolbox.
  2. The Data Adapter Configuration Wizard is displayed.
  3. Click on the Next button to start the wizard.
  4. In the Choose Your Data Connection page, click on the New Connection button to create a new connection to an SQL database.
  5. The Connection page of the Data Link Properties window is displayed.

    Note?/td>
    If you are using a database other than SQL, you need to select the appropriate data adapter in the OLE DB Provider(s) list in the Provider tab of the Data Link Properties window. By default, the Microsoft OLE DB Provider for SQL Server option is selected.

  6. From the Select or Enter a Server Name combo box, select the server to which you want to connect.
  7. In the Enter Information to Log On to the Server group box, select the authentication mode to connect to an SQL server.
  8. Note?/td>
    Visual Studio .NET provides you with two options. To connect to an SQL server by using the Windows authentication mode, select the Use Windows NT Integrated Security option. However, if you select the Use a Specific Name and Password option, you need to specify the username and the password in the Username and Password text boxes, respectively. To leave the password blank, check the Blank Password check box.

  9. Select the Select the Database on the Server option to choose the database to which you want to connect.
  10. Select the name of the database from the drop-down list as Employees.
  11. Click on the Test Connection button to test the connection to the Employees database. If the connection is successful, a message box showing the text Test connection succeeded is displayed.
  12. Click on the OK button to close the message box.
  13. Click on the OK button to close the Data Link Properties window.
  14. The Choose Your Data Connection page is displayed. The name of the Employees database is displayed in the Which Data Connection Should the Data Adapter Use list box.

    Click on the Next button to continue with the wizard.

    The Choose a Query Type page is displayed. This page provides you with several options to access the database.

  15. Select the Use SQL Statements option.
  16. This option allows you to create an SQL statement that enables the data adapter to access the database.

  17. Click on the Next button.
  18. The Generate the SQL Statements page is displayed. You can type the query in the What Data Should the Data Adapter Load into the Dataset text box.

  19. Type the following SQL statement into the text box:
  20. SELECT *
  21. FROM Employee
  22. Here, Employee is the name of the table in the Employees database. The preceding SQL statement allows you to select all fields from the Employee table. You can also click on the Query Builder button to graphically create the query.

    The Data Adapter Configuration Wizard uses the SQL query to create the Insert, Update, and Delete statements to insert, update, and delete records Click on the Next button.

    The View Wizard Results page is displayed. This page displays a list of the tasks that the wizard has performed. The Data Adapter Configuration Wizard creates the SELECT, INSERT, UPDATE, and DELETE statements.

  23. Click on the Finish button to create the data adapter.

The Data Adapter Configuration Wizard creates a data adapter named sqlDataAdapter1, which contains information about the Employee table. In addition, Data Adapter Configuration Wizard creates a connection, sqlConnection1, which contains information about how to access the Employees database.

After you create a connection by using the Data Adapter Configuration Wizard, you need to create a DataSet object. To generate a DataSet object, perform the following steps:

  1. Click anywhere in the form to activate it.
  2. On the Data menu, select the Generate Dataset option.
  3. The Generate Dataset window is displayed.
  4. From the Choose a Dataset group box, select the New option button. In the text box that is adjacent to the New option button, type the name of the DataSet object as EmpDataSet.
  5. Tip?/td>
    Make sure that the Employee table is selected in the Choose Which Table(s) to Add to the Dataset text box.

  6. Check the Add This Dataset to the Designer check box.
  7. This adds the dataset to the component tray of the form.
  8. Click on the OK button to close the Generate Dataset window.

Visual Studio .NET creates a dataset with the name EmpDataSet1, which contains the data from the Employee table. In addition, an EmpDataSet1.xsd file is created that contains information about the EmpDataSet1 dataset.

The data from the table is still not visible in the DataGrid control. To display the records from the dataset, you need to bind the DataGrid control to the dataset. To do so, perform the following steps:

  1. In the Design view, click on the DataGrid control to display its properties.
  2. Tip?/td>
    If the Properties window is not displayed, select the Properties Window option from the View menu or press the F4 key.

  3. In the Properties window, select the DataSource property.
  4. Click on the down arrow to select the EmpDataSet1 dataset.
  5. Click on the down arrow of the DataMember property and select the Employee table.

A DataGrid control showing the column headings is displayed, as shown in Figure.

The Web Form in Design View.

The Web Form in Design View.

After you perform the preceding steps, the DataGrid control contains only the column headings. To load the records from the table to the DataGrid control, you need to write the code for the Button control.

Adding the Programming Logic to the Button Control

Before you add code to the Button control, change the Name property of the control to Load. Now, double-click on the control to display the code window. Alternatively, you can choose the Code option on the View menu or press the F7 key. In the Click event of the Button control, add the following code:

In the previous code, the Click event of the Button control takes two parameters, sender and e, of the Object type. Inside the Click event, the Clear() method of the DataSet class is called. This method clears all records in the EmpDataSet1 dataset.

Next, the Fill() method of the DataSet class adds or refreshes the data in the dataset to match the data to the records in the database. The Fill() method takes the name of the data-set as the parameter. Finally, the DataBind() method is called to bind the DataGrid control to the database that the DataSource property of the control specifies:
EmpDataSet1.

Testing the Application

After adding the code, you have created the application. You can now test it. To test the application, select the Start option on the Debug menu. Alternatively, you can press the F5 key. The application opens a Web form in the browser. The Web form contains a Load button. Click on the button to display the records from the Employee table. Figure shows the Web form at runtime.

The Web Form at Runtime.

The Web Form at Runtime.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML Topics