Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a record in a dataset 1

Status
Not open for further replies.

vonehle

Programmer
May 16, 2006
35
US
I'm still pretty new to VB.NET.

I'm trying to figure out how to display a record from a dataset based on a value entered by the user. In this case, I have multiple lines with a field called "ID". The user should be able to type in an "ID" and have the record appear. I can't seem to figure out how to get it to find the record. I tried using rows.find, but I must not be using it correctly.

Example:
Do Until pstrGameID = txtEnterGameID.Text
drCurrent = DsResults1.tbl_Results.Rows.Find("ID")
pstrGameID = drCurrent.Item("ID")
Loop

Additionally, I have an Option Strict conflict with dumping the ID into pstrGameID. How can I get around this?
 
Another way to do this would be to use a DataView, and take advantage of the DataView's RowFilter property:

Dim dvResults As DataView

dvResults = DsResults1.tbl_Results.DefaultView

dvResults.RowFilter = "ID=" & txtEnterGameID.Text.Trim




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
That certainly was an improvement over what I had. It doesn't crash the program anymore, however, it still displays the first record in the table.

I'm guess that has something to do with this code...

pbndTemp = New Binding("text",DSResults1, "tbl_Results.Home")
lblHomeTeam.DataBindings.Add(pbndTemp)

 
You will need to change the binding to the DataView rather than to the DataSet. Then when the DataView is filtered, the correct values should show up in the bound controls.

pbndTemp = New Binding("text",dvResults, "Home")
lblHomeTeam.DataBindings.Add(pbndTemp)



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
I'm not sure why you're using a loop with the find method. That kinda defeats the purpose.

Get rid of the loop and change:

drCurrent = DsResults1.tbl_Results.Rows.Find("ID")

to:

drCurrent = DsResults1.tbl_Results.Rows.Find(txtEnterGameID.Text)

Then you can use the drCurrent (Row object) to populate your form. The reason you weren't getting results is because the find was looking for "ID" and not what the user typed in.
 
Jeb, your way worked perfectly. Changes to the data will not work now. I'm guessing I can't actually use this method because it is not using the dataset?

Macleod, I tried using your idea. I got an error saying the table didn't have a primary key. So, I went in the database and made ID the primary key. Still get the error. I wonder if I have to recreate the dataset?
 
Changes to the data in a DataView will propagate back to the underlying DataTable...I do this all the time in my apps.

How are you making the changes? How are you saving the changes to the database?



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
If you created the dataset programitcally, then you shouldn't have to. If you created it in the designer, then you'll need to delete it and recreate it so that the new schema will be there.

The only time the updates are automatic is if you're using a datareader. A dataset/dataview is disconnected. You have to set up your own update logic and connect it to the dataset. You need to set up an update statement into an SQLCommand object and then pass that through to the DataSet.UpdateCommand property.
 
Thanks Rick, but the way still gives me an error.

"Option strict on disallows implicit conversions from system.object to string
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top