AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=503&pId=-1
CodeSnip: Fill a DataGrid From an Access Database
page
by Web Team at ORCS Web
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 11291/ 15

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.


Product Spotlight
Product Spotlight 

©Copyright 1998-2020 ASPAlliance.com  |  Page Processed at 2020-11-28 9:28:35 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search