Reputation: 360
I am filling a combo box with dataset:
ComboName.Items.Clear()
DS = New DataSet
DS.Tables.Add(New DataTable("DEMAND"))
DA = New SqlDataAdapter("select * from DEMAND", CON)
DA.Fill(DS, "DEMAND")
ComboName.DataSource = DS.Tables(0)
ComboName.DisplayMember = "name"
ComboName.ValueMember = "id"
Now I want to retrieve address with the help of ValueMember
:
Private Sub ComboName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboName.SelectedIndexChanged
Dim I as Integer = CType(ComboName.SelectedValue.ToString(), Integer)
CMD = New SqlCommand("select * from DEMAND where id=" & I, CON)
RDR = CMD.ExecuteReader()
If RDR.Read Then
TxtAdrs.Text = RDR!adrs
End If
End Sub
But here i am getting an error :
Conversion from string
"System.Data.DataRowView"
to type'Integer'
is not valid.
Can anybody tell what i am doing wrong?
Upvotes: 0
Views: 1750
Reputation: 1
try to declare it as an object instead of integer.
dim selecteditem as object
selecteditem = comboname.selectedvalue
Upvotes: 0
Reputation: 13882
your ComboName.SelectedValue.ToString()
returns the name of the class
"System.Data.DataRowView"
which is the default behaviour of ToString()
try getting a specific column from the DataRowView.
SelectedItem is the data object that is bound to the ComboBox's data source, which in this case is DataRowView.
You need to cast SelectedItem to DataRowView, then retrieve the appropriate value from it.
You can do this as follows:
DataRowView oDataRowView = ComboName.SelectedItem as DataRowView;
string sValue = "";
if (oDataRowView != null) {
sValue = oDataRowView.Row["YourFieldName"] as string;
}
Upvotes: 2
Reputation: 9460
I guess the error is in this line
Dim I as Integer = CType(ComboName.SelectedValue.ToString(), Integer)
Use like this
Dim I as Integer = CType(ComboName.SelectedValue, Integer)
Hope this helps
Upvotes: 0