Results 1 to 4 of 4

Thread: A casting problem [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width