Bind GridView with MS Access Database Table in Asp.Net C# VB.Net

In many forums I found that developers struggle “To connect with MS Access Database from“. That’s why in this post i will show you how one can access MS Access database from ASPX page to bind GridView control. To do that first open any MS Access Database and then create below table:


Note: Of course copy your MS Access Database into your application root folder.

Add a GridView Control to Bind MS Access Database Table:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">

Now you need to write code in server side to bind GridView with MS Access Database. First make a connection string for your MS Access Database. And then read data from MS Access database. After that bind it to GridView Control:

C# Code:

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            string sFilePath = Server.MapPath("MSAccess.accdb");
            DataTable dt;
            OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");

            using (Conn)
                OleDbCommand cmd = new OleDbCommand("SELECT * FROM Student", Conn);
                OleDbDataAdapter oDA = new OleDbDataAdapter(cmd);
                dt = new DataTable();

            GridView1.DataSource = dt;


Note: Don’t forget to add “System.Data” and “System.Data.OleDb” namspaces.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim sFilePath As String = Server.MapPath("MSAccess.accdb")
            Dim dt As DataTable
            Dim Conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sFilePath & ";Persist Security Info=False;")

            Using Conn
                Dim cmd As New OleDbCommand("SELECT * FROM Student", Conn)
                Dim oDA As New OleDbDataAdapter(cmd)
                dt = New DataTable()
            End Using

            GridView1.DataSource = dt

        End If

    End Sub

Note: Don’t forget to add “System.Data” and “System.Data.OleDb” namspaces.


Hope now you can make connection with MS-Access Database and also can retrieve data from MS-Access into your Asp.Net application.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, Database, Gridview, MS Access, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *