Decrypt fields in VB.NET DataSet - vb.net

I have this working fine in a DataTable, however the DataSet does not have the .Rows property. All fields will not be encrypted, thus they will not all be decrypting. I am assuming it would be some kind of loop, like:
For (i = 0, i < DataSet.ColumnIndex [Or something], i++)
However, I am not sure how to perform this.
Essentially, when I bring back data using a SELECT queries based on input parameters the user enters (first name, last name) I would like to decrypt specific rows.
How I currently use it:
Try
For i As Integer = 0 To dt.Rows.Count - 1
dt.Rows(i)("FIRST_NM_TXT") = clsEncrypt.DecryptData(dt.Rows(i)("FIRST_NM_TXT"))
dt.Rows(i)("LAST_NM_TXT") = clsEncrypt.DecryptData(dt.Rows(i)("LAST_NM_TXT"))
Next
Catch ex As Exception
MessageBox.Show("Either the first name or last name did not match. Please check your spelling.")
End Try
The reason I need a DataSet is because I need to run reports off of this decrypted data. I have tried with my DataTable, however I have not been successful. From research, it seems as though DataSet is the common choice anyway.

A DataSet object is just a collection of DataTable objects
You can access the DataTables in a DataSet by:
Oridinal Dim MyDataTable as DataTable = MyDataSet.Tables(2) or
Name Dim MyDataTable as DataTable = MyDataSet.Tables("Customers")
So just use one of the above methods to decrypt the data once you have the DataSet
For i As Integer = 0 To MyDataTable.Rows.Count - 1
MyDataTable.Rows(i)("FIRST_NM_TXT") = clsEncrypt.DecryptData(MyDataTable.Rows(i)("FIRST_NM_TXT"))
MyDataTable.Rows(i)("LAST_NM_TXT") = clsEncrypt.DecryptData(MyDataTable.Rows(i)("LAST_NM_TXT"))
Next

Related

Read Data from Datatable via searched Record in Vb.Net

Hey i have an untyped Dataset with one Datatable and 9 Columns in it. Here are my DS DT and Columns
Dataset = ds_Databank_Skills
Datatable = dtab_Skills_Summary
Skill_ID = Int32 Unique and Primary Key
Skillname = String Unique
Lus = Boolen
Expert = Boolean
Categorie = String
Attribut = String
EP_Cost = Int16
Max_Level = Int16
Description = String
I would like to search my Datatable. Best way is Skillname *but i dont know if that works good (because the values have spaces and "" and - in it. So perhabs i need to search by Primary Key so Skill_ID. All workd good, load it into my Datatable via xml File. But i cant find a way to search my Datatable and get the Values from the other Columns. This Values, i like to store in some Variables with same type (int32 int16 string and boolean) for further usage.
Perhabs someone like to help me here?
thank you so much for that.
I searched the web for some help, but most uses mysql or get all Data from Datatable. And iam sure there is an easy way to get that data.
You could leverage the DataTable.Select method (documentation). There is an argument signature that allows you to pass in a String which represents a filter expression (documentation).
Here is an example:
Dim searchTerm = "Some Value"
Dim expression = $"[Skillname] LIKE '*{searchTerm}*'"
Dim results = dtab_Skills_Summary.Select(expression)
For Each result In results
Dim maxLevel = result.Item("Max_Level")
Console.WriteLine("Max Level: {0}", maxLevel)
Next

How do I query a local datatable and return information to a datatable in VB.net

I am trying to pass a query and existing datatable into a function. The function will query the passed datatable using the passed query and return the result.
Unfortunately, I am unable to return any data. I have posted my code below. Can anyone help me fix it? I don't know what I am doing wrong.
Public Function ExecQueryTest(Query As String, DT As DataTable) As DataTable
Dim Result() As DataRow
'initialize the table to have the same number of columns of the table that is passed into the function
Dim LocalTable As DataTable = DT
'initialize counting variables
Dim x, y As Integer
'use the select command to run a query and store the results in an array
Result = DT.Select(Query)
'remove all items from the localtable after initial formatting
For x = 0 To LocalTable.Rows.Count - 1
LocalTable.Rows.RemoveAt(0)
Next
'for loop to iterate for the amount of rows stored in result
For x = 0 To Result.GetUpperBound(0)
'add each array row into the table
LocalTable.Rows.Add(Result(x))
Next
ExecQueryTest = LocalTable
End Function
If there is a better way to accomplish my goal, I don't mind starting from scratch. I just want to be able to handle dynamic tables, queries, and be able to return the information in a datatable format.
The problem is here:
Dim LocalTable As DataTable = DT
That code does not do what you think it does. DataTable is a reference type, which means assigning DT to the LocalTable variable only assigns a reference to the same object. No new table is created, and nothing is copied. Therefore, this later code also clears out the original table:
'remove all items from the localtable after initial formatting
For x = 0 To LocalTable.Rows.Count - 1
LocalTable.Rows.RemoveAt(0)
Next
Try this instead:
Public Function ExecQueryTest(Query As String, DT As DataTable) As DataTable
ExecQueryTest = New DataTable() 'create new DataTable object to hold results
For Each row As DataRow In DT.Select(Query)
ExecQueryTest.LoadDataRow(row.ItemArray, True)
Next
End Function
Though you may also need to clone each DataRow record.
You can clear a table with just
LocalTable.Clear()
instead of using that cycle, Also the results of your select can be directly converted to datatable using
LocalTable = Result.CopyToDataTable

Fastest way of filling a combobox from a datatable in VB.Net

The data table in the following code is filled with 7500-+ records. This all loads quickly from the server. The problem is that it takes a while to loop through the data rows to add them to the combo box. Is there any alternative way of setting the data source of the combo box or a way of speeding this process up?
Dim dtColours As New DataTable
Dim daColours As New SqlDataAdapter
Dim i As Integer
ConnectToSQL()
daColours = New SqlDataAdapter("SELECT DISTINCT Rtrim(UPPER(Colour)) As Colour FROM invStockColour WHERE InUse = 1 ORDER BY Colour", dbSQL)
daColours.Fill(dtColours)
For i = 0 To dtColours.Rows.Count - 1
cboColours.Items.Add(dtColours.Rows(i).Item(0).ToString)
Next
dbSQL.Close()
The fasted way would be to use the AddRange method instead of using Add, something like:
Dim items = dtColours.AsEnumerable().Select(Function(d) DirectCast(d(0).ToString(), Object)).ToArray()
cboColours.Items.AddRange(items)
I did a simple check and using AddRange is ~3x faster than using Add.
Of course allocating an array and filling it with a For loop would probably some milliseconds faster than using Linq.
Dim daColours As New SqlDataAdapter("SELECT DISTINCT Rtrim(UPPER(Colour)) As Colour FROM invStockColour WHERE InUse = 1 ORDER BY Colour", dbSQL)
Dim dtColours As New DataTable
daColours.Fill(dtColours)
cboColours.DataSource=dtColours
cboColours.DisplayMember="Colour"
You could also bind your ComboBox DataSource property to the DataTable. This has the added advantage of binding other column data (such as key values you might not want the user to see).
You should be able to return a DataTable object from your SQLAdapter.
cboColours.DataSource = dtColours
cboColours.DisplayMember = dtColours.Columns("Colour").ToString
cboColours.ValueMember = dtColours.Columns("Colour_id").ToString
Try this:
cboColours.DataSource = dtColours 'For Windows Forms or
cboColours.ItemsSource = dtColours 'For WPF

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"))

Get the BindingSource position based on DataTable row

I have a datatable that contains the rows of a database table. This table has a primary key formed by 2 columns.
The components are assigned this way: datatable -> bindingsource -> datagridview. What I want is to search a specific row (based on the primary key) to select it on the grid. I cant use the bindingsource.Find method because you only can use one column.
I have access to the datatable, so I do manually search on the datatable, but how can I get bindingsource row position based on the datatable row? Or there is another way to solve this?
Im using Visual Studio 2005, VB.NET.
I am attempting to add an answer for this 2-year old question. One way to solve this is by appending this code after the UpdateAll method(of SaveItem_Click):
Me.YourDataSet.Tables("YourTable").Rows(YourBindingSource.Position).Item("YourColumn") = "YourNewValue"
Then call another UpdateAll method.
Well, I end up iterating using bindingsource.List and bindingsource.Item. I didnt know but these properties contains the data of the datatable applying the filter and sorting.
Dim value1 As String = "Juan"
Dim value2 As String = "Perez"
For i As Integer = 0 To bsData.Count - 1
Dim row As DataRowView = bsData.Item(i)
If row("Column1") = value1 AndAlso row("Column2") = value2 Then
bsData.Position = i
Return
End If
Next