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 :)
Printable View
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 :)
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.
And how can I display some fields of the record?
I used this to assign the row to a variable 'thisrow'
textbox1 is the textbox where the user enters the PK whose record they want to view.Code:Dim thisrow As PP4DBDataSet.Table1Row
thisrow = PP4DBDataSet1.Table1.FindByComplaint_Code(textbox1.text)
how do I display some fields of 'thisrow' record?
thanks :)
Is that table already bound to your UI?
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.
Quote:
In that case, I would recommend calling the Find method of the BindingSource.
what am I doing wrong?Code:Dim bs As New BindingSource
bs.DataSource = PP4DBDataSet.Table1
Dim found As Integer = bs.Find("Complaint Code", TextBox1.Text)
MsgBox(found)
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
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)
Nope. "DataMember property '[Complaint Code]' cannot be found on the DataSource."
there is.
Attachment 117697
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.
yeah, it is. The table which I've connected to m project.
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: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.Code:myBindingSource.Find("TableName.ColumnName", value)
okay. I'll try thisQuote:
Code:myBindingSource.Find("TableName.ColumnName", value)
is this what you wanted?Quote:
Open your DataSet in the designer and take a screen shot of that and post it.
Attachment 117753
well?