Use
ExecuteNonQuery
instead of
ExecuteScalar
.
I would recommend you to read this article :
http://blogs.x2line.com/al/archive/2007/05/01/3049.aspx[
^]
UPDATE 1:
You can give more proper spacing :
string sql = "INSERT INTO WAREHOUSE_DATA(BarCode, Product_Name, Amount) VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox5.Text + "')";
Some notes :
- Make sure that the textboxes are not empty.
- Use SqlParameters instead directly assigning textbox.text in the query.
- Any Exception?
- How about checking the data types of the columns?
UPDATE 2:
Comment from OP :
text box are not empty , no exception appears , data types are (nvarchar(50),nvarchar(50)and int)
I think there is a problem in your data type. You are assigning the field of type
int
to
String
.
Now change your query to this:
string sql = "INSERT INTO WAREHOUSE_DATA(BarCode, Product_Name, Amount) VALUES('" + textBox1.Text + "','" + textBox2.Text + "', int.Parse(textBox5.Text))";
;
Now it should work. And do use
ExecuteNonQuery
as
ExecuteScalar
will return only one record.