Hilal Al-Rajhi Hilal Al-Rajhi - 1 year ago 101
Vb.net Question

Vb.Net Add table to dataset and send it to an other form

I have an application that loads cars data from sql server database as follows.

sql = " select car.*, (model + ' -- ' + plateNo) as selectCar from car"
daAdapter = New SqlDataAdapter(sql, sqlConn)
daAdapter.Fill(dsDataset, "car")

So data would look like the following:

enter image description here

Then instead of loading distinct values of brand from database I have created one datatable to get the distinct values of the brand. And then add it to the dataset as follows:

tblBrand = New DataTabl
tblBrand = dsDataset.Tables("car").DefaultView.ToTable(True, "brand")
tblBrand.TableName = "brand"

Actually I use this brand table to display the filter options in the brand combobx without any problem.

What I need is that when I press the add button I go to an other form and I want to use this brand options in the other form. So when I click the add button I have the following code:

Dim newCar As New frmCar(dsDataset)
carMode = "new"

And I created a constructor in the frmCar form as follows:

Sub New(ByVal dsOptions)

cmbBrand.DataSource = dsOptions.table("brand")
cmbBrand.DisplayMember = "brand"
cmbBrand.ValueMember = "brand"
Catch ex As Exception
End Try
End Sub

But the following error pops up:

Public member 'table' on type 'DataSet' not found.

What is wrong with my code?

Answer Source

The first thing you should do is to turn both option strict and explicit On. This will prevent you from doing these kinds of mistakes in the future.

The error is quite self-explainable. A DataSet doesn't have a public member (property, method etc..) named table.

All you have to do is change:

Sub New(ByVal dsOptions)
    cmbBrand.DataSource = dsOptions.table("brand")


Sub New(ByVal dsOptions As DataSet)
    cmbBrand.DataSource = dsOptions.Tables("brand")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download