Yustme
Yustme

Reputation: 6255

Check if dataset contains a specific value

How can i check if a dataset contains a specific value? It's crazy that no one has done this before. Couldn't find it on the net!!!

Upvotes: 0

Views: 33653

Answers (3)

Vivek Singh
Vivek Singh

Reputation: 31

Suppose I have Dataset DsStu containing Table Student with Columns [Rollno,Name,Branch].

Case 1 I want Name of student whose Rollno is 15.

     Dim answer As String = ""
     Dim SerchRows() As Data.DataRow
     SerchRows = DsStu.Tables(0).Select("Rollno = '15'") 
     answer = ""

     For k As Integer = 0 To SerchRows.Length - 1
       If answer = "" then  
          answer = SerchRows(k).Item("Name")
       Else
          answer = answer & Vbnewline & SerchRows(k).Item("Name")
       End If
     Next

     MsgBox(" " & answer)

Case 2 I want Name of all student whose Rollno is greater than equal to 15 and Branch is Electrical.

     Dim answer As String = ""
     Dim SerchRows() As Data.DataRow

     SerchRows = DsStu.Tables(0).Select("Rollno >= '15' And Branch = 'Electrical'") 
     answer = ""

     For k As Integer = 0 To SerchRows.Length - 1
       If answer = "" then  
          answer = SerchRows(k).Item("Name")
       Else
          answer = answer & Vbnewline & SerchRows(k).Item("Name")
       End If
     Next

     MsgBox(" " & answer)

Upvotes: 3

Hasan
Hasan

Reputation: 154

If you're using a BindingSource use the "Find" method: http://msdn.microsoft.com/en-us/library/ms158165.aspx

So if it returns -1 it's not there and otherwise it will return the position.

Upvotes: 2

pjnovas
pjnovas

Reputation: 1116

You mean go through the entire dataset tables, columns and rows?

Here is something that could help you:

Dim valueToSearch as String = "some text"
For Each dTable As DataTable In ds.Tables
    For Each dRow As DataRow In dTable.Rows
        For index As Integer = 0 To dTable.Columns.Count - 1
            Convert.ToString(dRow(index)).Contains(valueToSearch)
        Next
    Next
Next

Upvotes: 5

Related Questions