|
-
Jun 7th, 2008, 06:41 AM
#1
Thread Starter
Frenzied Member
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????
Last edited by angelica; Jun 7th, 2008 at 07:57 AM.
-
Jun 7th, 2008, 06:51 AM
#2
Re: A casting problem
Have you specified a ValueMember for the ComboBox? If you expect to get an Integer from the SelectedValue then I can only assume that it's supposed to come from a column in a bound DataTable. You need to specify the name of that column as the ValueMember or the SelectedValue will simply be the whole item, which will be a DataRowView if you bound a DataTable.
Also, that code would not even compile if you had Option Strict On, so you must have it Off. I suggest that you remedy that immediately for the current project (project properties) and for all future projects (IDE options).
Last edited by jmcilhinney; Jun 7th, 2008 at 06:54 AM.
-
Jun 7th, 2008, 07:07 AM
#3
Thread Starter
Frenzied Member
Re: A casting problem
Ok JMC Solved. It was something with the properties of the combobox at design. dont know what but I deleted the combo and had a new.
Thanks loads
Last edited by angelica; Jun 7th, 2008 at 07:40 AM.
-
Jun 7th, 2008, 12:41 PM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|