Bind GridView with MS Access Database Table in ASP.NET

I found that developers struggle "To connect with MS Access Database from Asp.net". That's why in this post I will show you how one can access MS Access database from Asp.net ASPX page to bind GridView.

Tip

Create Access Database and Save Database File In Your Application's Bin Folder.

And Type Following Line in Your Connection String.

OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data source=" + Server.MapPath("Bin/MyDatabase.mdb") + "";

Solution

protected void Page_Load(object sender, EventArgs e)

    {
        if (Page.IsPostBack == false)

        {

            BindGridview();

        }

    }

    public void BindGridview()

    {
        OleDbConnection con = new OleDbConnection();

        con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data source=" + Server.MapPath("Bin/MyDatabase.mdb") + "";

        con.Open();

        OleDbCommand cmd = new OleDbCommand("SELECT * FROM MyTable", con);

        OleDbDataAdapter da = new OleDbDataAdapter(cmd);

        DataTable dt = new DataTable();

        da.Fill(dt);

        gdvMyGridView.DataSource = dt;

        gdvMyGridView.DataBind();

    }

Summary

Hope now you can make connection with MS-Access Database & also can retrieve data from MS-Access from your asp.net application.

Ebook Download
View all
Learn
View all