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

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 asp.net   
Name: Shakti
Date: 2011-03-06 3:31:33 AM
Comment:
It really Works!!!
Title: thank you!   
Name: slieck
Date: 2010-09-25 12:42:10 AM
Comment:
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
Comment:
Good But Some Scripting Error is Coming..
Title: Good work   
Name: Yasin
Date: 2006-04-11 4:02:11 AM
Comment:
It helped me a lot to begin with my ASP.net

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

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

Thanks,

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

I am just a new ASP.net programmer. thanks for the code
Title: RE: Compilation Error   
Name: Brad
Date: 2004-10-09 10:25:02 AM
Comment:
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.

Thanks,

Brad

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-09-21 4:45:36 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search