Results 1 to 5 of 5

Thread: Why Int DataType field is holding the string value without any error

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Why Int DataType field is holding the string value without any error

    Hi. I have a Table in SQL Server 2005 Standard, in which StID is a Primary Key, set to int DataType. When I insert the data from Front End using VB.Net, so If I insert the String Value deliberately and not the Integer. So.

    My question is that why it doesn't give any error, for sending string to DataType Int? Please guide me?

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Why Int DataType field is holding the string value without any error

    There's an internal data type conversion from String to Integer
    Try inserting the string value but with letters or special characters and see the result
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Why Int DataType field is holding the string value without any error

    so i insert the String Value
    Not really. I think you're saying you build up sql as a string so therefore it's a string you're inserting right? Something like this:-
    Code:
    sql = "Insert Into MyTable (stID) Values (" & anInteger.ToString & ")"
    which looks like you're passing a string in. But what that will produce is something like:-
    Code:
    Insert into MyTable (stID) Values (10)
    you'll notice that the 10 doesn't have quotes in it which means that, in SQL world, it's a number. If you passed in a string you'd have:-
    Code:
    sql = "Insert Into MyTable (stID) Values ('" & anInteger.ToString & "')"
    which would produce
    Code:
    Insert into MyTable (stID) Values ('10')
    Even then, as jggtz said, SQL would simply convert it as long as that's possible.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: Why Int DataType field is holding the string value without any error

    @ Jggtz and FunkyDexter
    I updated my Question, Post # 1.


    I didn't use any such converting in my code. So does it mean that SQL Server does this automatically?

  5. #5
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Why Int DataType field is holding the string value without any error

    So does it mean that SQL Server does this automatically?
    It depends on how you're sending the value. If you build up a sql string and don't wrap it in quotes then, as far as SQL Server is concerned it's already an integer. If you do wrap it in quotes then sql server converts it. IF you use an ADO parameter then VB.Net is automatically converting it (assuming you have option strict off). I can't think of any other scenario to cover. Post some example code and I might be able to give a better explanation.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

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