Proposed Answer SQL Server 2008

  • Sunday, July 08, 2012 10:16 PM
     
     

    hi..

    I'm trying to insert values into a sql database table, the values are getting inserted but i get an error "Error converting data type varchar to numeric."

All Replies

  • Monday, July 09, 2012 3:00 AM
    Moderator
     
     
    Check if you have triggers on that table.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

  • Monday, July 09, 2012 8:08 PM
     
     
    there are no triggers on that table...
  • Monday, July 09, 2012 9:32 PM
    Moderator
     
     
    In this case please post your code. It's hard to debug something we don't see.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

  • Monday, July 09, 2012 10:50 PM
     
     

    cmd.CommandText = (((((("insert into Property_table(PropertyType,Postedby,Location,City,Size,Price,ContactNo) values ('" + DropDownList1.Text & "','" & postby & "','") + TextBox1.Text & "','") + TextBox2.Text & "','") + TextBox3.Text & "','") + TextBox4.Text & "','") + TextBox5.Text & "')")
            Dim ok As Integer = cmd.ExecuteNonQuery()

    Table

    PropertyId    int   
    Postedby    varchar(50)   

    PropertyType    varchar(50)   
    Location    varchar(50)  
    City    varchar(50)   
    Size    varchar(50)   
    Price    money   
    ContactNo    numeric(18, 0)   
          

           
  • Tuesday, July 10, 2012 12:27 AM
    Moderator
     
     Proposed Answer
    Don't use direct string concatenation. Always use parameters. If you define your parameters correctly for the insert, I bet the problem will be fixed.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

  • Tuesday, July 10, 2012 11:56 AM
     
     

    As stated already, you should really use typed parameters for your queries.

    Then, try using Convert.ToDouble(TextBox3.Text) and Convert.ToDouble(TextBox4.Text) for the value of the numeric parameters.


  • Wednesday, July 11, 2012 6:19 PM
     
     
    still i'm getting this error:Conversion from string "insert into Property_table(Prope" to type 'Double' is not valid
  • Wednesday, July 11, 2012 7:15 PM
    Moderator
     
     

    What is your current code? Also, for the Insert command make sure to use this format

    insert into Property_table (field1, field2, field3) values (@Param1, @Param2, @Param3)


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

  • Thursday, July 12, 2012 2:47 AM
     
     

        Dim postby As String = Session("user").ToString()
            Dim con As New SqlConnection(constring)
            Dim cmd As New SqlCommand()
            cmd.Connection = con
            con.Open()
            cmd.CommandType = CommandType.Text
            cmd.CommandText = (((((("insert into Property_table(PropertyType,Postedby,Location,City,Size,Price,ContactNo) values ('" + DropDownList1.Text & "','" & postby & "','") + TextBox1.Text & "','") + TextBox2.Text & "','") + TextBox3.Text & "','") + TextBox4.Text & "','") + TextBox5.Text & "')")
            Dim ok As Integer = cmd.ExecuteNonQuery() Getting an error here..

        End Sub

    and the corresponding database table is

    PropertyId    int   
    Postedby    varchar(50)   

    PropertyType    varchar(50)   
    Location    varchar(50)  
    City    varchar(50)   
    Size    varchar(50)   
    Price    money   
    ContactNo    numeric(18, 0)   

          
  • Thursday, July 12, 2012 3:24 PM
    Moderator
     
     

    Well, you disregarded my earlier advice. I suggested to switch to parameters, so your CommandText should be

    insert into Property_Table (PropertyType, PostedBy, Location, City, Size, Price, ContactNo) 

    values (@PropertyType, @PostedBy, @Location, @City, @Size, @Price, @ContactNo)

    It does take a little bit more lines of code to set up, but there are many benefits of doing it this way and never doing it your way, the most important of them being preventing SQL injection attacks.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

  • Friday, July 13, 2012 10:10 PM
     
     

    now this problem got resolved and when i insert values into the table, two rows are getting inserted every time..

    what might be the problem?

  • Friday, July 13, 2012 10:16 PM
    Moderator
     
     
    May be you're calling ExecuteNonQuery method twice in your code? 

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog