Wednesday, March 21, 2012

Populating a Dataset

I've created a vb windows application and written the following function within a class (class1). I dragged a dataset onto my form and renamed it datBookInfo but when I run the code I don't get anything in the form.

Please tell me what I'm doing wrong.


Public Overloads Function GetBookInfo() As DataSet
Dim oPubConnection As SqlConnection
Dim SConnString As String
Dim osqlCommPubs As SqlCommand
Dim osqlCommTitles As SqlCommand
Dim oDataAdapterPubs As SqlDataAdapter
Dim oDataAdapterTitles As SqlDataAdapter

Try
SConnString = "Data Source=(local);Initial Catalog=Pubs;" & _
"User ID=sa;Password=;"
oPubConnection = New SqlConnection(SConnString)
oPubConnection.Open()

'Create command to retrieve pub info
osqlCommPubs = New SqlCommand
osqlCommPubs.Connection = oPubConnection
osqlCommPubs.CommandText = "Select Pub_ID, Pub_Name from Publishers"

'Create Data Adapter for pub info
oDataAdapterPubs = New SqlDataAdapter
oDataAdapterPubs.SelectCommand = osqlCommPubs

'Create command to retrieve title info
osqlCommTitles = New SqlCommand
osqlCommTitles.Connection = oPubConnection
osqlCommPubs.CommandText = "select Pub_ID, title, price, ytd_sales from titles"

'Create data adapter for title info
oDataAdapterTitles = New SqlDataAdapter
oDataAdapterTitles.SelectCommand = osqlCommTitles

'Create and fill a data set
Dim datBookInfo As DataSet = New DataSet
oDataAdapterPubs.Fill(datBookInfo, "Publishers")
oDataAdapterTitles.Fill(datBookInfo, "Titles")
Return datBookInfo

Catch ex As Exception
Finally
oPubConnection.Close()

End Try

End Function

check that if the dataset is populated when there is no PostBack.


If(!Page.IsPostBack)
{
LoadData();
}

Hmm, dont see anything obvious...How are you doing your bind of the dataset to the grid?

Also, since this is a windows project and not a web project (assuming your "I've created a vb windows application" is correct) there will NOT be a page.isPostBack...

Thanks,
MajorCats

0 comments:

Post a Comment