Filling a Datagrid

hobbes2103

Active member
Joined
Jul 10, 2003
Messages
43
Hello!

I have this code :

Dim myConnection As New OleDb.OleDbConnection
Dim myCommand As New OleDb.OleDbCommand
Dim myReader As OleDb.OleDbDataReader
Dim myConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Database.mdb"

myConnection = New OleDb.OleDbConnection(myConnectionString)
myConnection.Open()

Dim mySelect As String = "SELECT name, birthdate FROM MyTable"
myCommand = New Oledb.OleDbCommand(mySelect, myConnection)
myReader = myCommand.ExecuteReader()
End Sub

I want my datagrid to show the names and birthdates when I press on button1. How do I do that?
(as you have already guessed, Im a beginner so if you know the easiest way...) :confused:
 
do you have a data adapter created, either do it at design time or programmatically. Either way, you need to create a data adapter, then from that generate a dataset. Bind the dataset to the datagrid.

Try this:
dim mydataset as new dataset
oledbdataadpater1.fill(mydataset)
datagrid.setdatabinding(mydataset, "MyTable")

Hope this helps.
 
create oledbdataadapter as

Dim oledb_adap ad OleDb.OleDbDataAdapter
oledb_adap.selectcommand.commandtext="Select name, birthdate FROM MyTable"

Dim data_set as New DataSet()

oledb_adap.fill(data_set)
datagrid1.setdatabinding(data_set,"mytable")
 
Rufus Ive pasted your code and there seem to be a problem with the line :

oledb_adap.selectcommand.commandtext="Select name, birthdate FROM MyTable"

Ive never created dataAdapter or dataSet before, so maybe that youve omitted a line that seems obvious to you... but not to me!
 
Well, Ive tried this code :

Dim myConnection As New OleDb.OleDbConnection
Dim myCommand As New OleDb.OleDbCommand
Dim myConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Database.mdb"

myConnection = New OleDb.OleDbConnection(myConnectionString)
myConnection.Open()

Dim mySelect As String = "SELECT name, birthdate FROM MyTable"
myCommand = New OleDb.OleDbCommand(mySelect, myConnection)

Dim oledb_adap As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
oledb_adap.SelectCommand = myCommand

Dim data_set As DataSet = New DataSet
oledb_adap.Fill(data_set)
DataGrid1.SetDataBinding(data_set, "MyTable")

It works...except for the last line, the datagrid filling, which is where an error occurs...
There is an "unhandled exception",
The message says (Im translating from french so i dont know if it is right) "Impossible to create a child list (?) for the field MyTable"

Please help!!
 
Back
Top