NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Binding SQL Data to a DataGrid Object Using a Select * Query

The NGWS frameworks and runtime has a DataGrid class that makes the display of large databases an easier task. This section describes how to access a SQL database and bind that data to a DataGrid object. It uses a "select *" SQL query to get all rows and columns of data from the database. Sections that follow this one will then show how to manage that data.

To access a SQL database

  1. Import the proper namespaces into your page. This provides your code with access to the necessary classes.
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SQL" %>
  2. Within the <script language="C#" runat="server"> tag, implement a Page_Load function that contains a connection to the database, creates and fills a new DataSet with the database information, and then binds the DataGrid object to the DataSet. This is shown in the following example code.
    <script language="C#" runat="server">
        public DataView Source;
        protected void Page_Load(Object Src, EventArgs E ) 
        {
    1. Set up a connection to the "pubs" database located on the local computer.
              SQLConnection myConnection = new
                 SQLConnection("server=localhost;uid=sa;pwd=;database=pubs");
    2. Connect to the SQL database using a "select *" query to get all the data from the "Authors" table.
              SQLDataSetCommand myCommand = new SQLDataSetCommand("select * 
                                            from Authors", myConnection);
    3. Create and fill a Dataset.
              DataSet ds = new DataSet();
              myCommand.FillDataSet(ds, "Authors");
    4. Set up a DataView, and bind MyDataGrid to it.
              Source = new DataView(ds.Tables[0]);
              MyDataGrid.DataSource=Source ;
              MyDataGrid.DataBind();
          }
      </script>
  3. In the <body> of the page, display the DataGrid information.
    <body>
    1. Show a page title.
        <h3><font face="Verdana">Simple Select to a DataGrid 
                                 Control</font></h3>
    2. Display the data.
        <ASP:DataGrid id="MyDataGrid" runat="server"
          Width="700"
          BackColor="#ccccff" 
          BorderColor="black"
          ShowFooter="false" 
          CellPadding=3 
          CellSpacing="0"
          Font-Name="Verdana"
          Font-Size="8pt"
          HeaderStyle-BackColor="#aaaadd"
          MaintainState="false"
        />
      </body>

To see this example run, go to the ASP+ Quick Start and run sample DataGrid1.aspx.