Results 1 to 3 of 3

Thread: Finding a record in a dataset

  1. #1
    VonEhle is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2006

    Finding a record in a dataset

    I'm trying to figure out a way to find a row record in a dataset. There is a field in the table called "ID". I want to be able to give it a number and have it display the info from that row.

    Here's what I've been trying...

    Me.BindingContext(DsResults1, "tbl_Results").Position = 0
    Do Until drCurrent.Item("ID") Is txtGameID.Text
    drCurrent = DsResults1.tbl_Results.Rows(pintDataRow)
    pintDataRow += 1

    Thanks for your help.

  2. #2
    UncleRonin's Avatar
    UncleRonin is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2006
    South Africa
    One thing I dont get is the way you're using the 'Is' keyword. In my understanding, that keyword is used to check if two references refer to the same object, so if you're trying to check if two strings or numbers have the same value then this wont work (except under special circumstances). In this case wouldn't using the '=' operator be correct?

    The way you use your loop is rather odd as well (well, i learned how to use loops and that differently and am partial to using 'for' loops over any other ). I code in my own way but this is how I would handle the looping:
    Dim drCurrent As DataRow
    For Each drCurrent In DsResults1.tbl_Results.Rows
    If drCurrent.Item("ID") = txtGameID.Text Then
    Exit For
    End If
    After exiting the loop, the drCurrent variable already has the values for the row you were searching for so now you just have to access its items.

    *nod* ja, if its the primary key use the Find method to get the row

  3. #3
    DavidT_macktool's Avatar
    DavidT_macktool is offline VB.NET Forum Idol
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Oct 2004
    Dim DV As DataView
    Dim I As Integer
    DV = New DataView(DsDataSet1.Tables("TableName"))
    DV.Sort = ("ID")
    I = DV.Find(txtGameID.Text)
    Me.BindingContext(DsDataSet1, "TableName").Position = I
    You find the location of the matching record and then move to that location to access the other fields. Anything bound to the dataset will display the found row once you move there.
    David C. Talcott


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts