To create an ADO.NET application in Visual Studio .NET, perform the following steps:
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.
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:
The Connection page of the Data Link Properties window is displayed.
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.
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.
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.
This option allows you to create an SQL statement that enables the data adapter to access the database.
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.
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.
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:
Make sure that the Employee table is selected in the Choose Which Table(s) to Add to the Dataset text box.
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:
If the Properties window is not displayed, select the Properties Window option from the View menu or press the F4 key.
A DataGrid control showing the column headings is displayed, as shown in Figure.
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:
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.
XML Related Interview Questions
|Soap Tool Interview Questions||HTML Interview Questions|
|PHP Interview Questions||ASP.NET Interview Questions|
|PHP5 Interview Questions||Java Interview Questions|
|CSS Interview Questions||XSLT Interview Questions|
|Java XML Interview Questions||XMLHttpRequest (XHR) Interview Questions|
|ebXML Interview Questions||XML DOM Interview Questions|
|XML-RPC Interview Questions||XSD Interview Questions|
|Soap Web Services Interview Questions||XSL Interview Questions|
|Xml Publisher Interview Questions|
Basics Of Xml
Basics Of Web Services
Introduction To Soap
Introduction To Uddi
Introduction To Wsdl
Creating A Web Service Using The Microsoft Soap Toolkit
Building Web Applications On The .net Platform
Creating An Asp.net Web Service
Creating A Web Service From An Interface
Introduction To The Atl Server
Creating A Web Service Using The Atl Server Library
Design And Creation Of The Knowledge Share Web Service
Introduction To Java Xml Technologies
Developing Java Web Services
Design And Creation Of A Web Service Using The Ibm Toolkit
Introduction To Mobile Applications
Creating A Mobile Application That Consumes A Web Service
Web Services Development With Jdeveloper
Creating Web Services Using Perl
Integration Of Xml Web Services With The Office Xp And Sql
Server 2000 Toolkits
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.