Finding record in datatable

  • Thread starter Thread starter Chris Kennedy
  • Start date Start date
C

Chris Kennedy

I want to delete a specific record in a dataset based on its primary key
which I can get from a the value of a dropdown menu. I tried using the
select method to filter the datatable on the primary key and as this table
should only contain 1 record I should be deleting the right record. It
still, however, deletes the first record in the dataset not the filtered
table. Can anyone help. Some pointers to code examples would be great as
ADO.net is a real weak point. Regards, Chris.



Dim dt As New Data.DataTable()

dt = dsusers.Tables("wf_getusernames")

Dim strdelete As String

strdelete = "adminid=" & drpUserNames.SelectedItem.Value

dt.Select(strdelete)

dt.Rows(0).Delete()
 
The Select method returns a datarow array. It does absolutely nothing to the
original DataTable. I am not sure why you assumed that it filters the
datatable, but I recommend you read the documentation on this.

You could do something like this:

Dim rows() As DataRow = dt.Select(strDelete)
rows(0).Delete()

Alternatively, you can put a DataView on the datatable, and delete via the
view.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top