Tepken Vannkorn Tepken Vannkorn - 1 month ago 14
Vb.net Question

vb.net - combobox error:'Cannot bind to the new value member. Parameter name: value'

I actually have three combobox bind to diffrent table. and I got such the error in my title. Bellow are my codes:

myCommand = New SqlCommand("SELECT (FirstName +' '+ LastName) AS FullName FROM tblVisitor", myConnection)
myAdapter = New SqlDataAdapter(myCommand)
myAdapter.Fill(myDataSet, "tblVisitor")
cboVisitor.DataSource = myDataSet.Tables(0)
cboVisitor.DisplayMember = "FullName"
cboVisitor.ValueMember = "VisitorID"

'my another combobox
myCommand = New SqlCommand("SELECT BookCode FROM tblBook", myConnection)
myAdapter = New SqlDataAdapter(myCommand)
myAdapter.Fill(myDataSet, "tblBook")
cboBookCode.DataSource = myDataSet.Tables(1)
cboBookCode.DisplayMember = "BookCode"
cboBookCode.ValueMember = "BookCode" 'where BookCode is the pk-column of the tblBook

'my another combobox
myCommand = New SqlCommand("SELECT (FirstName +' '+ LastName) AS StaffFullName FROM tblStaff", myConnection)
myAdapter = New SqlDataAdapter(myCommand)
myAdapter.Fill(myDataSet, "tblStaff")
cboStaff.DataSource = myDataSet.Tables(2)
cboStaff.DisplayMember = "StaffFullName"
cboStaff.ValueMember = "StaffID"


Through this code, i got only the first comboboxthat display the query result, but the other two do not, and display the message 'Cannot bind to the new value member. Parameter name: value'. Please help...

Answer

Try this:

myCommand = New SqlCommand("SELECT (FirstName +' '+ LastName) AS FullName FROM tblVisitor;SELECT BookCode FROM tblBook;SELECT (FirstName +' '+ LastName) AS StaffFullName FROM tblStaff", myConnection)
   myAdapter = New SqlDataAdapter(myCommand)
   myAdapter.Fill(myDataSet)

   cboVisitor.DataSource = myDataSet.Tables(0)
   cboVisitor.DisplayMember = "FullName"
   cboVisitor.ValueMember = "VisitorID"

   'my another combobox
   cboBookCode.DataSource = myDataSet.Tables(1)
   cboBookCode.DisplayMember = "BookCode"
   cboBookCode.ValueMember = "BookCode"  'where BookCode is the pk-column of the tblBook

   'my another combobox
   cboStaff.DataSource = myDataSet.Tables(2)
   cboStaff.DisplayMember = "StaffFullName"
   cboStaff.ValueMember = "StaffID"

You were confusing things by calling .Fill() multiple times for the same dataset. This will also perform better by only making one round-trip to the database.