Results 1 to 9 of 9

Thread: SQL statement

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    SQL statement

    What is wrong with this...

    ' define and get the Customers Master
    Dim cmdSQL_C As New SqlCommand("Select Top 1 * from Customers where CustomerID = @CustomerID")
    cmdSQL_C.Connection = conSQL
    cmdSQL_C.Parameters.Add("@CustomerID", System.Data.SqlDbType.Int)
    cmdSQL_C.Parameters("@CustomerID").Value = TextBox3.Text 'has a valid key value
    conSQL.Open()
    Rdr = cmdSQL_C.ExecuteReader()
    Rdr.Read()

    At the .ExecuteReader I get the error :

    An unhandled exception of type 'System.FormatException' occurred in system.data.dll

    Additional information: Input string was not in a correct format


    The key to Customers is nchar(5).

    When I use this same set of code for a key that numeric .. all is fine... What's up???

    gollnick
    William E Gollnick

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Its probably just a casting problem try changing this line:
    VB Code:
    1. cmdSQL_C.Parameters("@CustomerID").Value = Interger.Parse(TextBox3.Text)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260
    Tried that.. Does not like "Interger" ... If this is a character key value .. should it be a numeric assignment?
    William E Gollnick

  4. #4
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Lightbulb

    I think Edneeis just made a typo. Did you try Integer? (without the extra "r")
    ~Peter


  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yeah that was my bad I meant Integer my fingers just got going a lil TOO fast and I threw in an extra r.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260
    Yupper .. Integer....... still .. with a charactor key?
    William E Gollnick

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If its not a numeric assignment then you set up the parameter wrong. This line sets the Parameter to an integer type:
    cmdSQL_C.Parameters.Add("@CustomerID", System.Data.SqlDbType.Int)

    So if it shouldn't be an integer then you'll need to change the parameter declaration to whatever it should really be.

    What do you mean by character key? What is the value you are passing in?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260
    Duh .....

    cmdSQL_C.Parameters.Add("@CustomerID", System.Data.SqlDbType.NChar) .... works.....

    Thanks to all....
    William E Gollnick

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Really there is no need for the parameter you can include it right in the SQL statement since they are created at the same time:
    VB Code:
    1. Dim sql_str As String=String.Format("Select Top 1 * from Customers where CustomerID = '{0}'",TextBox3.Text)
    2. Dim cmdSQL_C As New SqlCommand(sql_str)
    3. conSQL.Open()

    Although if the value is numeric then remove the ' and ' around the {0}.

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