A casting problem [RESOLVED]
Hey,
I have a compilation problem here and I cant figure it out. Can some one please pinpoint my mistake??
Error:
System.InvalidCastException was unhandled by user code
Message="Conversion from type 'DataRowView' to type 'Integer' is not valid."
Many thanks
----------------------------------------------------------------------
Code:
Private Sub CurrentBalanceShowD()
Dim checkBook As Integer
checkBook = cboBookD.SelectedValue
Dim cmd As SqlCommand
Try
MyConn.Open() 'opening the connection
cmd = New SqlCommand("Select stockBalance from tb_bookbalance where bookFID = '" & checkBook & "'", MyConn) 'executing the command and assigning it to connection
txtcurrentD.Text = Convert.ToString(cmd.ExecuteScalar()) 'displaying the data from the table
MyConn.Close()
Catch ex As Exception
MsgBox(ex.Message)
MyConn.Close()
End Try
End Sub
the red line is where the error is thrown. cboBookD.selectedvalue is an integer. so what the prob????
Re: A casting problem [RESOLVED]
I already told you what. You hadn't set the ValueMember properly. If you don't tell the ComboBox which member of the item to get the value from then it will just get the item itself, which is exactly what was happening.