Tuesday, July 31, 2012

GridView Examples for ASP.NET 2.0: Accessing Data with the DataSource Controls

One of the most common tasks Web developers face is working with data. The data might be from an XML file, from a database, or provided through a more abstract interface, such as through a set of classes acting as a data access layer. Needless to say, a large amount of time goes into rather mundane data-related tasks such as:
  • Writing the code to access data from a data store.
  • Writing the code to take a user's input and update the data within the data store.
  • Writing the code to delete one or more records from the data store.

The main data source controls that you'll find yourself working with in conjunction with the GridView are:
  • SqlDataSource—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—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—provides data access through an XML document.



Accessing Data with the SqlDataSource
To add any of the data source controls to an ASP.NET page, simply drag them from the Visual Studio 2005 Toolbox onto the appropriate ASP.NET Web page's Design view. Once you do this, the control's Smart Tag will appear with an option titled "Configure Data Source." Choosing this option will launch a wizard that prompts for the requisite information.
For the SqlDataSource, the first screen prompts you to specify the connection string for data store you want to connect to (see Figure 1). The drop-down shown in Figure 1 contains connection strings that exist in the <connectionStrings> section of the application's Web.config file. If you haven't yet defined any connection strings, or don't see the one you want, you can create a new connection string by clicking the New button.
Figure 1
Once you have created or chosen an existing connection string, the next stage of the wizard prompts you to specify what data you are interested in working with (see Figure 2). You can either select a table or view from the drop-down, picking the fields you want returned, or you can provide your own SQL statement or stored procedure name to call. From this pane you can also specify a WHERE condition to limit the returned data, as well as indicate how the retrieved data should be ordered. Through the Advanced Options button you will find options to create not only a SELECT statement to retrieve data, but INSERT, UPDATE, and DELETE statements as well. (We'll examine creating INSERT, UPDATE, and DELETE statements later on in this article.)
Figure 2
The last step of the wizard allows you to run the SELECT statement specified against the data store to see what data, exactly, is returned.
That's all there is to configuring a SqlDataSource. After stepping through the SqlDataSource's wizard, we have specified the data we want to display. All that remains is to display the data, which is where the GridView comes in. Start by dragging and dropping a GridView control from the Toolbox onto the ASP.NET page. The Smart Tag will prompt you to select the GridView's DataSource. Choose the SqlDataSource you just added and, like magic, the GridView will automatically have columns corresponding to the fields returned by the data source control.
Figure 3 shows the Design view of an ASP.NET Web page after tying a GridView to a SqlDataSource that returns the ProductID, ProductName, QuantityPerUnit, UnitPrice, and UnitsInStock fields of the Products table in the Northwind database. Figure 4 shows this ASP.NET page when viewed through a browser.
Figure 3 (Click on the graphic for a larger image)



<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
</script>
<html  >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="productsDataSource" Runat="server"
          "SelectCommand="SELECT [ProductID], [ProductName], " & _
          "[QuantityPerUnit], [UnitPrice], [UnitsInStock] " & _
          "FROM [Products]"
            ConnectionString=
            "<%$ ConnectionStrings:NWConnectionString %>"
                DataSourceMode="DataReader">
        </asp:SqlDataSource>
        <asp:GridView ID="productGridView" Runat="server"
             DataSourceID="productsDataSource"
            DataKeyNames="ProductID" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField ReadOnly="True"
                  HeaderText="ProductID" InsertVisible="False"
                  DataField="ProductID"
                    SortExpression="ProductID"></asp:BoundField>
                <asp:BoundField HeaderText="ProductName"
                  DataField="ProductName"
                  SortExpression="ProductName"></asp:BoundField>
                <asp:BoundField HeaderText="QuantityPerUnit"
                    DataField="QuantityPerUnit"
                    SortExpression="QuantityPerUnit"></asp:BoundField>
                <asp:BoundField HeaderText="UnitPrice"
                   DataField="UnitPrice" SortExpression="UnitPrice">
                </asp:BoundField>
                <asp:BoundField HeaderText="UnitsInStock"
                  DataField="UnitsInStock"
                  SortExpression="UnitsInStock"></asp:BoundField>
            </Columns>
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>











If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website