DataGrid Control

G

Guest

Im just toying with vb.net and am trying to find out howw things work. i
have managed to come up with the following code that will access a table in a
sql2005 server . what id like to do is populate 'DataGrid1' with the results
can anybody explain how this is done

STR_SQLQuery = "select LICENCE, COMPANYNAME from
DevSys.dbo.clientAdmin "
STR_SQLCOMMAND.CommandText = STR_SQLQuery
STR_SQLCOMMAND.Connection = CON_BOSSCONNECTION
STR_SQLREADER = STR_SQLCOMMAND.ExecuteReader
Try
While STR_SQLREADER.Read()
Console.WriteLine(STR_SQLREADER.GetString(0) & " " &
STR_SQLREADER.GetString(1))
End While
Finally
STR_SQLREADER.Close()
End Try
 
G

Guest

Peter,

You need to look into the DataAdapter, DataSet and DataTable classes.

Use a dataadapter to fill a dataset's datatable with the results of your sql
select query.

Then assign the dataset's datatable to the datagrid's DataSource property.

Kerry Moorman
 
G

Guest

Kerry,

I had a good look through the thread and came up with this funcction

Public Function SelectRows(ByVal dataSet As DataSet, ByVal queryString As
String) As DataSet
Dim adapter As New SqlClient.SqlDataAdapter
adapter.SelectCommand = New SqlClient.SqlCommand(queryString,
CON_BOSSCONNECTION)
adapter.Fill(dataSet)
Return dataSet
End Function

however it blows out at adapter.Fill(dataSet) with the following error

An unhandled exception of type 'System.ArgumentNullException' occurred in
system.data.dll

Additional information: Value cannot be null.

im calling it by using the following
dim REUSE_DATASET As DataSet

SelectRows(REUSE_DATASET, STR_SQLQuery)

this is all new to me and im totally confused now
 
G

Guest

Peter,

dim REUSE_DATASET As NEW DataSet

You might also consider:

Public Function SelectRows(ByVal queryString As
String) As DataSet
Dim dataSet as New DataSet
Dim adapter As New SqlClient.SqlDataAdapter
adapter.SelectCommand = New SqlClient.SqlCommand(queryString,
CON_BOSSCONNECTION)
adapter.Fill(dataSet)
Return dataSet
End Function

Kerry Moorman
 
C

Cor Ligthert [MVP]

Peter,

Are you using VB2003 with WebForm or Winform or even something else?.

In a VB2003 WebForm you can bind a datareader to a DataGrid what is not
possible in a Winform where you have to follow the way as Kerry has
described.

Just as little addition,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top