CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
DataSource Controls in ASP.Net 2.0 – Part 1

By Satheesh Babu
Posted On Mar 19,2008
Article Rating:
Average Rating: 5
No of Ratings: 1
No of Comments: 1
Category: ASP.Net
Print this article.
DataSource Controls in ASP.Net 2.0 – Part 1

Datagrid is one of the most commonly used controls in ASP.Net. Most often we will do edit, update, and delete operation in Datagrid control which leads to repeating the same code often. With the introduction of Asp.Net 2.0, Datagrid is replaced by GridView Control which can be found in data tab of Visual studio 2005. Using the GridView the common operations like edit, update, delete and select can be performed without writing a line of code. To make this happen a new set of data access controls are introduced in ASP.Net 2.0 called as DataSource Controls. These controls encapsulate the data from the various datasources and can be configured declaratively. This article will be helpful for those who are starting ASP.Net 2.0 from ASP.Net 1.x to use datasource controls.

In GridView Control, the ItemDataBound Event is replaced by RowDataBound Event and some new events are also added.

DataSource Controls
SqlDataSource Control
It provides data access through data stores that understand the SQL syntax. This includes databases such as Microsoft SQL Server, Microsoft Access, Oracle, and so on.

ObjectDataSource
It provides data access through a developer-created class. (Well architected Web applications oftentimes have a set of classes that provide methods to manipulate or retrieve the data from an underlying data store. These classes provide a layer of encapsulation, hiding the details of the underlying data store.)

XmlDataSource
It provides data access to XML document.

Using DataSource Controls with GridView

SqlDataSource Control
1) Drag a SqlDatasource and GridView control from the tool box.
2) Now Configure the SqlDatasource from “SqlDatasource Task” by clicking “Configure DataSource” as shown in the below figure.



3) This will open a window where you can specify the connection string if there is already one in web.config file or you can click the new connection to build a connection string to connect to the database. Click Next.


4) Here you can specify a custom query or stored procedure to get the data from database or you can select “specify table or view” radio button to select the table you want to display as shown in figure.

5) To configure edit, update and delete operation in Gridview we have to specify the query. The above one will only select the data from database. For configuring edit/update/delete, click “Advanced” button and check the “Generate Insert, Update and Delete statements” and click Ok. Refer the below figure. Click Next.

6) Click Finish. Now we have to configure the GridView Control to accept the data from the Datasource Control. We have to set the DataSourceID of the Gridview control to the ID of the SqlDataSource Control. To make the Delete Command to work we have to specify the DataKeyNames property to the primary key field name of the database table(Empid in our example).To specify the Paging, Sorting, Edit and update select enable paging, sorting, editing, deleting in Gridview tasks as shown in the figure.

7) Run the Application.




Master/Detail in GridView
1) Drag a DropDownList and bind the EmpID from the database using a SqlDataSource control by configuring it in the same way as we did for the Gridview.
2) Drag a GridView and configure SqlDataSource control to display the Details data.
3) Click “Configure DataSource” to specify the connection string, click next. In the Next screen you need to specify the columns to display in the detail Gridview. Here you will find a “Where” button and clicking the button will open a new window like the one shown below figure where you can configure the filter condition. Select a column from the “Column” dropdownlist which lists all the columns from Employee table. Select the appropriate operator i.e. “=” in our example. Select the source that will give you the data for the filtering condition for your query and it is dropdownlist in our example so select Control from the “Source” dropdownlist. Select ID of the dropdown in ControlID dropdown, provide default data if necessary and click "ADD" and then “OK”.  In the Parent window, click next and then finish to complete.


4) Make AutoPostBack Property of the EmpID dropdownlist to true.
5) Run the application.

Downloads

Download Source

Conclusion

Thus we have learnt to use SqlDataSource control with a walkthrough for creating Master/Detail in Gridview in this article. Download the source attached with this article and see it in action. The next part of this article will explain about ObjectDataSource and XmlDataSource controls.

Happy Coding!!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Thanks
I spend 2 hours and found your solution
Thanks a ton.