Cannot get ID from Combobox Value to Insert into Db

motti100

Member
Joined
Mar 6, 2005
Messages
19
Programming Experience
1-3
:)I am using VB.NET express and MS Access.

1. I have a Table named EVENTthat contains:

1. RaceID (1..14)
2. RaceName (50 yard, 100 yard, etc)

I populate my cboRace with the Raceame information.
User can select the RaceName from the list and then submit it.

OnSubmit, I am trying to update RaceID in another table named RESULT
containing RaceID, EventID, etc

I have tried setting Databinding on the cboRace and it still does not work.

How can this be done?
 
In future, don't just say "I tried and it didn't work". Please show/tell us exactly what you did and exactly what happened.

You need to set the ValueMember of the ComboBox to the name of the column/property you want returned and then you get the value that corresponds to the selection from the SelectedValue property.
 
I apologize for being so vague:

Here is the code that calls the DB and populates the combo box.


cmd = New OleDb.OleDbCommand("select * from Event", cn) ==> EVENT has RaceID and RaceType

Try
UnitIDReader = cmd.ExecuteReader

If UnitIDReader.HasRows Then

While UnitIDReader.Read()
race.ValueMember = UnitIDReader("RaceType")
race.Items.Add(UnitIDReader("RaceType").ToString)
End While

End If
Catch
End Try

combobox is populated with Race information RaceType (String = 50 meter backstroke)

I have attached a screenshot of the control

1. When I do the binding, the control comes up empty
2. When I do not do binding, I get the values, but Selected Value is always NULL.

Thank you. Mate
 

Attachments

  • attachment.JPG
    attachment.JPG
    114.7 KB · Views: 30
combo.datasource = event
displaymember = racename
valuemember = raceid
selectedvalue databinding is bound to result.raceid

remember: fill the event table
remember: do not bind the combo's text property
remember: there is NO NEED for a datarelation between result and event
remember: youre NOT supposed to be using a data reader and then adding the items to the combo; youre supposed to be filling the event datatable and the result datatable! replace all this junk:
VB.NET:
cmd = New OleDb.OleDbCommand("select * from Event", cn) ==> EVENT has RaceID and RaceType

Try
UnitIDReader = cmd.ExecuteReader

If UnitIDReader.HasRows Then

While UnitIDReader.Read()
race.ValueMember = UnitIDReader("RaceType")
race.Items.Add(UnitIDReader("RaceType").ToString)
End While

End If
Catch
End Try

With:
VB.NET:
Me.EventTableAdapter.Fill(swimdataset1.Events)
 
Back
Top