aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to Connect MS Access Database in using C#, VB.NET

Mar 9, 2015

Here I will explain how to connect ms access database in using c#, To use Microsoft (MS) access database to insert, update, delete data in we need to use System.Data.OleDb namespace reference in c#,

To use ms access database in first we need to create database in ms access for that follow below steps.

First Open Access 2013 and Create new database called “Sampledb” for that Open Access 2013 à select Blank Desktop Database à Give Name like “Sampledb” and click Create like as shown below

Now right click on your table and select Design view to design table with required columns like as shown below

Now add required fields in your table with required data type like as shown below

Now we will try to insert data into access database userdetails table for that open your aspx page and write the code like as shown

<html xmlns="">
<head runat="server">
<title>Connect Ms Access Database in using C#, VB.NET</title>
<form id="form1" runat="server">
<table align="center">
<td><asp:TextBox ID="txtUsername" runat="server"/></td>
<td><asp:TextBox ID="txtEducation" runat="server"/></td>
<td><asp:TextBox ID="txtLocation" runat="server"/></td>
<td><asp:Button ID="btnSubmit" Text="Submit" runat="server" onclick="btnSubmit_Click"/></td>
<asp:GridView ID="gvDetails" runat="server"></asp:GridView>

C# Code

using System;
using System.Data.OleDb;
using System.Data;


Imports System.Data.OleDb
Imports System.Data

After completion of adding namespaces you need to write the code like as shown below in code behind

C# Code

OleDbConnection con;
OleDbCommand cmd;
protected void Page_Load(object sender, EventArgs e)
protected void btnSubmit_Click(object sender, EventArgs e)
using (con = new OleDbConnection(@"PROVIDER=Microsoft.ACE.OLEDB.12;" + @"DATA SOURCE=E:\accessdb\Sampledb.accdb"))
cmd = new OleDbCommand();
cmd.CommandText = "insert into userdetails(UserName,Education,Location)values(" + txtUsername.Text + "," + txtEducation.Text + "," + txtLocation.Text + ")";
cmd.Connection = con;
txtUsername.Text = "";
txtEducation.Text = "";
txtLocation.Text = "";
protected void BindUserDetails()
DataSet ds = new DataSet();
string strquery = "SELECT * FROM userdetails";
using (con = new OleDbConnection(@"PROVIDER=Microsoft.ACE.OLEDB.12;" + @"DATA SOURCE=E:\accessdb\Sampledb.accdb"))
using (cmd = new OleDbCommand(strquery, con))
OleDbDataAdapter Da = new OleDbDataAdapter(cmd);

Imports System.Data.OleDb
Imports System.Data
Partial Class VBCode
Inherits System.Web.UI.Page
Private con As OleDbConnection
Private cmd As OleDbCommand
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
Using con = New OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12;" + "DATA SOURCE=E:\accessdb\Sampledb.accdb")
cmd = New OleDbCommand()
cmd.CommandText = "insert into userdetails(UserName,Education,Location)values(" + txtUsername.Text + "," + txtEducation.Text + "," + txtLocation.Text + ")"
cmd.Connection = con
txtUsername.Text = ""
txtEducation.Text = ""
txtLocation.Text = ""
End Using

End Sub

Protected Sub BindUserDetails()
Dim ds As New DataSet()
Dim strquery As String = "SELECT * FROM userdetails"
Using con = New OleDbConnection("PROVIDER = Microsoft.ACE.OLEDB.12; "+"DATA SOURCE =E:\accessdb\Sampledb.accdb")
Using cmd = New OleDbCommand(strquery, con)
Dim Da As New OleDbDataAdapter(cmd)
End Using
End Using
gvDetails.DataSource = ds
End Sub
End Class

If you observe above oledb connection I used like “@"PROVIDER=Microsoft.ACE.OLEDB.12;" + @"DATA SOURCE=E:\accessdb\Sampledb.accdb"” because I have office 2013 or Access 2013

Note: If you have Access 2007 or Access 2010 you need to use oledb connection like as shown below

con = new OleDbConnection(@"PROVIDER=Microsoft.Jet.OLEDB.4.0;" + @"DATA SOURCE = E:\accessdb\Sampledb.accdb")

How to Connect MS Access Database in using C#, VB.NET

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Unknown said...

Sir, I am quite new in ASP.NET with c# ,as i think its a very interesting language but i am new in there. I had done two projects in ASP.NET but still i get confused in here. At that time i am not paying much more attention as a result i felt guilty that i am not familier in this language.Sir please help me to learn from basic to become a programmer.
I study your all tutorials from your blog.

Anonymous said...

very nice....

Aravind L said...

Thank you giving wonderful and valuable notes. I done my project using your tutorial.

neeraj said...

sir, my 2003 db is password protectd pl. help about it.

I have googled but not found correct solution.
I m using this string in web.confg

with this even in gridview I am getting error as Incorrect Password, though I have saved correct password in sqldatasource for gridview.

the red pill said...

Hello, thanks for your tutorial, but there's an error

"The supplier 'Microsoft.ACE.OLEDB.12' is not registered on the local computer."

I change the platform from "Any CPU" to "x86" but the nothing changed

PS: I'm using (Windows 7 64bits, Office 2013 64 bits, visual studio 2012).

Anonymous said...

thank you

Unknown said...

how to extract image and save it to a file with or in server side ?

neha jangid said...

i have a run time error in this line...
c# code

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.