Hilal Al-Rajhi Hilal Al-Rajhi - 7 days ago 8
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"
dsDataset.Tables.Add(tblBrand)


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"
newCar.ShowDialog()


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

Sub New(ByVal dsOptions)
InitializeComponent()

Try
cmbBrand.DataSource = dsOptions.table("brand")
cmbBrand.DisplayMember = "brand"
cmbBrand.ValueMember = "brand"
Catch ex As Exception
MsgBox(Err.Description)
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

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")

to:

Sub New(ByVal dsOptions As DataSet)
    cmbBrand.DataSource = dsOptions.Tables("brand")