Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fill DataSet in Class, bind in Form

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
0
0
US
Hello
I would like to build a connection class and fill my data sets in the same class, then I would like to bind the datagrid in the form code. What I have does not work I get an error, stated below.

Here is my code:

Class db06302004
Dim objConn As SqlConnection = New SqlConnection("server=ereplandemoas3;database=FacetsSS;user id=FacetsSS;password=FacetsSS")
Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter
Public objDataSet As DataSet = New DataSet

Private Sub ConnOpen()
objConn.Open()
End Sub
Private Sub ConnClose()
objConn.Close()
End Sub

Public Sub FillDataSet()
Dim objDataAdapter = New SqlDataAdapter
objDataAdapter.SelectCommand = New SqlCommand

With objDataAdapter.SelectCommand
.CommandText = "Select * From FSS_USERS"
.Connection = objConn
.CommandType = CommandType.Text
End With

ConnOpen()

Dim objDataSet As DataSet = New DataSet
objDataAdapter.Fill(objDataSet, "Users")
ConnClose()
End Sub
End Class

I have this in the forms load event:

Dim Connection As New db06302004
Call Connection.FillDataSet()
DataGrid1.DataSource = Connection.objDataSet
DataGrid1.DataMember = "Users" ' I get this error " Cannot create a child list for field Users."
TextBox1.Text = Connection.objDataSet.GetXml

What am I doing wrong?
 
I'm pretty sure that you are going to need to define a schema for your Data Table here:

Code:
objDataAdapter.Fill(objDataSet, "Users")

You might have seen this slightly misleading example:

Code:
Dim SelectQuery As String = "SELECT * FROM Customers"
Dim adapter As New SqlClient.SqlDataAdapter(SelectQuery, dcNorthwind)
Dim ds As New DataSet("Northwind")
[highlight]adapter.Fill(DsNorthwind1,"Customers")[/highlight]

This MSDN example warns us that:

This example requires:
...A dataset named DSNorthwind1 [highlight]containing a data table named Customers.[/highlight]

So the table needs to be pre-defined some kind of way. Unfortunately, I am hooked on typed data sets on one hand and the [tt]SqlDataReader[/tt] on the other---but this article may really help a little: Creating a DataTable with Columns and a Primary Key (Visual Basic).

Bryan Wilhite
Songhay System
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top