Viewing data problem

richarddenton

Member
Joined
Jul 31, 2006
Messages
5
Programming Experience
Beginner
Hi,

I have set up my connection to an Access Database as per the code below (which I am pretty sure is fine as it is just copied from MSDN) but when I run the form it doesn't display the data. Am I missing out something obvious? At present I have nothing else on my form. Thanks in advance.

Dim DS As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection

MyConnection =
New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Student Database.MDB")
MyCommand =
New System.Data.OleDb.OleDbDataAdapter( _
"select * from tblResults", MyConnection)
DS =
New System.Data.DataSet()
MyCommand.Fill(DS)
grdResults.DataSource = DS
MyConnection.Close()


 
Yup, your missing the datamember property. You've bound the grid to the dataset but now you need to tell it where to get it's information from within the dataset.....

grdResults.DataMember = "tblResults"


Also the dataadapter handles all the opening and closing of the connections for you so yuo don't need to explicitly call connection.close.
 
I tried that but I got the error "Child list for field tblResults cannot be created". What does this mean? As you can see I'm quite new to VB but I've been trying to sort this data access for weeks now and I'll be so happy if I can get it! :)
 
Oh i get it... I though it was a bit strange when you were getting that error. Been staring at that code for about five minutes now!! Because you haven't explicitly created a datatable to hold the data it is using a default name probably something like NewDataTable or something.

Change your code to this...

VB.NET:
Dim DS As System.Data.DataTable
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection

MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Student Database.MDB")
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from tblResults", MyConnection)
DS = New System.Data.DataTable
MyCommand.Fill(DS)
grdResults.DataSource = DS
MyConnection.Close()

As a side note i was sure that you wouldn't have to use the datamember property anyway as you only would've had one table in your dataset. If you are only using one table in your app then i would suggest sticking to the datatable for now. You only need the dataset if you are going to be doing things will data relations and other multiple table stuff.
 
VB.NET:
Dim DS As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection

MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Student Database.MDB")
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from tblResults", MyConnection)
DS = New System.Data.DataSet()
MyCommand.Fill(DS, "tblResults") 'Change this line
grdResults.DataSource = DS
grdResults.DataMember = "tblResults" 'And add this line.
MyConnection.Close()

-tg
 
Back
Top