Q
Problem solve Get help with specific problems with your technologies, process and projects.

How do I access a database with XML and load it to an HTML table?

How do I call MS Access data in XML? And, how do I call XML data in HTML table format?
Accessing the Access database as XML can easily done by following these steps:

  1. Open the Server Explorer view (View menu).

  2. Add a new connection to it, pointing to the MDB. You can do so by right-clicking the root node and selecting Add Connection, or clicking the corresponding icon in the tab. The dialog is the familiar Data link, one from the OleDb days.

  3. You can test the connection at this point.

  4. Drop an OleDbDataAdapter from the Data tab on the Toolbox.

  5. A wizard will appear. Specify the connection you have just created as the one to use.

  6. Configure the SQL statement as you see fit. Make sure you check the Advanced Options dialog. If you're just retrieving and displaying data, you probably won't need those features.

  7. Once you finish, you have two new components on the page: the adapter and a connection. You can now right-click the adapter and select Generate Dataset. This will give you a typed dataset that can show you a preview of the query at design-time. Select a New dataset and give it a meaningful name (remember, it will become a class of your project). Leave the "Add this dataset to the designer" option checked.

    Now you have a new component, the dataset. From here, you can fill the data and get its XML representation simply with the following code:

    private void Page_Load(object sender, System.EventArgs e)
    {
     oleDbDataAdapter1.Fill(dsCustomers1);
     Response.Write(dsCustomers1.GetXml());   
    }

But to load the data in an HTML table you don't even need to get the XML representation. You can just use built-in databinding which generates an HTML table at run time. Steps follow:

  1. Drop a DataGrid from the WebControl tab in the Toolbox.

  2. Set its DataSource property to point to your dataset.

  3. Play with the datagrid Autoformat context menu. It will save you quite some time in formatting data!

  4. Remember to set EnableViewState to false! If you don't need advanced grid features, such as row select/editing support, etc., disabling it will make your page considerably lighter.

  5. Finally, replace the (silly) Response.Write line from the snippet above with the following:

    this.DataGrid1.DataBind();

And that's it! Two lines of code for a simple grid. Don't put that into production so fast, however. Paging is almost always necessary, editing and selecting is cool and may be required, and more important: going to your database on each page Load is certainly not the best option. There are others, each with its own pros and cons: storing the dataset in session state, enabling ViewState and let the page itself carry all the rendered data, using programmatic Cache object, etc. But my answer will give you a quickstart. ;)

Dig Deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

TheServerSide.com

Close