Retrieving Data from Microsoft SQL Server 2008 Using ASP.NET 3.5
(Page 1 of 4 )
Most of the web applications on the Internet require retrieving data from a database. Almost all websites today are database-driven, so it is of primary importance for any developer to retrieve data from a website's database and display it on the web browser. This article illustrates basic ways of retrieving data from Microsoft SQL Server 2008 using the ASP.NET 3.5 web platform.
Purposely, this article is written for first time developers in ASP.NET and MS SQL server 2008. However it is highly recommended to read the pre-requisite tutorial: "Creating an ASP.NET Database using MS SQL 2008 in Visual Web Developer 2008," because the database that you create in that article will be used in the exercises illustrated in this tutorial.
As a review, the sample database table has four records, or rows, in the database, and six fields:
If you are ready, then let's start the ball rolling.
Introducing: ASP.NET Data Source Controls
Most of the web development work in ASP.NET 3.5 is handled by so-called "web controls," a click-and-drag strategy implemented in Visual Web Developer Express 2008. For example, if you want to include a server-side text box form, simply click and drag Textbox in the toolbox to the Default.aspx source code.
Web controls make web development faster and simpler (because of the simple click-and-drag method, with the source code automatically generated by Visual Web Developer). They also save the developer from needing to write sophisticated and complex ASP.NET scripts.
To retrieve data from the MS SQL 2008 database, you need an ASP.NET 3.5 page and a working database (which is thoroughly discussed in the article linked to above). Once you have these preliminary requirements, though, there is still no way for ASP.NET to communicate, connect and retrieve records from the MS SQL database.
In ASP.NET, a web control set called "Data Source" controls is designed to connect to and interact with the database. Currently there are two important Data Source controls in ASP.NET (which you can see in View à Toolbox à Data): SqlDataSource and AccessDataSource.
SqlDataSource is primarily used for retrieving records from SQL databases such as MS SQL Server 2008 (or other supported databases, like Oracle). AccessDataSource is used when retrieving records from MS Access database.
In the previous example, since you are using an MS SQL Server 2008 database, you will be using SqlDataSource controls to retrieve data. The following are the steps you need to take:
Step 1: Launch Visual Web Developer Express 2008.
Step 2: Open "firstdatabasetest" projects (containing the database, default.aspx and other files created from the first tutorial, referred to earlier).
Step 3: You will then see the source code of Default.aspx. Go to View à Toolbox à SqlDataSource, click and drag "SqlDataSource" to between <form id="form1" runat="server"><div> and </div></form>
After the click and drag, the source code will be updated automatically:
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
</div>
</form>
Next: Configuring SqlDataSource Controls >>
More ASP.NET Articles
More By Codex-M