Click here to Skip to main content
15,888,241 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
error converting data type varchar to numeric
C#
SqlCommand cmd = new SqlCommand("insert into patient_master values('" + Txt1.Text + "','" + Txt2.Text + "','" + TextBox1.Text + "','" + TextBox2.TextMode + "','" + TextBox3.TextMode + "','" + DropDownList1.Text + "','" + Textans.Text + "','" + email_id.Text + "','" + no.Text + "','" + txtarea1.TextMode + "')", sc);
            
sc.Open();

cmd.ExecuteNonQuery();

sc.close();

help me to solve this error
Posted
Updated 10-Mar-14 22:50pm
v2
Comments
SwarupDChavan 11-Mar-14 4:42am    
Hey this seems to be a very simple problem there seems to be a column of int datatype in your sql table and you are passing the string value to it,just convert that string e.g convert.toint32(Txt2.Text).
Kornfeld Eliyahu Peter 11-Mar-14 4:50am    
No. If string IS convertible SQL will do that...
SwarupDChavan 11-Mar-14 5:01am    
can you explain?
Kornfeld Eliyahu Peter 11-Mar-14 5:17am    
If the string contains '1344' SQL can cast it alone, you need not do that by yourself...
If it has 'abcc', even convert will not help - in fact it will fail...
JB0301 11-Mar-14 4:43am    
Can you tell us the specifics of your database, maybe one of the columns you are trying to insert into is not matching with the ones in its database..

It's obvious you pass some non numeric value to a numeric data column...

However you real problem is much worse that that! You are using string concatenation to create you query string! It's an invitation for SQL injection attack on your system!!!
Use parameterized queries! That will solve the injection problem and probably will point out the column in which you have the problem...
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx[^]
 
Share this answer
 
I think you insert a string value to int field , i.e ..in your insert query for int "+ text.text +" and for string --- '"+ textb.text + "'

or int.Parse (textb.text)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900