Comparing one dataset to another dataset in vb.net - vb.net

I have two data set in my code. I need to compare that second data set
with first data set My first data set returns this result below:-
FirstDs:-
MaxUpdatedPrepped MaxUpdatedSent MaxUpdatedStamped
1900-01-01 1900-01-01 1900-01-01
And my second data set returns below:-
SecondDS:-
MaxUpdatedPrepped MaxUpdatedSent MaxUpdatedStamped
1900-01-01 1900-01-01 2014-11-11
I need to compare that both result and return alert like "Not matched" if the both first data set value is not match with second data set value. I tried a lot but i could get only wrong answer
For i As Integer = 0 To DsMaxDates1.Tables(0).Rows.Count - 1
Dim found As Boolean = False
For j As Integer = 0 To ds.Tables(0).Rows.Count - 1
If DsMaxDates1.Tables(0).Rows(i)(0).ToString = ds.Tables(0).Rows(j)(0).ToString Then
found = True
End If
Next
If found = False Then
ASPNET_MsgBox("Another User Working in Same Account. Please Click Reset.")
End If
Next
This above result returns true instead of false.

You should never change the type of your data unless it's absolutely necessary. Treat dates as Date, integers as Integer, strings as String, decimals as Decimal, etc. The ToString method is mostly used when you want to display the data to the user.
With that being said, you're not comparing datasets, you're comparing datatables.
The reason as to why it returns True is because you only compare the first column. You need to compare all the columns. If your table doesn't contain complex data types like byte arrays then the simplest way is to use LINQ combined with Enumerable.SequenceEqual.
The following code assumes that each table contains the same number of rows and columns.
''Uncomment to unleash the one-liner:
'Dim notEqual As Boolean = (From i As Integer In Enumerable.Range(0, DsMaxDates1.Tables(0).Rows.Count) Where (Not DsMaxDates1.Tables(0).Rows(i).ItemArray.SequenceEqual(ds.Tables(0).Rows(i).ItemArray)) Select True).FirstOrDefault()
Dim notEqual As Boolean = (
From i As Integer In Enumerable.Range(0, DsMaxDates1.Tables(0).Rows.Count)
Where (Not DsMaxDates1.Tables(0).Rows(i).ItemArray.SequenceEqual(ds.Tables(0).Rows(i).ItemArray))
Select True
).FirstOrDefault()
If (notEqual) Then
ASPNET_MsgBox("Another User Working in Same Account. Please Click Reset.")
End If
You can expand this even further by creating a reusable extension method:
Public Module Extensions
<System.Runtime.CompilerServices.Extension()>
Public Function SequenceEqual(table1 As DataTable, table2 As DataTable) As Boolean
Return (((((Not table1 Is Nothing) AndAlso (Not table2 Is Nothing))) AndAlso ((table1.Rows.Count = table2.Rows.Count) AndAlso (table1.Columns.Count = table2.Columns.Count))) AndAlso ((table1.Rows.Count = 0) OrElse (Not (From i As Integer In Enumerable.Range(0, table1.Rows.Count) Where (Not table1.Rows(i).ItemArray.SequenceEqual(table2.Rows(i).ItemArray)) Select True).FirstOrDefault())))
End Function
End Module
Then you can simply do as follows:
If (Not DsMaxDates1.Tables(0).SequenceEqual(ds.Tables(0))) Then
ASPNET_MsgBox("Another User Working in Same Account. Please Click Reset.")
End If

Related

Compare values with 3 states Y/N as String or DbNull

I have database table with a column DECISION which can have the following values:'Y','N', or (null). There can be multiple people updating the values in this database table via an application.
I want to ensure that the DataGridView which displays this information is kept up to date for all users regularly.
I have a background thread which does the following:
For Each new_row As DataRow In dsData.Tables("Progress").Rows
For Each cur_row As DataRow In dsData.Tables("List").Rows
If new_row("SEQ") = cur_row("SEQ") And new_row("DECISION") <> cur_row("DECISION") Then
cur_row("DECISION") = new_row("DECISION")
End If
Next
Next
Essentially dsData.Tables("Progress") is populated with the latest data and then compared to the current values in the DataGridView column, based on a sequence number SEQ.
What I want to happen is that if they are different, the cur_row value is changed to that of new_row.
However, it falls over because sometimes it tries to compare a string 'N' or 'Y' with (null) which can't be done.
What is the best way to work around this?
I do want to compare and update any (null) as it is a genuine state in this system.
The TryCast operator can help you here. You can try to cast as type String and a field containing DBNull.Value will simply return Nothing.
Dim cur_val = TryCast(cur_row("DECISION"), String)
Dim new_val = TryCast(new_row("DECISION"), String)
If new_val IsNot Nothing AndAlso cur_val <> new_val Then
cur_row("DECISION") = new_val
End If

Report Builder 3.0: syntax error when trying to loop through list of report parameters

I'm trying to do the following:
Provide a list of available values for a parameter. User can select multiple values or none.
If a certain value exists in the list of selected parameters, show the corresponding column. If not, hide the column.
To do this, I wrote a custom function that takes in an integer, checks if this integer exists in the list of parameters, and outputs False/True to hide/show the column.
Parameter Examples:
Label Value
Pallet 1
Item 2
.
.
.
nth-label n
The code: Report Builder 3.0 is saying that I have a syntax error on line 8, but I'm not sure what it could be.
dim parm_array() as String
dim col_num as Integer
dim i as Integer
function hide_column (ByVal col_num as Integer) as Boolean
parm_array = Split(Join(Report.Parameters!show.Value,","), ",")
for i in LBound(parm_array) To UBound(parm_array)
if col_num = CInt(parm_array(i)) then
hide_column = 0
exit for
else
hide_column = 1
end if
next
end function
You can do this directly in an expression with something like
=Array.IndexOf(Parameters!myParameter.Value, 1) > -1
Note: If you have left your parameter type as the default text then the expression will have to have the value quoted like this
=Array.IndexOf(Parameters!myParameter.Value, "1") > -1
or for readability we could use the parameter label instead of it's value
=Array.IndexOf(Parameters!myParameter.Label, "Pallet") > -1
You might need to reverse the result (i.e. <=-1) if the column visibility is doing the opposite of what you expected.

vb.net - Sort datagridview column numerically

I know this has been asked quite a few times, but i'm having issues with the solutions found on most other pages.
I have a single datagridview column that i want to be sorted by number (1,2,10 instead of 1,10,2)
Best i can see online, i need to convert the column or cell to an integer value type - but i'm not sure how to do so.
I've tried grid.columns(4).valuetype = typeof(System.int32), and tried the same for cells individually.
Trying above always results in a "int32 is a type in 'system' and cannot be used as an expression" error - which i'm not sure about.
The data itself is obtained froma text file, and converted from string to integer when being added into the cell datagrid_alltracks.Rows(shutupaboutlambda).Cells(4).Value = CInt(numstring))
You can just set the DataGridView SortCompare Event to compare two integers (or two singles, or two doubles). Code wise (calling your datagridview "grid")
Private Sub grid_SortCompare(sender as Object, e as DataGridViewSortCompareEventArgs) Handles grid.SortCompare
e.SortResult = CInt(e.cellvalue1).CompareTo(CInt(e.cellValue2))
e.Handled = True
End Sub
if you are doing single or double variables, use CSng or CDbl instead of CInt
e.SortResult = CSng(e.cellvalue1).Compareto(CSng(e.CellValue2)
You can do more fancy sorting if you want, You basically need to know that e.SortResult is Positive, Negative or Zero, and your cells are sorted according to that result (Positive keep order, negative reverse order, Zero - matched - do nothing (yet)). The current row index(s) and column are available in the e arguments so you can also compare adjacent column data if the current cells are matched)
If the grid is bound to a data source you could try
datatable.Columns.Add("ColumnName", GetType(Integer))
Else you may need to use the SortCompare event on the gridview.
See here
I know I'm coming to the party late, but I found that I once I had added the data, I needed to convert the desired columns to have a data type.
I'm adding data like the following:
DataGridView1.Rows.Add(New String() {CInt(recordnum), True, "play", wszName.ToString, qiOffset.ToString, value.ToString, qiLength.ToString})
Then, after all the data has been added, I then do a simple loop and convert the column, where I can then sort it. It's set up so you can do multiple columns if need be.
Dim colnum As Integer
colnum = 0 ' set this as your column to change the data type to
For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim d As Double = Double.Parse(DataGridView1.Rows(i).Cells(colnum).Value.ToString())
DataGridView1.Rows(i).Cells(colnum).Value = CInt(d)
DataGridView1.Rows(i).Cells(colnum).ValueType = GetType(Double)
Next
Sorting can work for whatever column you adjusted. In this case, it's column 4.
DataGridView1.Sort(DataGridView1.Columns(4), System.ComponentModel.ListSortDirection.Ascending)

Using LINQ to find updated rows in DataTable

I'm building an application in VB.NET where I am pushing data from one database to another. The source database is SQL Server and the target is MySQL.
What I am doing is first creating DataTables for each table in each database which I use to do a comparison. I've written the queries in such a way so that the source and target DataTables contain exactly the same columns and values to make the comparison easier.
This side of the application works fine. What I do next is find rows which do not exist in the target database by finding PKs which do not exist. I then insert these new rows into the target database with no problem.
The Problem
What I now need to do is find rows in each table that have been updated, i.e. are not identical to the corresponding rows in the target DataTable. I have tried using Except() as per the example below:
Public Function GetUpdates(ByVal DSDataSet As MSSQLQuery, ByVal AADataSet As MySQLQuery, Optional ByVal PK As String = Nothing) As List(Of DataRow)
' Determines records to be updated in the AADB and returns list of new Rows
' Param DSDataSet - MSSQLQuery Object for source table
' Param AADataSet - MySQLQuery Object for destination table
' Optional Param PK - String of name common columns to treat as PK
' Returns List(Of DataRow) containing rows to update in table
Dim orig = DSDataSet.GetDataset()
Dim origTable = orig.Tables(0).AsEnumerable()
Dim destination = AADataSet.GetDataset()
Dim destinationTable = destination.Tables(0).AsEnumerable()
' Get Records which are not in destination table
Dim ChangedRows = Nothing
If IsNothing(PK) Then
ChangedRows = destinationTable.AsEnumerable().Except(origTable.AsEnumerable(), DataRowComparer.Default)
End If
Dim List As New List(Of DataRow)
For Each addRow In ChangedRows
List.Add(addRow)
Next
Return List
End Function
The trouble is that it ends up simply returning the entire set of source rows.
How can I check for these changed rows? I could always hardcode queries to return what I want but this introduces problems because I need to make comparisons for 15 tables so it would be a complete mess.
Ideally I need a solution where it will take into account the variable number columns from the source tables for comparison against what is essentially an identical target table and simply compare the DataRows for equality.
There should be a corresponding row in the target tables for every source row since the addition of new rows is performed prior to this check for updated rows.
I am also open to using methods other than LINQ to achieve this.
Solution
In the end I implemented a custom comparer to use in the query as shown below. It first checks if the first column value matches (PK in my case) where if it does then it we check column-wise that everything matches.
Any discrepancy will set the flag value to FALSE which we return. If there aren't any issues then TRUE will be returned. In this case I used = to compare equality between values rather than Equals() since I'm not concerned about a strict equality.
The resulting set of DataRows is used to UPDATE the database using the first column value (PK) in the WHERE clause.
Imports System.Data
Class MyDataRowComparer
Inherits EqualityComparer(Of DataRow)
Public Overloads Overrides Function Equals(x As DataRow, y As DataRow) As Boolean
If x.Item(0).ToString().Equals(y.Item(0).ToString()) Then
' If PK matches then check column-wise.
Dim Flag As Boolean = True
For Counter As Integer = 0 To x.ItemArray.Count - 1
If Not x.Item(Counter) = y.Item(Counter) Then
Flag = False
End If
Next
Return Flag
Else
' Otherwise don't bother and just skip.
Return False
End If
End Function
...
End Class
class MyDataRowComparer : IEqualityComparer<DataRow>
{
public bool Equals(DataRow x, DataRow y)
{
return x["ColumnName"].Equals(y["ColumnName"]);
// Can add more columns to the Comparison
}
public int GetHashCode(DataRow obj)
{
return obj["ColumnName"].GetHashCode();
// Can add more columns to calculate HashCode
}
}
Now the Except statement will be like:
ChangedRows = destinationTable.AsEnumerable()
.Except(origTable.AsEnumerable(), MyDataRowComparer)

Function does not read last value

I am developing a program for a chain of restaurants, they should normally be able to calculate their guestcount and sales through a vb.net application with a connection to dbf files.
My dataset gets filled in correctly as is my datatable (checked by filling in datarowview = right data)
But then I get a problem, in all my functions using my datatable, the datatable skips the final value, in this case it is all values from a month so it either skips 31 or day 30.
Tried while, Tried for each, debugged alot (how i found it was the last value). But now I have no idea why the last value isn't used by the function
Public Function Getgctakeout(ByVal i_table As DataTable)
table = i_table
i = 0
gctakeout = 0
For Each row As DataRow In i_table.Rows
gctakeout = gctakeout + Convert.ToDouble(row(4))
Next row
'MessageBox.Show(gctakeout)
Return gctakeout
End Function
This function does not use the value of the last row to calculate gctakeout
what in the name of the lord is wrong :)
Assuming that your DataTable is really filled correctly, you have two other options to get the sum.
Use the old DataTable.Compute method which works also with .NET < 2.0
Use Linq-To-DatSet and it's Enumerable.Sum
1)
Dim Sum = CType(table.Compute("Sum(ColumnName)", Nothing), Double)
2)
Dim Sum = table.AsEnumerable().
Sum(Function(row)row.Field(Of Double)("ColumnName"))