With ASP.NET 2.0 and after, you are introduced to a bunch of new data source controls, i.e., LinqDataSource, ObjectDataSource, XmlDataSource, SqlDataSource, etc. You can use the newly added data source controls of ASP.NET 2.0 to implement CRUD (Create, Read, Update, and Delete) operations in your applications without having to write much code. All of these controls support paging, sorting, caching, editing, inserting, selecting and deleting data. You can use these controls to bind data to your ASP.NET data bound controls (GridView, Repeater, ListView, etc) declaratively, i.e., without writing even a single line of code. This article discusses these controls and how easily one can work with them.
ASP.NET 2.0 and beyond ships with a lot of data source controls, namely:
Besides these controls, you also have the SiteMapDataSource control that can be used to loads a site map file and expose it to controls such as the TreeView and SiteMapPath. Using these controls is easy: simply drag and drop one from the toolbox into your web form in the design view mode in Visual Studio.
The AccessDataSource, SQLDataSource, and XMLDataSource controls are used to bind data to the ASP.NET data controls from Access, SQL Server Database, or XML data sources, respectively. You use the ObjectDataSource control to bind data to the data controls from generic business objects. The ObjectDataSource in ASP.NET can be used to display, edit, and sort data on a Web page with little or no code, i.e., you can use it declaratively as well as programmatically. Here is how you can associate an ASP.NET ObjectDataSource control to an ASP.NET data source control:
<asp:gridview ID="GridView1" runat="server" datasourceid="ObjectDataSource1" /> <asp:objectdatasource id="ObjectDataSource1" runat="server" />
The ObjectDataSource control exposes a TypeName property that can be used to specify a type to instantiate which in turn will be used for performing the CRUD operations. The ObjectDataSource control supports properties such as SelectMethod, UpdateMethod, InsertMethod, and DeleteMethod that correspond to CRUD operations on your application's data. Here is an example that illustrates how you can use the SelectMethod, UpdateMethod, DeleteMethod and InsertMethod properties of this control:
<asp:ObjectDataSource ID="ObjectDataSource1" TypeName="EmployeeDataLayer" SelectMethod="GetEmployees" UpdateMethod="UpdateEmployees" DeleteMethod="DeleteEmployees" InsertMethod="InsertEmployees" runat="server"/>
The SqlDataSource control in ASP.NET can be used to connect to any SQL database provider and perform CRUD operations against the database. The MSDN states, "The SqlDataSource control enables you to use a Web server control to access data that is located in a relational data base. This can include Microsoft SQL Server and Oracle databases, as well as OLE DB and ODBC data sources. You can use the SqlDataSource control with data-bound controls such as the GridView, FormView, and DetailsView controls to display and manipulate data on an ASP.NET Web page, using little or no code." Here is an example that shows how you can use this control:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=" <%$ ConnectionStrings:EmployeeConnectionString %>" SelectCommand="SELECT [id], [fname], [lname], [basic] FROM [employee]" UpdateCommand="UPDATE [employee] SET [fname] = @fname, [lname] = @lname, _ [basic] = @basic WHERE [id] = @id" DeleteCommand="DELETE FROM [employee] WHERE [id] = @id"/>
Note that even if the name of this control is SqlDataSource, it can still be used to connect to any of the following databases:
The AccessDataSource control as the name implies, is used to connect to any Access database and perform CRUD operations. Actually this control makes your life much easier when you are trying to connect to an Access database and perform database operations. Here is how the markup code of the AccessDataSource control looks like:
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataSourceMode="DataSet" DataFile="~/App_Data/Employee.mdb" SelectCommand="SELECT [ID], [FirstName], [LastName], [Basic] FROM [Employee]"> </asp:AccessDataSource>
In this first part in the series of articles on data source controls in ASP.NET, we discussed the ObjectDataSource, AccessDataSource and SqlDataSource controls. In the next and concluding part in this series of articles, we will discuss the XmlDataSource, LinqDataSource and the newly added EntityDataSource controls.