Link to home
Start Free TrialLog in
Avatar of ljhodgett
ljhodgett

asked on

clear datatable

Hi,

i have the following code: -

            lstRigs.Items.Clear()
            sql = "SELECT * FROM Where_Used where ProductID ='" & cmbProducts.SelectedValue.ToString & "'"

            dbOpen()
            Dim da4 As New SqlClient.SqlDataAdapter(sql, oConn)
            da4.Fill(ds, "Where_Used") ' populate data set "da" with the data returned from sql command "ds" and set dataset name to "Rigs"


            lstRigs.DisplayMember = "RigID"
            lstRigs.ValueMember = "DeptID"
            lstRigs.DataSource = ds.Tables("Where_Used")

It wont allow me to clear the listbox I presume because its bound to a datatable. I'm trying to find out how I clear it please.

Many thanks
Lee
Avatar of appari
appari
Flag of India image

try

lstRigs.DataSource = nothing
appari is correct

first remove the datasource of control,
second clear the items
third again bind the data to the control


Avatar of ljhodgett
ljhodgett

ASKER

Hi,

No joy i'm afraid. I've tried : -

            sql = "SELECT * FROM Where_Used where ProductID ='" & cmbProducts.SelectedValue.ToString & "'"

            lstRigs.DataSource = Nothing
            lstRigs.Items.Clear()
            lstRigs.Refresh()
            dbOpen()
            Dim da4 As New SqlClient.SqlDataAdapter(sql, oConn)
            da4.Fill(ds, "Where_Used") ' populate data set "da" with the data returned from sql command "ds" and set dataset name to "Rigs"
            'DataGridView1.DataSource = ds.Tables("Rigs")

            lstRigs.DisplayMember = "RigID"
            lstRigs.ValueMember = "DeptID"
            lstRigs.DataSource = ds.Tables("Where_Used")

but it still does not clear the datatable.

Best Regards
Lee
>>but it still does not clear the datatable.
what do you mean?
after clearing you are binding the new table again. so you are seeing the new results.
try clearing the dataset also

by

ds.clear
Hi,

I'm not seeing the new results just adding to the end I presume because the datatable is not empty. I did think of clearing the dataset using ds.clear but the problem is that I use the same dataset fro multiple datatables. Is there anyway I can clear datatable da4 by itself.

Many thanks
Lee
ASKER CERTIFIED SOLUTION
Avatar of Ajay Sharma
Ajay Sharma
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've tried: -

            ds.Tables("da4").Clear()
            dbOpen()
            Dim da4 As New SqlClient.SqlDataAdapter(sql, oConn)
            da4.Fill(ds, "Where_Used") ' populate data set "da" with the data returned from sql command "ds" and set dataset name to "Rigs"
            'DataGridView1.DataSource = ds.Tables("Rigs")

            lstRigs.DisplayMember = "RigID"
            lstRigs.ValueMember = "DeptID"
            lstRigs.DataSource = ds.Tables("Where_Used")

but it comes up Object reference not set to an instance of an object.

Best Regards
Lee