vb.net - combobox displays System.Data.DataRowView when trying to select the data from sql database
I got the System.Data.DataRowView when I tried to select the data from sql database. These are my code:
......
myC开发者_运维百科ommand = New SqlCommand("SELECT FirstName +' '+ LastName FROM tblVisitor", myConnection)
myAdapter = New SqlDataAdapter(myCommand)
myAdapter.Fill(myDataSet, "tblVisitor")
cboName.DataSource = myDataSet.Tables(0)
cboName.DisplayMember = "FirstName"
cboName.DisplayMember = "LastName"
cboName.ValueMember = "FirstName"
cboName.ValueMember = "LastName"
..............
and I got the above error. Please help.
Your SelectCommand does neither return a column LastName
nor FirstName
. You are only returning one column(a concatenation of both columns).
So this should work:
myCommand = New SqlCommand("SELECT VisitorID, LastName, FirstName, (FirstName +' '+ LastName)As FullName FROM tblVisitor", myConnection)
myAdapter = New SqlDataAdapter(myCommand)
myAdapter.Fill(myDataSet, "tblVisitor")
cboName.DataSource = myDataSet.Tables(0)
cboName.DisplayMember = "FullName"
cboName.ValueMember = "VisitorID" 'change to the correct pk-column'
You need to set ValueMember
as well as DisplayMember
.
精彩评论