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."
- Changed Type Andrea MontanariModerator Monday, July 09, 2012 10:42 PM
All Replies
-
Monday, July 09, 2012 3:00 AMModeratorCheck 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 PMthere are no triggers on that table...
-
Monday, July 09, 2012 9:32 PMModeratorIn 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 AMModerator
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- Proposed As Answer by Shahfaisal Muhammed Tuesday, July 10, 2012 12:34 AM
-
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.
- Edited by Stefan Zvonar Tuesday, July 10, 2012 8:06 PM
-
Wednesday, July 11, 2012 6:19 PMstill 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 PMModerator
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 Suband the corresponding database table is
PropertyId int
PropertyType varchar(50)
Postedby varchar(50)
Location varchar(50)
City varchar(50)
Size varchar(50)
Price money
ContactNo numeric(18, 0) -
Thursday, July 12, 2012 3:24 PMModerator
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 PMModeratorMay 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

