CodeSnip: Fill a DataGrid From an Access Database
page 1 of 1
Published: 30 Aug 2004
Unedited - Community Contributed
This code 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.
by Web Team at ORCS Web
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 8284/ 16

The code below demonstrates how to connect to and query a Microsoft Access database and 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 
End Sub 

<title>Fill A DataGrid From Access Database</title> 
  <asp:DataGrid id="oGrid" runat="server" /> 

by Brad Kingsley the Founder and President of ORCS Web, Inc. - a company that provides managed hosting services for clients who develop and deploy their applications on Microsoft Windows platforms.

User Comments

Title: database in   
Name: Shakti
Date: 2011-03-06 3:31:33 AM
It really Works!!!
Title: thank you!   
Name: slieck
Date: 2010-09-25 12:42:10 AM
thanks you so much! it help me a lot!
Title: Fill a DataGrid From an Access Database   
Name: Aditya
Date: 2007-03-09 8:00:47 AM
Good But Some Scripting Error is Coming..
Title: Good work   
Name: Yasin
Date: 2006-04-11 4:02:11 AM
It helped me a lot to begin with my

thanks a lot.
Title: it worked for me when i did this   
Name: salman
Date: 2005-07-27 8:07:53 AM
Title: re: Great   
Name: Brad Kingsley
Date: 2005-04-12 7:08:37 AM
You are welcome!

Title: Great   
Name: Mário Lopes
Date: 2005-04-12 7:03:26 AM
This code helped me a lot.
Thanks from Portugal
Title: Yes, it works   
Name: Brad
Date: 2005-03-06 5:46:25 PM
Yes, the code should work fine.


Title: haave you corrected the code?   
Date: 2005-03-06 2:11:01 AM
Good day! I just want to ask if the code above is already working.

I am just a new programmer. thanks for the code
Title: RE: Compilation Error   
Name: Brad
Date: 2004-10-09 10:25:02 AM
I see there is a % sign missing from the third line of the sample code. Please add it before the @ and see if that resolves the issue.

We will get the code sample corrected shortly.



Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-23 6:56:43 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search