Geekpedia Programming Tutorials






Connect to Access Database in Visual Studio .NET

This tutorial uses DataReaders to read the data from a Microsoft Access Database in Visual Studio .Net 2003. ASP.NET web application is used in this tutorial so at the end you will have a web page that derives its data from an Access database.

On Friday, January 6th 2006 at 04:17 PM
By Nabeel Akhtar (View Profile)
*****   (Rated 4.7 with 34 votes)
Contextual Ads
More ASP.NET Resources
Advertisement
Video Tutorial; Opens in a new window

This tutorial is available as a VIDEO in Flash Format. To access the video tutorial, click here





You can use DataSets or DataReaders to read the information from the database. A DataReader is about twice as fast as a DataSet (according to Sams Publication). This tutorial uses DataReaders to read the data from a Microsoft Access Database
in Visual Studio .Net 2003. ASP.NET web application is used in this tutorial so at the end you will have a web page that derives its data from an Access database.



Following are screen-shots along with some description but I recommend you view the VIDEO TUTORIAL.



STEP-1:



Open up Visual Studio .NET 2003 and click on New Project on the Start page:









STEP-2:



In the New Project window select Visual Basic Projects on the left side and click on ASP.NET Web Application on the right.
Assign a location in the location tab and click OK to continue









STEP-3:



When the web form opens up, click on the Server Explorer tab at the left:









STEP-4:



Right click on Data Connections and select Add Connection...









STEP-5:



Under the Provider tab, select Microsoft Jet 4.0 OLE DB Provider. You will use this to connect to an Access database. If you have a SQL database, make sure you select the right provider from this list. Click on Next to continue










STEP-6:



Under the Connection tab, select the ... button to select the database you are connecting to.
Once the location to the database has been defined, click on the Test Connection button make sure your database is visible to the application.













STEP-7
:



From the Toolbox menu look under the Data tab and click on OleDbConnection. Once clicked, click on the white space of your project. You can also drag and drop this to the white space.














STEP-8
:



Right click on the OleDbConnection1 in your project area and select Properties.
In the ConnectionString field, click on the drop-down button and select the database path that shows up. This creates a connection to the database in our project.









STEP-9:



Next we need to set a command for this database that will query the database and bring back data. For this we will need to use the
OledbCommand object. Select it and click on the white are in your project window.









STEP-10:



Right click on OleDbCommand1 object in your project window and click on Properties. In the CommandTextfield, type in your select statement

(e.g. Select * FROM tablename WHERE column1 = 'somevalue' )









STEP-11:



Before you close the properties, there is one more thing you need to do. You need to tell the application that this command is associated with the database connection we created earlier. To do this, select the Connection field and expand the Existing option to see the OleDbconnection1 we created. Click on it to select it.







STEP-12:



Now we have all the objects that we needed to define the database parameters. Its not time to create a DataGrid that will display the data in our web page. To do that, select the DataGrid option under the Web Forms tab and draw a window in your project area (simply click and drag).













STEP-13:



Double click anywhere in the project window to view the code for this page. When the code window comes up, type in the code as shown in the following image:









STEP-14:



Go back to the project view and click on the RUN button to run the code. The results from the database will show up in a web page.










I hope this helps.



More Tutorials


Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Munir on Friday, January 13th 2006 at 08:33 AM

Excellent ! . i need more briefly how i'll connect vb & Access database

by Nabeel Akhtar on Friday, January 13th 2006 at 09:35 AM

Munir, did you watch the video tutorial? I don't think it too long.

by Ryan on Wednesday, January 18th 2006 at 09:10 AM

Very good article. I would also however mention about making sure the folder in your wwwroot directory is shared. I had big probs accessing the database.

by JJ on Wednesday, January 18th 2006 at 01:31 PM

hi, this is very good and helpful article. I had a question. I followed up all steps and finally did not get result. I guess the reason problobly happens on the oledbconnection1.commandstring. I selected the database path that showed up on first line and been asked to choose include or not the password by a poped up box. i chose dont and the path changed to "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source="C:\Documents and Settings..." however, I guess there is no real connection that is why I can not get data retrieved from database. eventhough, the test connection was ok and when I open server exploer and right click the test table, select retrieve data from table, I did get what I want to get. could you spent some time to reply to me. thank you.

by Nabeel Akhtar on Wednesday, January 18th 2006 at 01:55 PM

JJ, when prompted, choose "Incude Passwords". I believe your database is password protected. This is approach is secure to some level... alternative will be to strore the data connection string in the database and write a function to call it everytime you make the connection to the DB.

-hope this helps.

by jj on Wednesday, January 18th 2006 at 02:40 PM

thank you for your reply. well, I tried and found whatever include or not, it does not matter at all. what do you think the problem? was it like what I guess?
thank you

by jj on Wednesday, January 18th 2006 at 05:17 PM

for the function, do you mean the one that manully create the connection object and set up connection string and call this function every time need to connect to database?

by Nabeel Akhtar on Thursday, January 19th 2006 at 08:43 AM

JJ, Try to delete the old connection and make a new one... maybe that will help. Also post the exact error message that you receive while debugging...

by shareen khan on Tuesday, January 24th 2006 at 01:07 PM

Your tutorial is very helpful, have you got a tutorial on being able to write to the database too?

by Nabeel Akhtar on Tuesday, January 24th 2006 at 01:14 PM

Shareen,
The process is pretty much the same. All you have to do is use the INSERT statement and pass-in the parameters...

I don't have a tutorial on INSERTing data into the db at this time.

by Ian on Saturday, January 28th 2006 at 04:36 PM

I was trying for over half an hour to coonect an access database until I came upon this article and did it in two minuets. One question, I only want to connect the database to the webapplication so that when a user types data into the web form, it is collected in the database. Therfore am I correct in thinking I do not need to include the datagrid in the webapplication?

by Nabeel Akhtar on Monday, January 30th 2006 at 09:13 AM

Ian, Datagrids are used to display the data already contained in the database. If you want to create a webform to collect information, then you will need to take the data from the input fields and INSERT it into the database. A datagrid is NOT required, however you can choose to refresh the datagrid and display the new entry. - I hope this makes sense

by Chris Veale on Monday, January 30th 2006 at 10:03 PM

HI.

Just like to say this is one of the best tutorials of database implementation I have seen in a while...well done.

I followed all your steps fine and managed to get it connecting to a local database I have running on my webserver, thats fine, however I am now trying to access a database via a UNC filename on another networked computer, and for some reason this is failing. I am getting an error message saying "The Microsoft Jet database engine cannot open the file '\\Zeus\envirodata\db\hilltop.mdb'. It is already opened exclusively by another user, or you need permission to view its data."

This is trying to use the database as the Admin userID as I did for my local drive and its not working for me. I also tried coding in my network user id but this also failed.

I have been told the database doesnt actually have a username and password associated with it, so Im assuming that a network ID or an admin generic userid would work, but I cant make this happen.

Do you have any thoughts that might help me make this happen? It would be VERY useful for me to understand how to connect to access databases on machines different than the webserver I am developing on.

Cheers

Chris Veale

by Sachin Arole on Tuesday, January 31st 2006 at 02:51 AM

Very explantory article. Helped me to get the right connection within no time.
Keep up the good work ...

by sk on Wednesday, February 1st 2006 at 04:36 PM

Hi, I followed the above and i keep getting the error message "Server Error in 'WebApplication3' Application. The microsoft jet database engine cannot open the file 'c:/Documents and Settings/shareen/desktop/db2.mdb'. It is already opened excusively by another user, or you need permission to view its data. line 48: OleDbConnection1.Open()

Ive double checked to see if access was open but it wasn't. Any ideas how to sort this out.

by Chris Veale on Wednesday, February 1st 2006 at 05:51 PM

Hi Sk.

your DB needs to be in the same location as the web server and you need to use the mappath function.

I havve had similar problems with databases outside the webserver, but at least that much I know.

Cheers

Chris

by sk on Thursday, February 2nd 2006 at 10:23 AM

Hi Chris,

Thanks for your reply, im kind of new at this, but what do you mean when you say mappath function?

thankyou
sk

by Chris Veale on Thursday, February 2nd 2006 at 03:04 PM

Hi Sk.

There is a function you can use to connect to a database of the form....

Dim myPath As String = Server.MapPath("mydb.mdb")

and then in the connection string set the datasource to "myPath"

Chris

If you want more info try doing a google for mappath and the language you are using....my example is VB.


by Sadiq on Sunday, February 19th 2006 at 04:27 PM

I need your HELP

when i use your movie and i did the same way you did in your tutorial i am getting the following error messages

oledbconnection1.open()
the database is open by other user and you dont have permission message

by Sanjay on Thursday, February 23rd 2006 at 01:35 AM

Its really helped me to connect database.
Thanks a lot!

by Sooraj on Saturday, April 22nd 2006 at 12:01 PM

Can i connect to a ms access database present in a different computer

by Nabeel Akhtar on Sunday, April 23rd 2006 at 10:15 AM

Yes you can. You can map the drive of the other computer and call it anything (e.g. F:/). Then when the time comes to choose the location of the database, simply point to the drive F: and select the database.

by Gary on Tuesday, May 2nd 2006 at 10:14 AM

I am new to this also. I tried this and I am getting the following response:

"An invalid data source is being used for DataGrid1. A valid data source must implement either IListSource or IEnumerable."

Line 11: <body MS_POSITIONING="GridLayout">
Line 12: <form id="Form1" method="post" runat="server">
Line 13: <asp:DataGrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 80px; POSITION: absolute; TOP: 32px" runat="server" DataSource="<%# OleDbConnection1 %>">
Line 14: </asp:DataGrid></form>
Line 15: </body>

[ArgumentException: An invalid data source is being used for DataGrid1. A valid data source must implement either IListSource or IEnumerable.]
System.Web.UI.WebControls.BaseDataList.set_DataSource(Object value) +112
ASP.DataGridTest_aspx.__DataBindDataGrid2(Object sender, EventArgs e) in c:\inetpub\wwwroot\Trainee\DataGridTest.aspx:13
System.Web.UI.Control.OnDataBinding(EventArgs e) +66
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +10
System.Web.UI.WebControls.BaseDataList.DataBind() +23
Trainee.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\Trainee\DataGridTest.aspx.vb:51
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750

I can browse the connection's tables fine in Server Explorer. Any ideas? Thanks.

by Gary on Tuesday, May 2nd 2006 at 10:18 AM

The following lines didn't show up in my previous post so I am trying to repost them with the beginning and ending "<>" removed:

Line 12: form id="Form1" method="post" runat="server"
Line 13: asp:DataGrid id=DataGrid2 style="Z-INDEX: 101; LEFT: 80px; POSITION: absolute; TOP: 32px" runat="server" DataSource="<%# OleDbConnection1 %>"
Line 14: /asp:DataGrid></form

by Nabeel Akhtar on Tuesday, May 2nd 2006 at 10:44 AM

Gary, I see that you have a grid by the name of DataGrid2 in your page... but in the code you are using DataGrid1.DataSource .... Instead of this, try using
DataGrid2.DataSource......

by Gary on Tuesday, May 2nd 2006 at 12:28 PM

Nabeel,

Sorry for the confusion. My Grid is actually called DataGrid2 and all the error messages above should have reflected that. I had tried to delete the grid and recreate it and I mixed up the two error messages. The result was the same in either case.

Thanks so much for your time.

by Gary on Wednesday, May 3rd 2006 at 04:11 PM

Got it!

I watched the Video Tutorial and started over from scratch. It worked perfectly. The only thing that I think I did differently from before was to select "Don't Include Password" when I was setting up the Connection String for OleDbConnection1. This was clear in the Video but I didn't see it above in the screen shots.

Thanks much for this great site!

by Zach on Tuesday, May 9th 2006 at 04:47 PM

I am trying to connect to a database on a remote server. The entire web application is being hosted on the remote server. When I try to use the jet OLEDB 4.0 driver I am unable to specify the server and I cannot browse for the files on the server. If I point it to the connection in "My Network Places" the link references something in memory. I've tried typing in C:\Inetpub\faithjanesville\httpdocs\webapplication2\Database.mdb <the path for the file, but it still does not recognize it. Do I need to use a map path? I really am lost and have read more than one book and countless internet sites and am unable to figure it out. Any help would be apprecaited. Thanks

by Zach on Tuesday, May 9th 2006 at 04:48 PM

but it still does not recognize it. Do I need to use a map path? I really am lost and have read more than one book and countless internet sites and am unable to figure it out. Any help would be apprecaited. Thanks (It cut me off)

by Nabeel Akhtar on Wednesday, May 10th 2006 at 02:42 PM

If you have access to the webserver, map the remote database as a Drive letter on your computer:
Right Click My Computer > Map Network Drives... and then give it a shot... make sure you have proper permissions....

by Zach on Friday, May 12th 2006 at 05:58 PM

I tried to Map the network drive, but I was unable to. I do not have access to the server. The only way I can access it is an ftp or http protocol or through the web inteface control panel. I appreciate the help, but it seems I'm stuck. I'll keep looking. If you have any other ideas I'd be glad to hear them. Thanks guys.
Zach

by braich on Friday, July 21st 2006 at 12:13 PM

i tried this but i keep getting this error message:
System.Data.OleDb.OleDbException: The Microsoft Jet database engine cannot open the file 'C:\Inetpub\wwwroot\trial.mdb'. It is already opened exclusively by another user, or you need permission to view its data.

By chris veale:
There is a function you can use to connect to a database of the form....

Dim myPath As String = Server.MapPath("mydb.mdb")

and then in the connection string set the datasource to "myPath"

i tried this but it could not be done

by Prasant on Wednesday, August 16th 2006 at 01:21 AM

Hi, is any one have any idea to connect to access data base in the web server can be connected using windows application.

actually i want to connect to the data base , that is in a web server using the desk top application.

by Elle on Tuesday, August 22nd 2006 at 01:59 AM

This is a great tutorial. I have a minor problem. Below is the error returned.

Server Error in \\\'/WebApplication1\\\' Application.
--------------------------------------------------------------------------------

The Microsoft Jet database engine cannot open the file \\\'C:\\\\Inetpub\\\\wwwroot\\\\STOCKDATA.mdb\\\'. It is already opened exclusively by another user, or you need permission to view its data.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine cannot open the file \\\'C:\\\\Inetpub\\\\wwwroot\\\\STOCKDATA.mdb\\\'. It is already opened exclusively by another user, or you need permission to view its data.

Source Error:


Line 44: \\\'Put user code to initialize the page here
Line 45: Dim myReader As System.Data.OleDb.OleDbDataReader
Line 46: OleDbConnection1.Open()
Line 47: myReader = OleDbCommand1.ExecuteReader()
Line 48: DataGrid1.DataSource() = myReader


Source File: C:\\\\Inetpub\\\\wwwroot\\\\WebApplication1\\\\WebForm1.aspx.vb Line: 46

Stack Trace:


[OleDbException (0x80004005): The Microsoft Jet database engine cannot open the file \\\'C:\\\\Inetpub\\\\wwwroot\\\\STOCKDATA.mdb\\\'. It is already opened exclusively by another user, or you need permission to view its data.]
System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr)
System.Data.OleDb.OleDbConnection.InitializeProvider()
System.Data.OleDb.OleDbConnection.Open()
WebApplication1.WebForm1.Page_Load(Object sender, EventArgs e) in C:\\\\Inetpub\\\\wwwroot\\\\WebApplication1\\\\WebForm1.aspx.vb:46
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032

9

by mani on Sunday, September 24th 2006 at 01:23 PM

I am getting error meessage:
The Microsoft Jet database engine cannot open the file \'C:\\Documents and Settings\\Sabree\\Desktop\\db1.mdb\'. It is already opened exclusively by another user, or you need permission to view its data.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine cannot open the file \'C:\\Documents and Settings\\Sabree\\Desktop\\db1.mdb\'. It is already opened exclusively by another user, or you need permission to view its data.

Source Error:


Line 26: // Put user code to initialize the page here
Line 27: OleDbDataReader myreader;
Line 28: oleDbConnection1.Open();
Line 29: myreader = oleDbCommand1.ExecuteReader();
Line 30: DataGrid1.DataSource = myreader;


Source File: c:\\inetpub\\wwwroot\\work\\webform1.aspx.cs Line: 28

Stack Trace:
can anyone help me with this why I am getting this error that its already open by another user.
Thank You,
Mani

by faisal on Wednesday, September 27th 2006 at 08:46 AM

your discription on how to connect database and web form is very helpfull apart i would like to know more on how to add update delete edit data when you have already connected the database and webfform p ls if there is an explanation or tutorial couse it giving me a big problem

by fei on Wednesday, September 27th 2006 at 08:54 AM

hey
this tutorial has really helped me in my work, i would like to know more on how to insert data up date data delete data and view data when the database and web form is connected how can i add delete or update data using my web form to database

by NK on Friday, October 6th 2006 at 01:07 PM

It looks great!
1) I have the same question as faisal does (i would like to know more on how to add, update, delete, edit data when you have already connected the database and webform).
2) Please let me know what a program(s) you used to create so great Video Tutorial.

Thank you very much for your great help

by Andrew on Wednesday, November 29th 2006 at 06:40 AM

Add me to the list of people who want to know how to update/add data. LOL. just creating a new OLEDBCOMMAND doesn't seem to work for me. Even after using ExecuteNonQuery. I get an error saying :

"Operation must use an updateable query.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Operation must use an updateable query."

by Andres on Wednesday, December 27th 2006 at 04:38 PM

Is it similar for forms and windows app? For some reason it doesn\'t show the DataGrid1.DataBind() as valid.

by velu on Tuesday, January 16th 2007 at 04:26 AM

how to connect the ms access database to C# dot net and coding for update, delete, save

by fuck on Wednesday, January 17th 2007 at 04:33 AM

fuck

by CooP on Thursday, January 18th 2007 at 04:15 AM

"Operation must use an updateable query" usually means that the folder containing the database does not have the correct 'write' permissions on it to allow the script to update, despite various webmasters insisting that it doesn't.

by Nishan on Sunday, March 18th 2007 at 10:23 AM

hi,
what if the 'db1.mdb' file is on the internet. i mean for example the location is "http://www.tutorialized.com/db1.mdb" then is it suppose to work? I mean i tried and the web page doesn't show the data fields. i see only the text. what could be the problem?

by BF on Monday, April 9th 2007 at 11:10 PM

Hey this was a very helpful article, but do you have a way to do this in C#? And is there a way to show only an individual record? For example, I only want to show a particular field of a specified record in one part of the webpage (like a content field) and show another field somewhere else (like the title field).

Thanks!

by waleed on Monday, April 23rd 2007 at 02:04 AM

Server Error in '/db20' Application.
--------------------------------------------------------------------------------

The Microsoft Jet database engine cannot open the file 'C:\Documents and Settings\Administrator.SHJMOH\Desktop\Northwind.mdb'. It is already opened exclusively by another user, or you need permission to view its data.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine cannot open the file 'C:\Documents and Settings\Administrator.SHJMOH\Desktop\Northwind.mdb'. It is already opened exclusively by another user, or you need permission to view its data.

Source Error:


Line 42: Dim myReader As System.Data.OleDb.OleDbDataReader
Line 43:
Line 44: Me.OleDbConnection1.Open()
Line 45: myReader = OleDbCommand1.ExecuteReader()
Line 46: DataGrid1.DataSource = myReader


Source File: c:\inetpub\wwwroot\db20\WebForm1.aspx.vb Line: 44

Stack Trace:


[OleDbException (0x80004005): The Microsoft Jet database engine cannot open the file 'C:\Documents and Settings\Administrator.SHJMOH\Desktop\Northwind.mdb'. It is already opened exclusively by another user, or you need permission to view its data.]
System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr)
System.Data.OleDb.OleDbConnection.InitializeProvider()
System.Data.OleDb.OleDbConnection.Open()
db20.WebForm1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\db20\WebForm1.aspx.vb:44
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()




--------------------------------------------------------------------------------

by raj on Friday, June 8th 2007 at 07:56 AM

hi,
i have done one visual basic project using MSACCESS as backend. Now i want to put my exe file in two different systems and to use same database for both via networking. is it possible, then please help me to do that

by sabita on Monday, June 11th 2007 at 04:02 AM

hi i need a help on database connectivity.
can you please help me with the connectivity of vb.net and Access.I am constantly getting error messages regarding the same.Please help me on that.

Thanks
sabita

by luke on Friday, June 22nd 2007 at 03:49 PM

Discover an hidden menu, right click a magic button ...
But we are programmer, not wizards.

Is not possible to write CODE to connect to a database, as in a normal programming language? I write 10 lines in a minute, but I may need 2 days to discover a magic button

by ankush on Thursday, July 5th 2007 at 04:30 AM

i want to connect access to my project using c#....
can u plztell me code for that.....
i got error in code only...else evry thing is working.......
i jst reqd code 4c#...
waitin 4 ur rply

by Saeed on Tuesday, October 16th 2007 at 12:47 AM

Hi every Body
plz help to
showed how it is possible to connect a Flash movie to an Access database
Thanks.

by Vusisizwe Justice Sibanda on Tuesday, May 27th 2008 at 10:49 AM

I Need to know how to connect to the access database and pass data from the text boxes to the recordset and save it without using the ADO control in Visual C

by Vusisizwe Justice Sibanda on Tuesday, May 27th 2008 at 11:00 AM

I Need to know how to connect to the access database and pass data from the text boxes to the recordset and save it without using the ADO control in Visual C

by antony on Saturday, June 28th 2008 at 06:45 AM

hi i am getting an error message when trying to open the connection . the error message is" could not lock the file" i tried it for c# please do reply i am very eager to know about it. thanks

by mug on Tuesday, September 30th 2008 at 03:17 PM

i cant view the
video or the screen shots!!!!!!

by sanjay on Tuesday, November 11th 2008 at 11:37 PM

hi i need a help on database connectivity.
can you please help me with the connectivity of asp.net and Access. give me process through maps.where iam write the data base code in asp.net.please give me total process.
Please help me on that.

thankyou for u r suggtions.

by ryan on Tuesday, December 2nd 2008 at 09:38 PM

This is very good!

I’m new in programming and this tutorial helps me a lot!
Please create tutorial on how to add, edit and delete using this tutorial you created.

Thanks!


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP.NET Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons