Do you want to delete it in your database or do you want to remove it from the DataTable
? Btw, use dr.IsNull("BIL")
instead. Your code compiles only because you've set OPTION STRICT off
because dr("BIL")
returns object instead of string.
Dataset are getting data from EXCEL,so,dont have any identity
column.BTW i just want remove from datatable, not database
Then you have to use DataRowCollection.Remove
instead of DataRow.Delete
. With Delete
wthe row will change it's RowState
to Deleted
. If you then use a DataAdapter
to update the DataSet
/DataTable
or DataRow
it will be deleted in the database.
But you can also use Linq-To-DataSet
to filter the table and use DataRow.Field
extension method which is strongly typed and supports nullable types:
Dim notNullBilRows = From row In ds.Tables(0)
Where Not String.IsNullOrEmpty(row.Field(Of String)("BIL"))
Now you can use CopyToDataTable
to create a new DataTable with only rows where BIL
is not null, which seems to be the actual requirement.
Dim tblNotNullBilRows = notNullBilRows.CopyToDataTable()
Here's the non-Linq approach with Remove
, you have to create an intermediate collection since you cannot remove elements from a collection during enumeration:
Dim removeList = New List(Of DataRow)
For Each dr As DataRow In ds.Tables(0).Rows
If String.IsNullOrEmpty(dr.Field(Of String)("BIL")) Then
removeList.Add(dr)
End If
Next
For Each dr As DataRow In removeList
ds.Tables(0).Rows.Remove(dr)
Next