vb.net - 当尝试从sql数据库中选择数据时,combobox显示System.Data.DataRowView

时间:2011-06-30 16:12:19

标签: sql vb.net combobox

当我尝试从sql数据库中选择数据时,我得到了System.Data.DataRowView。这些是我的代码:

......
myCommand = 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"
..............

我得到了上述错误。请帮忙。

2 个答案:

答案 0 :(得分:2)

您的SelectCommand既不返回列LastName也不返回FirstName。您只返回一列(两列的串联)。

所以这应该有效:

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'

答案 1 :(得分:0)

您需要设置ValueMember以及DisplayMember