CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2003
    Posts
    48

    sql/vb.net problem

    Whenever I try to programatically execute:

    "INSERT INTO tblCampRegistrations(fldRWUID, fldRegionID, fldYear, fldRWUIDDiscount) VALUES(" & strBlah & " , " & cmbRegion.SelectedIndex + 1 & " , " & cmbYear.SelectedItem & ")"

    it throws an exception, the .tostring is:


    System.InvalidCastException: Cast from type 'DataRowView' to type 'String' is not valid.
    at Microsoft.VisualBasic.CompilerServices.StringType.FromObject(Object Value)
    at Microsoft.VisualBasic.CompilerServices.ObjectType.StrCatObj(Object vLeft, Object vRight)
    at RWUDatabase.frmRegister.cmdOK_Click(Object sender, EventArgs e)



    I've taken out every single SQL line that's not required and I still get this, and the datatypes all seem to be ok. Any suggestions?

    -Jacob the n00b

  2. #2
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    I see that you want to insert 4 fields
    fldRWUID,
    fldRegionID,
    fldYear,
    fldRWUIDDiscount

    But you give only 3 values
    strBlah
    cmbRegion.SelectedIndex + 1
    cmbYear.SelectedItem

    The number of fields has to match the number of values.

  3. #3
    Join Date
    Jan 2003
    Posts
    48

    last

    Observant, but not the cause of the error. Now the code is:

    "INSERT INTO tblCampRegistrations(fldRWUID, fldRegionID, fldYear) " & _
    "VALUES(" & strBlah & " , " & _
    cmbRegion.SelectedIndex + 1 & " , " & _ cmbYear.SelectedItem & ")"

  4. #4
    Join Date
    Jan 2000
    Posts
    264
    When specifying values in an INSERT statement, if the value is a string, you need to enclose it quotes. Also, if you are pulling values from a combobox, then you need to use the .text property to retrieve the text that is selected.

    If fldRWUID is a string use quotes, if not - dont. I cant tell but your variable strBlah tells me its a string.

    Try this:
    Code:
    Dim sSQL as string
    sSQL = "INSERT INTO tblCampRegistrations (fldRWUID, fldRegionID, fldYear) " _
     & "VALUES ('" & strBlah & "', " & cmbRegion.SelectedIndex + 1 _
     & ", " & cmbYear.SelectedItem.Text & ")"
    HTH,
    Greg

  5. #5
    Join Date
    Jan 2003
    Posts
    48

    last

    Yeah that did it. I checked out what the .selectedmember actually was. I'm an idiot. I got it working last night though, thanks for all the help y'all

    -Jacob

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

Featured