How to Bind MS Access Table into DataList in Asp.Net C# VB.Net

For different DataSources we need to Bind Controls differently. DataList has no exception. Here in this article I will explain “How to connect to MS Access DataBase and Bind Data into a DataList Control using Asp.Net C# VB.Net“.

How to Bind MS Access Table into DataList in Asp.Net C# VB.Net

To Do the Example Preapare an MS Access Table:
Note: Of course copy your MS Access Database into your application root folder.

Add a DataList Control to Bind MS Access Database Table:

<asp:DataList id="DataList1" runat="server">
                <asp:Label id="lblBrandName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label><br />
                <asp:Label id="lblCategoryName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label><br />
                <asp:Label id="lblProductName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Roll") %>'></asp:Label><br />
                <b>Admission Date:</b>
                <asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Admission Date") %>'></asp:Label><br />
                <asp:Label id="lblHeader" runat="server" Font-Names="Tahoma" ><h1>List of Students:</h1></asp:Label>
                <hr />
                <hr />
                <hr />

Now you need to write code in server side to bind DataList 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 DataList 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();

            DataList1.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

            DataList1.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, DataList, MS Access, VB.Net
One comment on “How to Bind MS Access Table into DataList in Asp.Net C# VB.Net
  1. Sunil Kumar says:

    hey i have created two dropdownboxes in suppose is select 1 option from drop down list 1 and second option from drop down list 2 when i click on submit then it’s display the data on label control but what the code i have to write to fetch the particular row and column in database and display on label control in

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code