A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Connect to Access Database in Visual Studio .NETThis 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 47 votes) |
||
|
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 It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsExcellent ! . i need more briefly how i'll connect vb & Access database
Munir, did you watch the video tutorial? I don't think it too long.
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.
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.
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.
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
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?
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...
Your tutorial is very helpful, have you got a tutorial on being able to write to the database too?
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.
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?
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
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
Very explantory article. Helped me to get the right connection within no time.
Keep up the good work ...
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.
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
Hi Chris,
Thanks for your reply, im kind of new at this, but what do you mean when you say mappath function?
thankyou
sk
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.
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
Its really helped me to connect database.
Thanks a lot!
Can i connect to a ms access database present in a different computer
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.
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.
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
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......
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.
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!
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
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)
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....
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
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
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.
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
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
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
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
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
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."
Is it similar for forms and windows app? For some reason it doesn\'t show the DataGrid1.DataBind() as valid.
how to connect the ms access database to C# dot net and coding for update, delete, save
fuck
"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.
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?
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!
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()
--------------------------------------------------------------------------------
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
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
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
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
Hi every Body
plz help to
showed how it is possible to connect a Flash movie to an Access database
Thanks.
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
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
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
i cant view the
video or the screen shots!!!!!!
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.
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!
I am very thankful for the above thing.
I am very thankful for the above thing.
hi,
this tutorial is really very much useful to me while done my mini project.
i really want to thank this author.....
hi!
its realy nice.. but i am new to .net. is it possible to do the same work in vb .net using c#. i am using mysql(odbc) connection.please any one help me...its urgent..rajaece2006@gmail.com..
this tutorial is really very much useful to me while done my mini project.
thanxs, this will help me in my Final year project at University.
Very usefull help.
Thanking you
Any one know how i can access my ms-access in ftp
from my desktop
Sir:
Ita Very Usefull to me, Thanku 4 ur Service, Leep it Up, If u can Sent me the sent me the Access 2007 and VB Application Database Conection Menual , I have Many daubt in this Chapter, \
Than u
Barath
Hello,
The pictures do not show in my brownsers. I have both "firefox" and "Exp". It there something that I need to do to see it because I really need the code shown for the DataGrid.
Thanks
Set your life easier get the <a href="http://bestfinance-blog.com">loan</a> and everything you want.
Related Tutorials
Related Source Code
ASP.NET Job Search