When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Friday, September 15, 2000

Data Access with ASP.NET, Part 3

  • Read Part 1
  • Read Part 2

  • In Part 2 we looked at how to use ADO+ to read data into a Dataset. Great. But now that we have that information in a Dataset how can we display it? We'll answer that question in this part!

    - continued -

    Displaying a Dataset
    OK, great, we have our gelatinous Dataset brimming over with the information from the ADODatasetCommand query. Now... we need to display this information! Datasets can be displayed through the use of a DataGrid. A DataGrid is an ASP.NET HTML control that renders a Dataset into a nifty HTML table. DataGrids have tons of properties and customization features, but for this article we'll simply look at a very simple use of a DataGrid. An ASP.NET DataGrid control can be created with the following syntax:

    <asp:DataGrid id="DataGridName"

    To bind a Dataset to a DataGrid, we first need to set the DataSource property of the DataGrid to our Dataset; then, we simply call the DataBind() method! Oftentimes we will specify a Dataview in the DataSource property of the DataGrid control. For our example below, we will use the DefaultView DataView of the objDataset Dataset. (Egad, that's wordy!)

    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.ADO" %>
    <script language="vb" runat="server">
      Sub Page_Load(Source as Object, E as EventArgs)
        Dim strConnString as String
        strConnString = "Provider=SQLOLEDB; Data Source=(local); " & _
                        "Initial Catalog=pubs; User ID=sa"
        Dim objConn as ADOConnection
        objConn = New ADOConnection(strConnString)
        Dim strSQL as String = "SELECT * FROM authors"
        Dim objDSCommand as ADODatasetCommand
        Dim objDataset as Dataset
        objDSCommand = New ADODatasetCommand(strSQL, objConn)
        objConn = Nothing  
        objDataset = New Dataset
        objDSCommand.FillDataSet(objDataset, "Author Information")
        MyFirstDataGrid.DataSource = _
              objDataset.Tables("Author Information").DefaultView
      End Sub
      <asp:DataGrid id="MyFirstDataGrid" runat="server" />

    There are a couple of things to note in the above code. First, recall that Datasets are disconnected representations of data. Hence, we can close our ADOConnection object before using our Dataset object, as I showed above. Second, note that we specified to use the DefaultView for the DataGrid display by specifying DatasetObjectName.Tables(DatasetCommandName).DefaultView. The output of the above script can be seen below. With old-school ASP, to produce the following output, we'd simply loop through a Recordset object, creating the table as we proceed. The steps are a bit different using ASP.NET and ADO+.

    The DataGrid creates a table-view of the data in the Dataset

    Comments from Scott Guthrie
    Scott Guthrie, one of the ASP.NET Developers, shares the following comments:
    "The following line of code required today (and in beta1):

    MyGrid.DataSource = MyDataSet.Tables["foo"].DefaultView

    will go away in beta2. Instead, it will just be:

    MyGrid.DataSource = MyDataSet

    This will eliminate the whole need to understand DefaultViews and/or refrence tables specifically. Should be a lot easier to understand conceptually, and a lot less code to have to require users to write! I'm also pushing on the ADO+ team for some additional object model changes with the dataset model that could reduce the total required lines of code by another few lines."

    Also, "For straight forward connected data access, you'll be able to use the ADO+ DataReader in beta1. This is similar to a Recordset (and unlike the ADO+ DataSet is connected instead of disconnected) and will probably look very familiar to existing ADO programmers."

    Well, that about wraps up data access in ASP.NET using ADO+. Hopefully you've learned something new! (Chances are you've learned a lot of new things!) All these new changes do seem a bit overwhelming, but hang in there! The best thing to do is roll your sleeves up and get playing around with ASP.NET code. As always, for the latest ASP.NET information, be sure to check out the ASP.NET Article Index!

    Happy Programming!

    ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article