Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  ADO.NET for Beginners Part One  »  Retrieving Data from the database
 »  Home  »  Data Programming  »  ADO.NET  »  ADO.NET for Beginners Part One  »  Retrieving Data from the database
ADO.NET for Beginners Part One
by David Jeavons | Published  07/12/2006 | .NET Newbie ADO.NET | Rating:
Retrieving Data from the database

Now that we know what objects we will be using for this first part of the article, it is time to get stuck in and produce something a little more interesting.

Show me the Data!

Open the design of Form1 and double click in the form area to create the Form_Load event where we will place the code to retrieve all Contacts from our Address Book database. Now, before moving onto the actual code, we will add an "Imports" statement to the top of the code file in order to access the System.Data.OleDb objects without having to refer to the full namespace each time:

Imports System.Data.OleDb
 
So your code file should now look like the following screen shot:
 
 
Code file with Imports statement
 
By adding the above "Imports" statement, it allows us to write code such as "Dim connection As OleDbConnection". Without the above "Imports" statement we still have access to the OleDbConnection object, but in order to use it we would have to write code such as "Dim connection As System.Data.OleDb.OleDbConnection". You can think of the "Imports" statement as a shortcut to coding.
 
The next set of code to write will retrieve all data from the Contacts table within the database and show that data on the DataGrid. We will use the Form_Load event for this so that all data will be immediately visible when the program starts.
 
The first thing we need to do however is to create a Connection String for the database. A Connection String informs the OleDbConnection object what parameters to use when attempting to connect to the database. In it's most simplest form, the Connection String (for OleDb) will state the Provider (Microsoft Access, Microsoft Excel etc.) to use and the location of the data file.
 
The ConnectionString that we will be using looks like:
 
"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Application.StartupPath & "\AddressBook.mdb"
 
This specifies that we will be using the Microsoft.Jet.OleDb.4.0 provider and the location of the database is in the StartupPath of the application (should be your bin\Debug folder of your project) and the database is called AddressBook.mdb.
 
So putting this together with the code necessary to populate the DataGrid, we should have the following code in our Form_Load event:
 
 
Code to retrieve data and populate DataGrid
 
If you want to run this now then press F5 or select "Start Debugging" from the "Debug" menu. When the form loads you should see all entries from the Contacts table of the Address Book database loaded into the DataGrid.
 
Looking at the above code we can see that Line 7 declares a variable called "conn" which is declared as an OleDbConnection object. This declaration creates a new instance of the OleDbConnection object and makes use of its overloaded constructor to supply the Connection String. Any communication from our program to the database will occur through this connection object. 
 
Line 8 creates a new OleDbDataAdapter object. You can think of a DataAdapter as a bridge between the program and the database. Again, the OleDbDataAdapter has a number of overloaded constructors and in the above example, we are making use of one that allows us to specify a SELECT command and OleDbConnection object to use.
 
Note, in the above example we are selecting all fields from the Contacts table. However, it is considered (and rightly so) bad programming practice to make use of SELECT * queries. Usually, you would specify which fields to return from the database, such as SELECT ContactName, Telephone etc. but for the purpose of this tutorial, we will keep it simple. We will come back to the DataAdapter in a minute.
 
Line 9 creates a new DataTable variable. A DataTable is simply a collection of rows and fields and represents a database table in memory.
 
Line 11 makes use of the DataAdapter's Fill method to populate the DataTable with data retrieved from the database based on the SELECT statement used when constructing the DataAdapter. Quite a bit is actually happening at this point, namely:
 
  • A connection to the database is opened (based on the conn variable)
  • The DataTable is populated with data (based on the DataAdapter's SelectCommand Text). This includes both actual data and the table's schema (it's design definition).
  • The connection to the database is closed.

That's quite a lot of work for a single line call.

Finally, line 13 sets the DataSource property of the DataGrid to the DataTable. The DataGrid reads the schema information from the DataTable and uses the column names of the DataTable to create it's own columns and set the column heading text.  

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 07/12/2006)
Rating
Very nice. Looking forward to the rest.
 
Comment #2  (Posted by an unknown user on 08/07/2006)
Rating
What if my database has a password an not in a dbw group?
 
Comment #3  (Posted by David Jeavons on 08/08/2006)
Rating
If your database is password protected then you will need to modify your connection string to include the user credentials. The site below contains many examples of connection strings:

http://www.carlprothman.net/Default.aspx?tabid=86#OLEDBManagedProvider
 
Comment #4  (Posted by an unknown user on 08/30/2006)
Rating
Very good introduction.
How do I download the db and the sample code? I could not find any link.
 
Comment #5  (Posted by an unknown user on 08/30/2006)
Rating
Hi

Thank you for your comments.

You can download the database by going to page 4 of this article and at the bottom (just above the comments) you will see the AddressBook.zip attachment.
 
Comment #6  (Posted by an unknown user on 09/07/2006)
Rating
It didnt bloody work, did it
 
Comment #7  (Posted by David Jeavons on 09/07/2006)
Rating
Would you care to elaborate :)
 
Comment #8  (Posted by Jake on 09/08/2006)
Rating
will not compile

error = dgContacts

name dgContacts is not declares
 
Comment #9  (Posted by David Jeavons on 09/09/2006)
Rating
Hi Jake

When you placed the DataGrid or DataGridView onto the form, did you name it dgContacts?
 
Comment #10  (Posted by an unknown user on 09/09/2006)
Rating
it surely help the starting stage
 
Comment #11  (Posted by an unknown user on 09/12/2006)
Rating
Clear and Simple, very appropriate for beginner. Well Done!
 
Comment #12  (Posted by an unknown user on 09/20/2006)
Rating
David, vikram here

Simple, Consices and clear except for one thing man. You might wanna follow naming conventions since beginners tend follow the same names with their sample and from sample to their main projects. Other than its totally good article :)
 
Comment #13  (Posted by an unknown user on 10/08/2006)
Rating
David:

I have 6 books opened and lying all around me, and even though I've been programming access vba using ado for over 3 years, I couldn't find the necessary details to make the jump to ado .net.

Using the example that you provided, I connected to a database on sql server 2005 and got the datagrid to work when all of the code from the books I've been reading were generating errors or were exceptionally unclear.

You're doing a great job so far...

Please keep it up!

Thank You
Harold Morgan
Memphis, TN
 
Comment #14  (Posted by diallo on 10/20/2006)
Rating
please ,help me to have the solution of the exercice to "play cancel"
 
Comment #15  (Posted by an unknown user on 10/29/2006)
Rating
good job
 
Comment #16  (Posted by an unknown user on 11/13/2006)
Rating
Excellent for Newbie's
It's easyer to understand then "How do I" from Microsoft.With all their inbuild links.

Thanks a lot.

Henk, Netherlands
 
Comment #17  (Posted by an unknown user on 01/07/2007)
Rating
Hi! It will surely help the starting stage. Good Job!!

 
Comment #18  (Posted by an unknown user on 01/30/2007)
Rating
This is the clearest most simple yet educational example of how to connect to a database, you should write a book....
 
Comment #19  (Posted by an unknown user on 02/06/2007)
Rating
the article is easy to understand.
 
Comment #20  (Posted by M Ayaz on 02/07/2007)
Rating
I am using vb.net2005 and Office Xp. I downloaded the acces database that u provided and converted it to access 2002 format. I have followed the instructions in your tutorial to pin accuracy. When I press F5 an exception is thrown " Could not find installable ISAM". Can u please tell me what is wrong here?
 
Comment #21  (Posted by an unknown user on 03/01/2007)
Rating
very well
 
Comment #22  (Posted by an unknown user on 03/05/2007)
Rating
Great newbie stuff. I did this exercise using VC# 2005. But instead I used the dataview object. Which provokes me to ask: what is the difference between dataview and datagrid?
 
Comment #23  (Posted by David Jeavons on 03/05/2007)
Rating
Hi, thanks for your comment.

By DataView I assume you mean the new DataGridView control, this is an updated version of the DataGrid control for the .NET 2.0 Framework and provides more functionality than that found in the original DataGrid.

The reason I chose to use the DataGrid in this tutorial is that both users of .NET 1.1 and .NET 2.0 Frameworks would have access to this control, but of course, if you are using .NET 2.0 then there are not many reasons to reverting to the older DataGrid.
 
Comment #24  (Posted by an unknown user on 03/06/2007)
Rating
For a complete beginner, nice and simple to understand. Explained in easy to understand english.
 
Comment #25  (Posted by an unknown user on 03/08/2007)
Rating
It's a great introduction to database interfacing for beginners with minimal to zero knowledge in VB.NET programming.
 
Comment #26  (Posted by an unknown user on 03/27/2007)
Rating
This article has thorough explanations which made me feel comfortable with what I was doing. I am new to ADO.NET and I learned a lot from this article.
 
Comment #27  (Posted by an unknown user on 04/10/2007)
Rating
Is Part 2 out yet? Thanks. Worked Great. LTroxell
 
Comment #28  (Posted by Ronald on 04/17/2007)
Rating
Sorry, just wanted to follow on your tutorial but could not locate the example database you mentioned. can you help me with that anyone who found it?
many thanks.
 
Comment #29  (Posted by David Jeavons on 04/18/2007)
Rating
Hi Ronald

The database zip file can be found at the end of the article on page four.

Thanks
 
Comment #30  (Posted by an unknown user on 04/18/2007)
Rating
well if there is one thing i can say thats would be "you know what you are talking about"
.::Thanks::.
 
Comment #31  (Posted by an unknown user on 04/26/2007)
Rating
Very simple explanations.Keep it up.....So nice
 
Comment #32  (Posted by an unknown user on 05/09/2007)
Rating
I get an exception error at :

adapter.Fill(dt) - OleDb Exception "Not a valid file name"
 
Comment #33  (Posted by Steve on 05/10/2007)
Rating
I have resolved the above exception error. ( I had made a mod in the conn stmt to the path - oops)

The level of this tutorial fits with where I am perfectly.

Very Nice job
 
Comment #34  (Posted by an unknown user on 05/14/2007)
Rating
Thanks for great article.
 
Comment #35  (Posted by an unknown user on 06/08/2007)
Rating
Finally! Something made simple and methodical for a newbie to understand!
 
Comment #36  (Posted by an unknown user on 06/14/2007)
Rating
Easy and short.Thanks.....!
 
Comment #37  (Posted by an unknown user on 07/03/2007)
Rating
David is an excellent writer and instructor. If he hasn't written any books yet, he definetly should.
 
Comment #38  (Posted by an unknown user on 07/31/2007)
Rating
Clearly understandable..
Follow the rules and it will work for sure!!!
Thanks
 
Comment #39  (Posted by an unknown user on 08/23/2007)
Rating
its preety good for beginers like us but explain y v can't give ne name to r database
 
Comment #40  (Posted by an unknown user on 09/14/2007)
Rating
Very nice article for newbees

 
Comment #41  (Posted by an unknown user on 09/28/2007)
Rating
Clear, consise and great explanations
 
Comment #42  (Posted by TC on 09/30/2007)
Rating
Very good article.
Very good writting skills, clean and to the point
 
Comment #43  (Posted by bros_cchkout on 10/04/2007)
Rating
Good
 
Comment #44  (Posted by an unknown user on 10/05/2007)
Rating