Access DB DataReader To Gridview

Pluralsight courses
LinkedIn Premium uses PayScale to estimate job salaries

Premium (Not Free) Video Tutorials

Free Video Tutorials & Free Tools

Access DB DataReader 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 AccessDBDataReaderToGridview 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 AccessDBDataReaderToGridview
  5. Click on copy code in code below to copy code into web form AccessDBDataReaderToGridview.aspx
  6. Click on copy code in second set of code below to copy code into code-behind AccessDBDataReaderToGridview.aspx.vb
  7. Right-click on AccessDBDataReaderToGridview.aspx in solution explorer and select view in browser

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

 XML |  copy code |? 
< %@ Page Language="VB" AutoEventWireup="false" CodeFile="AccessDBDataReaderToGridview.aspx.vb" Inherits="ADONET_AccessDBDataReaderToGridview" %>
 
< !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 Reader</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 AccessDBDataReaderToGridview.aspx.vb
 Visual Basic |  copy code |? 
Imports System.Data
Imports System.Data.OleDb
 
Partial Class ADONET_AccessDBDataReaderToGridview
    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
 
        Dim sql As String = "SELECT * FROM Contacts"
        Dim cmd As New OleDbCommand(sql, myConnection)
        myConnection.Open()
        Dim reader As OleDbDataReader
        reader = cmd.ExecuteReader()
        Dim Table1 As DataTable
        Table1 = New DataTable("Employees")
        'creating a table named Employees
        Dim Row1 As DataRow
        'declaring row for the table
 
        Dim EmployeeID As DataColumn = New DataColumn("ContactID")
        'declaring a column named ContactID
        EmployeeID.DataType = System.Type.GetType("System.Int32")
        'setting the datatype for the column
        Table1.Columns.Add(EmployeeID)
        'adding the column to table
        Dim FullName As DataColumn = New DataColumn("LastName")
        FullName.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(FullName)
        While reader.Read()
            Row1 = Table1.NewRow()
            'declaring a new row
            Row1.Item("ContactID") = reader.GetInt32(0)
            'filling the row with values. Item property is used to set the field value.
            Row1.Item("LastName") = reader.GetString(2)
            'filling the row with values. adding LastName
            Table1.Rows.Add(Row1)
        End While
        reader.Close()
        myConnection.Close()
        GridView1.DataSource = Table1
        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
 XML |  copy code |? 
<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>

Related posts:

  1. Access DB DataAdapter To Gridview Access DB DataAdapter To Gridview...
  2. VB.NET ASP.NET Table Load From SqlDataReader Source Code Example VB.NET ASP.NET Table Load From SqlDataReader Source Code Example...
  3. C# ASP.NET Table SqlDataReader Example – SqlDataReader Table Example C# ASP.NET C# ASP.NET Table SqlDataReader Example - SqlDataReader Table Example C#...
  4. C# ASP.NET DropDownList Bind To Table Source Code Example C# ASP.NET DropDownList Bind To Table Source Code Example...
  5. VB.NET ASP.NET DropDownList Bind To Table Source Code Example VB.NET ASP.NET DropDownList Bind To Table Source Code Example...

Related posts brought to you by Yet Another Related Posts Plugin.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Powered by WP Hashcash

This blog uses the cross-linker plugin developed by Web-Developers.Net