开发者

vb.net - combobox displays System.Data.DataRowView when trying to select the data from sql database

开发者 https://www.devze.com 2023-03-16 07:03 出处:网络
I got the System.Data.DataRowView when I tried to select the data from sql database. These are my code:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消