Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using this code to insert into database :
C#
SqlConnection conn = new SqlConnection("Server=XP-PC\\SQLEXPRESS;Database=Warehouse;Trusted_Connection=True");
            string sql = "insert into warehouse_data (Barcode,Product_Name,Amount) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox5.Text + "')";
            SqlCommand cmd = new SqlCommand(sql,conn);
            
            try
            {
                conn.Open();
                cmd.ExecuteScalar();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            conn.Close();



but nothing happens
why ?
and this is the right code or not ?
Posted
Updated 18-Apr-11 0:30am
v2
Comments
Oshtri Deka 18-Apr-11 6:49am    
Which is the type of Amount column? Integer or some kind of char type?

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 :

C#
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:
C#
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.
 
Share this answer
 
v4
Comments
Mohammed Ahmed Gouda 18-Apr-11 6:34am    
i used it but nothing happens also :D
Tarun.K.S 18-Apr-11 6:37am    
Lol! then lemme take another close look at it!
Mohammed Ahmed Gouda 18-Apr-11 6:44am    
when i am running the sql server management studio i have to run it as administrator
Tarun.K.S 18-Apr-11 6:55am    
But you can also do queries in Visual Studio itself. Even I have to run it as Admin! :D
Mohammed Ahmed Gouda 18-Apr-11 6:53am    
text box are not empty , no exception appears , data types are (nvarchar(50),nvarchar(50)and int)
As Tarun says ,
Use ExecuteNonQuery instead of ExecuteScalar.

i can see only this error on your code and yes plz take care of notes written by tarun.

try this ,
SqlCommand comm = new SqlCommand("INSERT INTO desg VALUES (@txtBox1, @txtBox2, @txtBox3)", connection);

comm.Parameters.AddWithValue("@txtBox1", textBox1.Text.Trim());
comm.Parameters.AddWithValue("@txtBox2", textBox2.Text.Trim());
comm.Parameters.AddWithValue("@txtBox3", textBox3.Text.Trim());

C#
try
            {
                conn.Open();
                comm.ExecuteNonQuery ();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            conn.Close();


Refer this link it will definitely help you, [^]
 
Share this answer
 
v4
Comments
Mohammed Ahmed Gouda 18-Apr-11 6:49am    
i used ExecuteNonQuery but nothing happens

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