databasedev.co.uk - database solutions and downloads for microsoft access

ASP.NET

Working with the New Data Source Controls in ASP.NET (Part I)

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.

The New Data Source Controls of ASP.NET 2.0

ASP.NET 2.0 and beyond ships with a lot of data source controls, namely:

  • ObjectDataSource
  • SQLDataSource
  • AccessDataSource
  • XMLDataSource
  • LinqDataSource

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 ObjectDataSource Control

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

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:

  • SQL Server
  • Access
  • Oracle
  • DB2

The AccessDataSource Control

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>

Conclusion

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.

The Author

Joydip Kanjilal is a Microsoft MVP in ASP.NET.

He has more than 12 years of industry experience in IT with more than six years in Microsoft .NET and its related technologies.

He has authored articles for some of the most reputable sites, including http://www.asptoday.com, http://www.devx.com, http://www.aspalliance.com, http://www.aspnetpro.com, http://www.sql-server-performance.com, and http://www.sswug.com.

Many of these articles have been selected at http://www.asp.net, MicrosoftÂ’s official site for ASP.NET. Joydip was also a community credit winner at http://www.community-credit.com a number of times.

He is currently working as a Lead Architect in a reputable company in Hyderabad, India. He has years of experience in designing and architecting solutions for various domains. His technical strengths include, C, C++, VC++, Java, C#, Microsoft .NET, AJAX, Design Patterns, SQL Server, Operating Systems, and Computer Architecture.

Joydip blogs at http://aspadvice.com/blogs/joydip and spends most of his time reading books and blogs, and writing books and articles. His hobbies include watching cricket and soccer and playing chess