Results 1 to 2 of 2

Thread: why doesnt this work?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    12

    why doesnt this work?

    Dim addcards As String
    Dim cardnum As Integer
    Dim Peopleid As Integer
    cardnum = TextBox4.Text
    Peopleid = TextBox5.Text
    addcards = "INSERT INTO Cards (Card_Number, People_ID) VALUES (" + cardnum + ", " + peopleid + ")"
    OleDbCommand3.CommandText = addcards

    but this does

    Dim addcards As String
    Dim cardnum As Integer
    Dim Peopleid As Integer
    cardnum = TextBox4.Text
    Peopleid = TextBox5.Text
    addcards = "INSERT INTO Cards (Card_Number, People_ID) VALUES (" + "1014" + ", " + "1528" + ")"
    OleDbCommand3.CommandText = addcards

    I keep getting the error of:

    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

    Additional information: Cast from string "INSERT INTO Cards (Card_Number, " to type 'Double' is not valid.

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    That's the bad habit of VB6. You cant just add a number to a string, you have to convert it first.
    for the first part:
    VB Code:
    1. Dim addcards As String
    2.         Dim cardnum As Integer
    3.         Dim Peopleid As Integer
    4.         cardnum = CInt(TextBox4.Text)   ' cardNum is an integer. Use the CInt() function to
    5.                                         ' convert the string to integer.
    6.         Peopleid = CInt(TextBox5.Text)  ' same here
    Umm you just have to make sure that the textboxes contain numbers. You can use the Val() function too. It will only return the part of the string that contains numbers. Then you have to convert that to integer. (ie: Cint(Val(someString)) )



    for the last part, you can either use the ToString method to convert your numbers to integer, or you can use the CStr() function. Also I rather use the & symbol rather than + when dealing with strings
    VB Code:
    1. addcards = "INSERT INTO Cards (Card_Number, People_ID) VALUES (" & cardnum.ToString() & ", " + Peopleid.ToString() & ")"


    HTH
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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