Access DB DataAdapter To Gridview

Access DB DataAdapter To Gridview

Purpose: – Illustrates using in VB.NET ASP.NET.

Prerequistes:

  1. Install Visual Web Developer 2010
  2. Install SQL Server Express
  3. Download Northwind and Pubs Databases
  4. Attach Northwind Database to Databases in Sql Express
  5. Attach pubs Database to Databases in Sql Express

Notes:

  • You can build your own library of syntax examples by using same web site over and over and just add new web forms to it.
  • This example uses MS Access 2010 to create the Contacts DB

Instructions:

  1. Use Visual Web Developer 2010
  2. Create new web site;
    • Click File/New Web Site
    • Select ASP.NET Website Template
    • Select Visual Basic for Language
    • name of Web Site could be VBNET_ASPNET_Syntax.
  3. Add New folder named “Database_ADONET”
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: Database_ADONET
  4. Add Web Form Named AccessDBDataAdapterToGridview to Database_ADONET folder
    • Right-click Database_ADONET folder;
    • add new item;
    • Select Web Form
    • Check place code behind in separate file
    • Web Form name could be AccessDBDataAdapterToGridview
  5. Click on copy code in code below to copy code into web form AccessDBDataAdapterToGridview.aspx
  6. Click on copy code in second set of code below to copy code into code-behind AccessDBDataAdapterToGridview.aspx.vb
  7. Right-click on AccessDBDataAdapterToGridview.aspx in solution explorer and select view in browser

Step 1: Click on Copy Code to Cut-n-paste code into AccessDBDataAdapterToGridview.aspx

< %@ Page Language="VB" AutoEventWireup="false" CodeFile="AccessDBDataAdapterToGridview.aspx.vb" Inherits="ADONET_AccessDBDataAdapterToGridview" %>
 
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>Populate Gridview1 with DataAdapter</h2>
        <asp:gridview ID="GridView1" runat="server">
        </asp:gridview>
 
    </div>
    </form>
</body>
</html>

Step 2: Click on Copy Code to Cut-n-paste code into AccessDBDataAdapterToGridview.aspx.vb

Imports System.Data
Imports System.Data.OleDb
 
Partial Class ADONET_AccessDBDataAdapterToGridview
    Inherits System.Web.UI.Page
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' create a connection string 
        Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Contacts.accdb"
        Dim myConnection As OleDbConnection = New OleDbConnection
        myConnection.ConnectionString = connString
        ' create a data adapter 
        Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Contacts", myConnection)
        ' create a new dataset 
        Dim ds As DataSet = New DataSet
        ' fill dataset 
        da.Fill(ds, "Contacts")
        ' write dataset contents to an xml file by calling WriteXml method 
        ' Attach DataSet to DataGrid 
        GridView1.DataSource = ds
        GridView1.DataBind()
 
 
    End Sub
 
End Class

Step 3: Click on Copy Code to Cut-n-paste code into web.config right after the appSettings section

<connectionstrings>
    <add name="Northwind_ConnectionString"
      connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" />
    <add name="Pubs_ConnectionString"
      connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" />
  </connectionstrings>