'Item' is not a member of 'DataRow()' - vb.net

I am trying to use this code from this answer but I keep getting this error
'Item' is not a member of 'DataRow()'
Does anyone know why? Or how I can get the first row of a data table?
Dim results As DataRow() = Students.Select("ID=1 and FirstName='Karthik'", "ID")
Test1Highest.Text = results.Item("Name") & " Got the highest in test 1!"

From DataTable.Select Method, we can see that the results variable will be an Array of DataRow objects.
Therefore, to access the first item in the list, you will need to add an Array Indexer.
Change this line:
Test1Highest.Text = results.Item("Name") & " Got the highest in test 1!
to be
If results IsNot Nothing AndAlso results.Count > 0 Then
Test1Highest.Text = results(0).Item("Name") & " Got the highest in test 1!
Else
Test1Highest.Text = "No results!"
End If
Or, you can do (using FirstOrDefault())
Dim singleResult As DataRow = Students.Select("ID=1 and FirstName='Karthik'", "ID").FirstOrDefault()
If singleResult IsNot Nothing Then
Test1Highest.Text = singleResult.Item("Name") & " Got the highest in test 1!
Else
Test1Highest.Text = "No results!"
End If
Looking at the question you linked to, the accepted answer has the command FirstOrDefault() being called from the result of the Select statement. This, like the name suggests, will return the first item in the array or a Null value if there are no elements in the array.

Related

how to check if dataset contains specific value in VB.net

I have a dataset that contains multiple values. I want to take those rows from that dataset that contains "the specific value" and firstly I want to display those in a MessageBox.
Furtheron, I try to view them in a datagridview called ErrorsDgV.
I already searched this topic and found a good function, but unfortunately, all I get from the MessageBox is an empty box.
ErrorsDgV.DataSource = Srchdataset.Tables("blubb")
LineLabel.Text = "Lines: " &
Srchdataset.Tables("blubb").Rows.Count.ToString
ErrorsDgV.Sort(ErrorsDgV.Columns(1), System.ComponentModel.ListSortDirection.Ascending)
ErrorsDgV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
ErrorsDgV.Columns(1).DefaultCellStyle.Format = "dd/MM/yyyy HH:mm:ss.fff"
Dim answer As String = ""
Dim SearchRows() As Data.DataRow
SearchRows = Srchdataset.Tables("blubb").Select("Data = 'the specific value'")
answer = ""
For k As Integer = 0 To SearchRows.Length - 1
If answer = "" Then
answer = SearchRows(k).Item("Data")
Else
answer = answer & vbNewLine & SearchRows(k).Item("Data")
End If
Next
MsgBox(" " & answer)
I debugged also and got to know that SearchRows is empty, even if the specific value is inlcuded in that DataSet.

Is there an easier way to do this?

I am looping through a listbox, and I need to get the same value you would get if you did .selectvalue, but without selecting the row. This is how I am doing it now, and it seems dumb.
For intCtr = 0 To lstUserRoleAll.Items.Count -1
lstUserRoleAll.SelectedIndex = intCtr
InsertUserRoles(lstUserRoleAll.SelectedValue)
Next
I have read that lstUserRoleAll.Items(intCtr).value should work, but value isn't a member of that. Also, lstUserRoleAll.Items(intCtr).tostring gets me the text "System.Data.DataRowView".
Surely there is a better way to do this.
Here is the code that I use to add the rows:
Dim rsSelectedGroups As DataTable
strSQL = "SELECT UID, Name "
[Rest of query]
rsSelectedGroups = DataAccess.GetDataTable(strSQL)
lstUserRoleCurrent.DataSource = rsSelectedGroups
lstUserRoleCurrent.ValueMember = "UID"
lstUserRoleCurrent.DisplayMember = "Name"
Because I populated the listbox with a datatable, the following code works. I did not realize that is what was being returned.
For intCtr = 0 To lstUserRoleAll.Items.Count
InsertUserRoles(lstUserRoleAll.Items(intCtr).Item("UID"))
Next

VB.NET - Unable to retrieve the first ID on a table using SELECT query

Good day everyone! After 3 years without using VB.NET I decided to use again for my project that not require web development.
this is my code (Reference: link)
cmdOLEDB.CommandText = "SELECT Price FROM tblPrice"
cmdOLEDB.Connection = cnnOLEDB
Dim rdrOLEDB As OleDbDataReader = cmdOLEDB.ExecuteReader
Dim priceList(18) As String
Dim i As Integer = 0
If rdrOLEDB.Read = True Then
While rdrOLEDB.Read()
priceList(i) = rdrOLEDB.GetValue(0)
i += 1
End While
txtPrice1.Text = priceList(0).ToString
cnnOLEDB.Close()
Else
MsgBox("Record not found.")
cnnOLEDB.Close()
End If
when I put this code in a MsgBox
MsgBox(rdrOLEDB.GetValue(0))
the result is "2" but I have 1 more data before that. It means the query retrieve the ID # 2 not the ID # 1. Here's the screenshot on my Access database
and when I use this code:
txtPrice1.Text = priceList(17).ToString
the result is 35.
You are skipping the first record because you call two times the Read method.
The first call reads the first record and returns true, then you enter the while loop extracting the info, but at this point you are on the second record.
If you want to check if there are rows then call HasRows
If rdrOLEDB.HasRows Then
While rdrOLEDB.Read()
priceList(i) = rdrOLEDB.GetValue(0)
i += 1
End While
txtPrice1.Text = priceList(0).ToString
cnnOLEDB.Close()
Else
MsgBox("Record not found.")
cnnOLEDB.Close()
End If
Please check with If condition , replace rdrOLEDB.Read with rdrOLEDB.hasrows
If rdrOLEDB.Hasrows= True Then
and check it again.

Listbox Remove Spaces

I am trying to use this code to remove spaces from a listbox but it is not working
Dim word As String() = {" "}
For i As Integer = 0 To ListBox5.Items.Count - 1
For Each Word As String In word
If ListBox5.Items(i).ToString.Contains(Word) Then
ListBox5.Items(i) = ListBox5.Items(i).ToString.Replace(Word, String.Empty)
End If
Next
Next
any help would be appreciated a lot.
You define a string array with one value and its static, why?
It seems you could do this simply by coding it like this
scan every item and replace " " with string.empty,
don't bother checking if it exists, just run the replace statement on every item
For i As Integer = 0 To ListBox5.Items.Count - 1
ListBox5.Items(i) = ListBox5.Items(i).ToString.Replace(" ", String.Empty)
Next
try this code
For i As Integer = 0 To ListBox5.Items.Count - 1
ListBox5.Items(i) = ListBox5.Items(i).ToString.Replace(" ", Nothing)
Next
fist we have to loop through the listbox items. we declared the items from index 0 to the final item index,ie listbox.items.count-1 . this is stored in a variable i. next all the items ,say from 0 to count-1 is replaced.
istBox5.Items(i).ToString.Replace(" ", Nothing) " " indicates a space and'nothing' indicates null.you can also use regex.replace here by importing system.regularexpressions.

VB.Net search for new occurencies of value in datatable

I'm trying to search one datatable for a string, so if new hits are appeared the action is triggered. How to do that?
My current code:
If searchvalue <> "" Then
foundRows = table.Select("Name LIKE '%" & searchvalue & "%'")
If foundRows.Length = 0 Then
'none found
Else
For Each r In foundRows
notif("Found "&r.itemarray(0) & " in " & r.itemarray(1))
Next
End If
End If
the "notif" function is called for every hit each time the sub is called; but i want it to be called once for every unique hit. How to do it?
Using case: say, first time sub is called when table is like that:
something foo
smthelse bar
the search string is "some", the Notif called once for "something foo". Next time the sub is called table is like that:
something foo
something else
smthelse bar
Now Notif should be called only for "something else"
I would suggest to use Linq-To-DataSet instead which makes your code more readable, maintainable and also has some nice features like grouping:
If searchvalue.Length <> 0 Then
Dim foundNamegroups = From row In table
Let Name = row.Field(Of String)("Name")
Where Name.Contains(searchvalue)
Group row By Name Into NameGroup = Group
If foundNamegroups.Any() Then
For Each grp In foundNamegroups
' note that the DataRows are in grp.NameGroup
Dim msg = String.Format("Found {0} rows for name {1}", grp.NameGroup.Count(), grp.Name)
notif(msg)
Next
End If
End If
Found the solution - i used the row.RowState property.
Each time the row is changed or added, its row.RowState property equals either DataRowState.Added or DataRowState.Modified, and when the row.AcceptChanges() called it becomes DataRowState.Unchanged .