ASP.NET allows access and use of the following data sources:
Database (for example: Access, SQL Server, Oracle, MySQL)
XML document
Business Objects
Flat files
ASP.NET hides the complex data access process and provides more advanced classes and objects through which data can be accessed more easily. These classes hide all the complex code for connection, data access, data retrieval and data manipulation.
ADO.NET technology provides a bridge between various ASP.NET control objects and background data. In this guide, we focus on data access and briefly introduce data.
Retrieving and displaying data in ASP.NET requires two types of data controls:
Data source control - It manages data connections, data selection and other tasks such as paging and caching of data, etc.
Data Display Control - This will constrain and display the data and allow manipulation of the data.
We'll explore data constraints and data source control in detail later. In this section, we will use the SqlDataSource control to access data. In this chapter, the GridView control is used to display and manipulate data.
We will also use the Access database, which contains detailed information on .Net books available on the market. Name our database ASPDotNetStepByStep.mdb and we will apply a data table named DotNetReferences.
This table contains the following columns: ID, Title, AuthorFirstName, AuthorLastName, Topic, and Publisher. The picture below is a screenshot of this data table:
Let us directly follow the steps below to practice:
(1) Create a website and add SqlDataSourceControl in the web form.
(2) Click the Configure Data Source option.
(3) Click the New Connection button to establish a database connection.
(4) Once connections are established, you can save them for later use. Next, you will be asked to set up the select statement:
(5) After selecting the items in the columns, click the next button to complete the remaining steps. Observe the WHERE, ORDER BY, and Advanced buttons. These buttons allow you to execute the where clause, order by clause and specify insert, update and delete commands in SQL respectively. This way you can operate on the data.
(6) Add GridView control to the table. Select the data source and use the AutoFormat option to generate the control.
(7) The GridView control set after this can display the column title, and this program can be executed.
(8) Finally execute the program.
The code involved above is listed below:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="dataaccess.aspx.cs" Inherits="datacaching.WebForm1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title> Untitled Page </title> </head> <body> <form id="form1" runat="server"> <div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString= "<%$ ConnectionStrings:ASPDotNetStepByStepConnectionString%>" ProviderName= "<%$ ConnectionStrings: ASPDotNetStepByStepConnectionString.ProviderName %>" SelectCommand="SELECT [Title], [AuthorLastName], [AuthorFirstName], [Topic] FROM [DotNetReferences]"> </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <Columns> <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> <asp:BoundField DataField="AuthorLastName" HeaderText="AuthorLastName" SortExpression="AuthorLastName" /> <asp:BoundField DataField="AuthorFirstName" HeaderText="AuthorFirstName" SortExpression="AuthorFirstName" /> <asp:BoundField DataField="Topic" HeaderText="Topic" SortExpression="Topic" /> </Columns> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#999999" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView> </div> </form> </body></html>