|
-
Aug 16th, 2014, 09:35 PM
#1
Thread Starter
Lively Member
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
-
Aug 16th, 2014, 09:46 PM
#2
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>)
-
Aug 17th, 2014, 12:49 AM
#3
Re: Locate records in a DataTable using Primary Key
 Originally Posted by dday9
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.
-
Aug 17th, 2014, 05:22 AM
#4
Thread Starter
Lively Member
Re: Locate records in a DataTable using Primary Key
And how can I display some fields of the record?
-
Aug 17th, 2014, 05:48 AM
#5
Thread Starter
Lively Member
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
-
Aug 17th, 2014, 05:59 AM
#6
Thread Starter
Lively Member
Re: Locate records in a DataTable using Primary Key
 Originally Posted by bruel1999
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?
-
Aug 17th, 2014, 06:05 AM
#7
Re: Locate records in a DataTable using Primary Key
Is that table already bound to your UI?
-
Aug 18th, 2014, 10:33 AM
#8
Thread Starter
Lively Member
Re: Locate records in a DataTable using Primary Key
-
Aug 18th, 2014, 05:41 PM
#9
Re: Locate records in a DataTable using Primary Key
 Originally Posted by bruel1999
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.
-
Aug 18th, 2014, 05:42 PM
#10
Re: Locate records in a DataTable using Primary Key
 Originally Posted by bruel1999
okay, this isn't working. sorry :P
It's returning a null value. why?
There is no row with that PK value.
-
Aug 18th, 2014, 08:33 PM
#11
Thread Starter
Lively Member
Re: Locate records in a DataTable using Primary Key
 Originally Posted by jmcilhinney
There is no row with that PK value.
no, there is.
-
Aug 18th, 2014, 08:46 PM
#12
Re: Locate records in a DataTable using Primary Key
 Originally Posted by bruel1999
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.
-
Aug 18th, 2014, 08:56 PM
#13
Thread Starter
Lively Member
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
-
Aug 18th, 2014, 09:07 PM
#14
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)
-
Aug 19th, 2014, 09:03 AM
#15
Thread Starter
Lively Member
Re: Locate records in a DataTable using Primary Key
Nope. "DataMember property '[Complaint Code]' cannot be found on the DataSource."
-
Aug 19th, 2014, 06:04 PM
#16
Re: Locate records in a DataTable using Primary Key
 Originally Posted by bruel1999
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.
-
Aug 19th, 2014, 08:38 PM
#17
Thread Starter
Lively Member
Re: Locate records in a DataTable using Primary Key
there is.
-
Aug 19th, 2014, 08:49 PM
#18
Re: Locate records in a DataTable using Primary Key
 Originally Posted by bruel1999
there is.

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.
-
Aug 19th, 2014, 09:04 PM
#19
Thread Starter
Lively Member
Re: Locate records in a DataTable using Primary Key
yeah, it is. The table which I've connected to m project.
-
Aug 19th, 2014, 09:23 PM
#20
Re: Locate records in a DataTable using Primary Key
 Originally Posted by bruel1999
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.
-
Aug 19th, 2014, 09:49 PM
#21
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.
-
Aug 21st, 2014, 07:08 AM
#22
Thread Starter
Lively Member
Re: Locate records in a DataTable using Primary Key
Code:
myBindingSource.Find("TableName.ColumnName", value)
okay. I'll try this
-
Aug 21st, 2014, 07:20 AM
#23
Thread Starter
Lively Member
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?
Last edited by bruel1999; Aug 21st, 2014 at 08:52 PM.
-
Sep 1st, 2014, 07:05 AM
#24
Thread Starter
Lively Member
Re: Locate records in a DataTable using Primary Key
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|