VB.NET delete empty datarow - vb.net

For Each dr In ds.Tables(0).Rows
If String.IsNullOrEmpty(dr("BIL")) Then
dr.Delete() //how to delete this row?
End If
Next
first,will loop all data then check which row in BIL column are empty,if the row in BIL column are empty,then delete the row from dataset,how to delete this empty datarow?

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

Try this:
For i As Integer = dt.Rows.Count - 1 To 0 Step -1
If String.IsNullOrEmpty(dt.Rows(i)("BIL")) Then
dt.Rows.RemoveAt(i)
End If
Next

You will want to put the index of the rows you wish to delete into an array, then iterate through the array deleting each rows from the datatable using the indexes. You will not need an 'identity' column to do this, the rows will automatically be asigned indexes.

Assuming you have 2 columns in table tbl: ColumnA and ColumnB
Dim dv as new DataView
dv = new DataView(tbl)
dv.RowFilter = "ColumnA <> '' AND ColumnB <> ''"
tbl = dv.ToTable()
tbl should no longer have empty rows. Hope this helps.

Related

Selecting a particular column from dataTable and updating values in all the rows

I'm currently selecting a bunch of values from SQL and load them into a dataTable. I'm trying to find that particular column and update the value in each rows.
Dim dt as DataTable
'not all code is included - but this definitely works on selecting data
sql = " Select value1, value2, value3 from TblA "
cmd = New SqlClient.SqlCommand(sql, conn)
Dim reader As SqlDataReader = cmd.ExecuteReader
dt.Load(reader)
return dt
My goal is to take the value from each row in VALUE3, and run it through a small Formatting function called - UpdateFormat. Something along the lines of....
Dim specValue As String
For Each row As DataRow In dt.Rows
UpdateFormat(specValue ) = row.Item("Value3")
Next row
I'm hoping to update each value and update the dataTable so it has the correct values.
In a previous episode, we learned that you want to format the data in that column from 012345678 to 012-34-5678W where the column is VarChar and the 'W' is fixed.
You can do the formatting in the SQL:
Dim sql = <sql>
SELECT Id, Foo, Bar, ColA,
CONCAT(SUBSTRING([Value3],1,3),
'-',
SUBSTRING([Value3],4,2),
'-',
SUBSTRING([Value3],6,4),
'W' ) AS NewV3Value
FROM
dbo.TableBob
</sql>.Value
Doing so, the column is delivered to you already formatted. Depending on your DB flavor, the exact syntax may vary.
The <sql>...</sql>.Value is just an XML literal which allows you to format long or complex queries in such a way that it makes it easier for you to read.
To do it yourself in the table. As the format is trivial to apply, you really dont need a method to do it:
Dim tmp As String
For Each row As DataRow In dt.Rows
tmp = row.Field(Of String)("Value3")
row("Value3") = String.Format("{0}-{1}-{2}W",
tmp.Substring(0, 3),
tmp.Substring(3, 2),
tmp.Substring(5, 4))
Next row
The tmp var is just to shorten the code.

Vb.net pull in a SQL table row by row

I am a little new to using vb.net and SQL so I figured I would check with you guys to see if what I am doing makes sense, or if there is a better way. For the first step I need to read in all the rows from a couple of tables and store the data in the way the code needs to see it. First I get a count:
mysqlCommand = New SQLCommand("SELECT COUNT(*) From TableName")
Try
SQLConnection.Open()
count = myCommand.ExecuteScalar()
Catch ex As SqlException
Finally
SQLConnection.Close()
End Try
Next
Now I just want to iterate through the rows, but I am having a hard time with two parts, First, I cannot figure out the SELECT statement that will jet me grab a particular row of the table. I saw the example here, How to select the nth row in a SQL database table?. However, this was how to do it in SQL only, but I was not sure how well that would translate over to a vb.net call.
Second, in the above mycommand.ExecuteScalar() tell VB that we expect a number back from this. I believe the select statement will return a DataRow, but I do not know which Execute() statement tells the script to expect that.
Thank you in advance.
A simple approach is using a DataTable which you iterate row by row. You can use a DataAdapter to fill it. Use the Using-statement to dispose/close objects property that implement IDisposable like the connection:
Dim table = New DataTable
Using sqlConnection = New SqlConnection("ConnectionString")
Using da = New SqlDataAdapter("SELECT Column1, Column2, ColumnX FROM TableName ORDER By Column1", sqlConnection)
' you dont need to open/close the connection with a DataAdapter '
da.Fill(table)
End Using
End Using
Now you can iterate all rows with a loop:
For Each row As DataRow In table.Rows
Dim col1 As Int32 = row.Field(Of Int32)(0)
Dim col2 As String = row.Field(Of String)("Column1")
' ...'
Next
or use the table as DataSource for a databound control.

Datatable Sorting From A Custom Number Sequence

My datatable has a field that has values like so..
0000006685-001
0000006685-002
0000006713-001
0000006714-002
0000006713-002
0000006697-002
I want the datatable to be re-ordered by this field so the numbers on the left of the hyphen go in order, followed by the corresponding sequence in order. So the above example data would be ordered like..
0000006685-001
0000006685-002
0000006697-002
0000006713-001
0000006713-002
0000006714-002
Dim dv As New DataView(parsedDataset.Tables("Detail"))
dv.Sort = "the field"
Dim dt As DataTable = dv.ToTable
The easiest way is, most probably, through the Select method then the CopyToTable method
dt = dt.Select("", "ColumnName ASC").CopyToDataTable
Replace ColumnName with the name of the column for that field.

DataTable.Select.Where VB.Net - Delete rows

I'm currently pulling information using a query that I'm not allowed to tamper with:
Dim dt As DataTable = BLL.GetData(variable).Tables(0)
Immediately afterwards, I'm removing any records where a field begins with a specific value:
For Each dr As DataRow In dt.Rows
If dr.Item(2).ToString().StartsWith("value") Then
dr.Delete()
End If
Next
What I'd really like to do is something like:
dt.Select.Where(field1 => field1.StartsWith("value")).Delete()
I know that is not the syntax of it and I'm probably very off from what it would be like. The For Each works fine, I'm just trying to "simplify" it. Any idea? Any and all help is appreciated.
Actually, your initial code is probably the cleanest and most straight forward.
To delete items using LINQ, you first need to read them into a separate collection, then loop through that collection and call Delete on each record. If you'd rather go that route, you could try:
Dim records = dt.Rows.Where(Function(r) r.StartsWith("value")).ToList()
For Each r In records
r.Delete()
Next
The answer I think you are looking for is below from Microsoft. https://msdn.microsoft.com/en-us/library/det4aw50(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
Dim table As DataTable = DataSet1.Tables("Orders")
' Presuming the DataTable has a column named Date.
Dim expression As String
expression = "Date > #1/1/00#"
Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression)
Dim i As Integer
' Print column 0 of each returned row.
For i = 0 to foundRows.GetUpperBound(0)
Console.WriteLine(foundRows(i)(0))
Next i

VB.Net - Efficient way of de-duplicating data

I am dealing with a legacy application which is written in VB.Net 2.0 against a SQL 2000 database.
There is a single table which has ~125,000 rows and 2 pairs of fields with similar data.
i.e. FieldA1, FieldB1, FieldA2, FieldB2
I need to process a combined, distinct list of FieldA, FieldB.
Using SQL I have confirmed that there are ~140,000 distinct rows.
Due to a very restrictive framework in the application I can only retrieve the data as either 2 XML objects, 2 DataTable objects or 2 DataTableReader objects. I am unable to execute custom SQL using the framework.
Due to a very restrictive DB access policy I am unable to add a View or Stored Proc to retrieve as a single list.
What is the most efficient way to combine the 2 XML / DataTable / DataTableReader objects into a single, distinct, IEnumerable object for later processing?
I may have missed something here but could you not combine both DataTables using Merge?
DataTableA.Merge(DataTableB)
You can then use DataTableA.AsEnumerable()
Then see this answer on how to remove duplicates or
You can do this with a DataView as follows: dt.DefaultView.ToTable(True,[Column names])
This is the solution I came up with.
Combine the 2 DataTables using .Merge (thanks to Matt's answer)
Using this as a base I came up with the following code to get distinct rows from the DataTable based on 2 columns:
Private Shared Function GetDistinctRows(sourceTable As DataTable, ParamArray columnNames As String()) As DataTable
Dim dt As New DataTable
Dim sort = String.Empty
For Each columnName As String In columnNames
dt.Columns.Add(columnName, sourceTable.Columns(columnName).DataType)
If sort.Length > 0 Then
sort = sort & ","
End If
sort = sort & columnName
Next
Dim lastValue As DataRow = Nothing
For Each dr As DataRow In sourceTable.Select("", sort)
Dim add As Boolean = False
If IsNothing(lastValue) Then
add = True
Else
For Each columnName As String In columnNames
If Not (lastValue(columnName).Equals(dr(columnName))) Then
add = True
Exit For
End If
Next
End If
If add Then
lastValue = dr
dt.ImportRow(dr)
End If
Next
Return dt
End Function