Results 1 to 24 of 24

Thread: Locate records in a DataTable using Primary Key

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Locate records in a DataTable using Primary Key

    Hey people,
    I have a dataset called PP4DBDataSet and a table called Table1 within it. This table has a PK called 'Complaint code'. How do I search for and display records using the PK?
    All help appreciated,
    Thanks

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Locate records in a DataTable using Primary Key

    Bind a BindingSource to the DataTable and the set the Filter property to where the Complaint code equals the value that you're searching. That would look something like this:
    Code:
    BindingSource.Filter = String.Format("complaint_code = '{0}'", <some value here>)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Locate records in a DataTable using Primary Key

    Quote Originally Posted by dday9 View Post
    Bind a BindingSource to the DataTable and the set the Filter property to where the Complaint code equals the value that you're searching. That would look something like this:
    Code:
    BindingSource.Filter = String.Format("complaint_code = '{0}'", <some value here>)
    Just keep in mind that that is going to hide every other record too, which may not be what you want. If you want the actual DataRow then you should call Rows.Find on the DataTable. You can also call Find on the BindingSource to get the index of the record, allowing for any sorting and filtering, and then assign that to the Position property. That will select the appropriate record without hiding any others.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    And how can I display some fields of the record?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    I used this to assign the row to a variable 'thisrow'
    Code:
    Dim thisrow As PP4DBDataSet.Table1Row
    thisrow = PP4DBDataSet1.Table1.FindByComplaint_Code(textbox1.text)
    textbox1 is the textbox where the user enters the PK whose record they want to view.
    how do I display some fields of 'thisrow' record?
    thanks

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    Quote Originally Posted by bruel1999 View Post
    I used this to assign the row to a variable 'thisrow'
    Code:
    Dim thisrow As PP4DBDataSet.Table1Row
    thisrow = PP4DBDataSet1.Table1.FindByComplaint_Code(textbox1.text)
    textbox1 is the textbox where the user enters the PK whose record they want to view.
    how do I display some fields of 'thisrow' record?
    thanks
    okay, this isn't working. sorry :P
    It's returning a null value. why?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Locate records in a DataTable using Primary Key

    Is that table already bound to your UI?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    yes.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Locate records in a DataTable using Primary Key

    Quote Originally Posted by bruel1999 View Post
    yes.
    In that case, I would recommend calling the Find method of the BindingSource. It's not as good as the method you're calling from the point of view that it requires you to specify the column by name as a String rather than the ID column being implicit in the method itself, but it's easier from the point of view that it will give you the index of the matching row, which you can then simply assign to the Position property of the BindingSource to make that row the Current. That said, if you know for a fact that the DataRows in the DataTable are in the same order as the DataRowViews in the BindingSource, i.e. you haven't applied any sorting or filtering, then you can use the DataRow to find its index in the DataTable and then assign that to the Position.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Locate records in a DataTable using Primary Key

    Quote Originally Posted by bruel1999 View Post
    okay, this isn't working. sorry :P
    It's returning a null value. why?
    There is no row with that PK value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    Quote Originally Posted by jmcilhinney View Post
    There is no row with that PK value.
    no, there is.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Locate records in a DataTable using Primary Key

    Quote Originally Posted by bruel1999 View Post
    no, there is.
    If there was a row in your DataTable with a Complaint_Code that matched the value you were providing then it would be returned by that method. If the method returns Nothing then there is no matching row.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    In that case, I would recommend calling the Find method of the BindingSource.
    Code:
    Dim bs As New BindingSource
            bs.DataSource = PP4DBDataSet.Table1
            Dim found As Integer = bs.Find("Complaint Code", TextBox1.Text)
            MsgBox(found)
    what am I doing wrong?

    The messagebox is just to test whether the code is returning the right value, and it isn't. Whatever the text in textbox1 is, the value of found is always -1

  14. #14
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Locate records in a DataTable using Primary Key

    It could be the fact that you have whitespace in your column name. Try wrapping the column name in square brackets:
    Code:
    bs.Find("[Complaint Code]", TextBox1.Text)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    Nope. "DataMember property '[Complaint Code]' cannot be found on the DataSource."

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Locate records in a DataTable using Primary Key

    Quote Originally Posted by bruel1999 View Post
    Nope. "DataMember property '[Complaint Code]' cannot be found on the DataSource."
    Then you have no such column. You need to check your table column names.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    there is.
    Name:  db.jpg
Views: 656
Size:  9.0 KB

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Locate records in a DataTable using Primary Key

    Quote Originally Posted by bruel1999 View Post
    there is.
    Name:  db.jpg
Views: 656
Size:  9.0 KB
    That looks rather like a screen shot of Access. That's irrelevant. When you call Find on a BindingSource, you're providing the name of a PropertyDescriptor. In this case, that maps to ColumnName of a DataColumn in the Columns property of the DataTable bound to your BindingSource. If you're getting that error message then there is no such column.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    yeah, it is. The table which I've connected to m project.

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Locate records in a DataTable using Primary Key

    Quote Originally Posted by bruel1999 View Post
    yeah, it is.
    No, it's not. If there was a column with that name then you wouldn't be getting that error message. Open your DataSet in the designer and take a screen shot of that and post it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Locate records in a DataTable using Primary Key

    OK, I think I might know what the issue is. If you have bound the BindingSource in the designer then the DataSource will actually your DataSet rather than your DataTable. It may be that you need to qualify the column name with the table name in this particular case. I'll do a bit of testing and see what I can see but, if you want to try that in the mean time for yourself then you can, i.e. use something like:
    Code:
    myBindingSource.Find("TableName.ColumnName", value)
    For future reference, I would strongly recommend NOT putting spaces in column names or the like. It's not the issue specifically in this case but it just makes life harder by adding confusion at least. Just use names like "ComplaintCode", exactly as you would in VB code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    Code:
    myBindingSource.Find("TableName.ColumnName", value)
    okay. I'll try this

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    Open your DataSet in the designer and take a screen shot of that and post it.
    is this what you wanted?
    Name:  labaad.JPG
Views: 768
Size:  32.7 KB
    Last edited by bruel1999; Aug 21st, 2014 at 08:52 PM.

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Locate records in a DataTable using Primary Key

    well?

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width