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.
ADO.NET and Access database IIHow to connect to an Access database from Visual C# .NET, retrieve data and display it in a listBox with ADO .NET, using a dataSet. |
On Monday, June 14th 2004 at 07:22 AM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 4.3 with 27 votes) |
||||
|
The first part of the tutorial is similar to the one in the tutorial named 'Connecting to an Access database', although it's recommended to read that tutorial first as it is a bit more simple. First download the database we will use in this example and save it somewhere: Start a new 'Windows Application' project named 'accessDB2'. From the View menu of Microsoft Visual C# .NET select Server Explorer, or press Ctrl+Alt+S. From the list right-click Data Connections and choose 'Add Connection...'. In the 'Provider' tab select Microsoft Jet 4.0 OLE DB Provider (used for connecting to an Access database), and click Next. Use the '...' button to browse for an Access Database and choose the database you have downloaded, db1.mdb. After clicking OK, test the connection by clicking the 'Test Connection' button. It should say 'Test connection succeeded.'. Press OK and a window pops up that says 'Please Enter MS JET OLE DB Initialization Information'. Leave the defaults and just press OK. ![]() Just like you see in the above screenshot, you can browse the Access file database. Yet the database is not connected to our program. For this you need to drag the node below 'Data Connections' on the form. The node is named using the form 'ACCESS.X:\PathToYourDatabase\file.mdb.Admin'. As I said, drag it on the form and 'oleDbConnection1' should appear below the form: Next open the Toolbox (Ctrl+Alt+X) and from the 'Data' group drag an 'OleDbDataAdapter'. The 'Data Adapter Configuration Wizard' starts. Clicking next will take you to the part where you need to select the connection you wish to use. Choose the connection we have just created (ends up in db1.mdb.Admin). Press Next and then again Next (leave the default 'Use SQL statements'). Now you are beeing asked 'What data should the data adapter load in the dataset?'. We want all the tables and all the columns therefore we need to take the following steps. Open the Query Builder using the button and you should now be able to add the two tables named 'books' and 'categs'. Add them and close the small window and now we have two small windows representing the two tables. We want to select all the columns, therefore check '* (All Columns)' on both tables. The following SQL query is created: SELECT books.*, categs.* FROM (books INNER JOIN categs ON books.ID = categs.ID) We could also do it more simple by typing 'SELECT * FROM books, categs'... it would have been the same. Press OK to exit the Query Builder and then press Finish. Now add a DataSet to our application by dragging one from the Data group (Toolbox). Choose 'Untyped DataSet (no schema)'. DataSets are used to store the query results with which we later populate the DataGrid. The main reason of this tutorial is to demonstrate how to add the values in a Access database in a listBox. Therefore create a new listBox (listBox1). Also create a button named 'btnUpdate' and with the caption 'Update'. Double click it to get to the btnUpdate_Click event. First use the following code:
The code is explained in this tutorial. Now we create a DataTable named dTable and assign it the collection of data stored by dataSet1:
This is the way you should do it, although the second method isn't that bad data binding is specially built for this therefore you should use it:
As you can see, we just set a few properties and we are done. Method 2 - doing it manuallyThe second method is by populating the listBox1 by looping through the DataTable:
At dTable.Rows[x][2].ToString() x is the row number and 2 is the column number. x increases every loop because we want to loop through the whole table. 2 represents the second column which in our database is 'bookName', the column that stores the name of the book (first column is the one that stores the ID). Either method you choose, this is the result: |
|||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||||
|
|||||
Current CommentsHi, thank you very much for all the freeware tutorials. I just start working with C# and I found them very useful. For the tutorial 62 (ADO .Net and Access databases), it would be good if you would show multple fields from a table as well, or to provide a link to another article which shows how to do that.
Regards,
Nicolae
Good tut! Just perfect for helping me with the basics while converting from Borland to .NET.
Thanks!
i want more articles about GDI+ using vb.net
i'm in 3rd computer science from egypt
my doctor of AI wants aproject has graphic and animation.
please:
send me several articles about my reuirement
www.powerearth.4t.com
Thank u.
Brilliant, this is simple and yet very educational. you must start with the basis and then build on that not put a entire complex program on display and expect novices to understand it well done
I like this tutorial, because it is not some click-cut-paste machinery, but an good and easy intoduction!
I want to have save,delete,next,previous,last,first button in my form with data access . If it possible , give a example with its code . Thanks
Really useful!!
This was indeed very useful. I think you can add tutorials on how to add records to the existing database.
Great tut ! Quick and easy, I spent $103 on the book that was not NEARLY as helpfull as this tutorial ! Thanks a lot !!!
great one!really enjoyes doin it
JUST AMAZING!!!!
IT WAS SO USEFULL TO ME
THKS
Hey i'm new to all of this but just started programming with C#....i'm having trouble clicking and dragging the node onto the form....anyone know why?? Thanks
nice tutorial
thank's alot!!
hi this is the method for binding data from Ms Access database in listbox so good i f want take a new question then how to find Answer. please send me a link where i put my ques.
yujyujtyuyikuyiouyi
the oleDBbDataAdapter is not working giving me an error when i am trying to add the server name
---------------------------
Microsoft SQL Server Login
---------------------------
Connection failed:
SQLState: '08001'
SQL Server Error: 53
Named Pipes Provider: Could not open a connection to SQL Server [53].
Connection failed:
SQLState: '08001'
SQL Server Error: 53
Connection failed:
SQLState: 'HYT00'
SQL Server Error: 0
---------------------------
OK
---------------------------
how can i solve it?
Hi,thank you very much you've been a great help with you're quite fulfilling informations.
THX again
thanks
Thank you :)
Related Tutorials
Related Source Code
C# Job Search