Originally posted October 2, 2001 on OrcsWeb.com
The code below demonstrates how to connect to and query a Microsoft Access database then bind those results to an ASP.Net datagrid for display as an HTML table.
<%@ Page language="VB" Debug="false" %> <%@ Import Namespace="System.Data" %> <@ Import Namespace="System.Data.OleDb" %> <script language="VB" runat="server"> Sub Page_Load(Sender as Object, E as EventArgs) Dim oConn As OleDbConnection Dim oComm As OleDbDataAdapter Dim sConn As String Dim sComm As String Dim oDataSet As New DataSet 'Build the connection string sConn = "Provider=Microsoft.Jet.OLEDB.4.0;" sConn += "Data Source=C:\Inetpub\wwwroot\Sample\grocertogo.mdb;" sConn += "Persist Security Info=False" 'Build the SQL string sComm = "SELECT Products.ProductID, " sComm += "Products.ProductName, " sComm += "Products.ProductDescription, " sComm += "Products.UnitPrice " sComm += "FROM Products" 'Usually you would use error-handling here. It is left out to 'make the code as simple as possible. 'Create the connection and command objects oConn = New OleDbConnection(sConn) oComm = New OleDbDataAdapter(sComm, oConn) 'Fill the dataset with the results of the query oComm.Fill(oDataSet, "Products") 'Set the grid source to the dataset and bind the data oGrid.DataSource=oDataSet.Tables("Products").DefaultView oGrid.DataBind() End Sub </script> <html> <head> <title>Fill A DataGrid From Access Database</title> </head> <body> <asp:DataGrid id="oGrid" runat="server" /> </body> </html>