Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to insert information into Mysql database, using this code...

C#
private void SubmitBtn_Click(object sender, EventArgs e)
        {


            SqlCommand cmd = new SqlCommand ("INSERT into [Materials Inventory] (Barcode, Material, Location, Quanitiy,  [Date Added]) VALUES ('" + textBox3.Text + "','" + textBox4.Text + "', '" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "' )",sqlcon);

            cmd.Parameters.AddWithValue("Barcode", textBox3);
            cmd.Parameters.AddWithValue("Material", textBox4);
            cmd.Parameters.AddWithValue("Location", textBox5);
            cmd.Parameters.AddWithValue("Quantity", textBox6);
            cmd.Parameters.AddWithValue("[Date Added]", textBox7);

            cmd.ExecuteNonQuery();


But I get this error when running the application
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Additional information: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

What I have tried:

Using a non perameterized query,
Rewritting code
Posted
Updated 12-Feb-18 17:48pm
v2
Comments
Richard Deeming 13-Feb-18 10:23am    
Once again:

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

One of these days, you might actually pay attention to this warning. Here's hoping that happens before your database is destroyed, or your users' private information leaked and sold on the dark web.

You need to open the connection and you don't show that you do.

And also, you want something along the lines of:

SQL
SqlCommand cmd = new SqlCommand ("INSERT into [Materials Inventory] (Barcode, Material, Location, Quanitiy,  [Date Added]) VALUES (@Barcode, @Material, @Location, @Quantity,  @[Date Added] )",sqlcon);


experiment from there.
 
Share this answer
 
v3
Comments
Maciej Los 13-Feb-18 1:54am    
5!
refer the inline comments, as an advice, Please use the proper naming convention to the controls so that it will be easy to find the correct control on the fly.

string barCode = textBox3.Text.Trim();
          string Material = textBox4.Text.Trim();
          string Location = textBox5.Text.Trim();
          string Quantity = textBox6.Text.Trim();
          string date = textBox7.Text.Trim();

          SqlCommand cmd = new SqlCommand("INSERT into [Materials Inventory] (Barcode, Material, Location, Quanitiy,  [Date Added])  " +
          " VALUES ( @Barcode,@Material,@Location,@Quantity,@Date)", sqlcon);  // add the parameter names
          cmd.Parameters.AddWithValue("@Barcode", barCode);  // parameter name with respect to the value
          cmd.Parameters.AddWithValue("@Material", Material);
          cmd.Parameters.AddWithValue("@Location", Location);
          cmd.Parameters.AddWithValue("@Quantity", Quantity);
          cmd.Parameters.AddWithValue("@Date", date);
          sqlcon.Open();  //open the connection
          cmd.ExecuteNonQuery();
          sqlcon.Close();  // close the connection
 
Share this answer
 
Comments
Maciej Los 13-Feb-18 1:54am    
5ed!
Karthik_Mahalingam 13-Feb-18 1:58am    
Thank you Maciej

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