Click here to Skip to main content
15,905,593 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi,

I'm executing the following the code using datagrid view. but getting the error "Couldn't store d in CustomerName Column. Expected type is Int32." plz hlp me

thanx in advance..

C#
private void Done_button_Click(object sender, EventArgs e)
       {
           OdbcConnection con;
           OdbcCommand cmd;
           con = new OdbcConnection(@"Dsn=chaitudi;dbq=C:\project\Distributor.accdb;driverid=25;fil=MS Access;maxbuffersize=2048;pagetimeout=5;uid=admin");
           con.Open();
           cmd = new OdbcCommand("insert into Sell(ProductName,CustomerName,Quantity,ItemRate,Mrp,Vat,VatAmount,discount,DiscountAmount,netAmount)values('"+Product_name.Text+"','"+Cust_name.Text+"',"+Quan_tity.Text+","+Item_Rate.Text+","+M_rp.Text+","+V_at.Text+","+Vat_Amount.Text+","+Dis_count.Text+","+Discount_Amount.Text+","+net_Amount.Text+")", con);
           cmd.ExecuteNonQuery();
           MessageBox.Show("Done");
           con.Close();
       }
Posted
Updated 21-Feb-13 18:46pm
v2

'"+Cust_name.Text+"' is a text entry that you are storing into your database.
However, the error message suggests your table expects to store an integer type.

If your Cust.Text is a valid integer, remove the ' from the query.
If it is text, you need to fix your code to pass an integer into the insert query.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Feb-13 0:52am    
Useful bit of explanation, a 5.
And a great explanation you can find in the comics. Please locate the first link right after [EDIT], in Solution 2.
(Credit to Espen who found it...)
—SA
Abhinav S 22-Feb-13 1:13am    
Thanks.
chaitali chandane 22-Feb-13 1:01am    
I hv tried this bt nt working.........
Naturally, if you input something in a text box, you can always enter some data which is not parsed as int. Just check your query under debugger, and you will see what's wrong.

But you are doing it wrong. You should never create the query by concatenation of strings with strings obtained from UI. At least, you should parse data to integer (int.Parse or int.TryParse) separately. But you cannot do this because you are not using parametrized statements. You should use them, and use typed data, not strings. Please see:
http://msdn.microsoft.com/en-us/library/ms254953.aspx[^].

Not only it's right thing to do, but failure to do so open up the doors to a well-known exploit called SQL injection:
http://en.wikipedia.org/wiki/SQL_injection[^].

So, you should never do what you are doing. In the article referenced above, importance of parametrized statements is explained. Always use them.

[EDIT]

This is a great explanation of the essence of things: http://xkcd.com/327/[^].

Please also see my past answers:
hi name is not displaying in name?[^],
EROR IN UPATE in com.ExecuteNonQuery();[^].

—SA
 
Share this answer
 
v2
Comments
Abhinav S 22-Feb-13 1:13am    
My 5.
Sergey Alexandrovich Kryukov 22-Feb-13 1:34am    
Thank you, Abhinav.
—SA
Set "0" to all these before saving Quan_tity,Item_Rate,M_rp,V_at,Vat_Amount,Dis_count,Discount_Amount,net_Amount if they are empty , otherwise use parameterise query.

hopes this can solve your probles.
 
Share this answer
 
v3
Hi
Convert the values where you have integers or double e.g

C#
Convert.ToInt32(Vat_Amount.Text)


OR

C#
Convert.ToDouble(Vat_Amount.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