简体   繁体   中英

Exception Error in trying to update database using asp.net

How come I'm getting this error while trying to update my database?

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

Here is the code:

cmd1 = new SqlCommand("UPDATE [guitarBrands] SET type = @type, name = @name, image = @image WHERE id = @id", con1);

con1.Open();

cmd1.Parameters.Add(new SqlParameter("type", newType.Text));
cmd1.Parameters.Add(new SqlParameter("name", newName.Text));
cmd1.Parameters.Add(new SqlParameter("image", newImage.Text));
cmd1.Parameters.Add(new SqlParameter("id", id));

cmd1.ExecuteNonQuery();

con1.Close();
cmd1.Parameters.Clear();

cmd = new SqlCommand("UPDATE [guitarItems] SET brand = @brand WHERE id = @id", con1);
con.Open();

cmd.Parameters.Add(new SqlParameter("brand", newName.Text));
cmd.Parameters.Add(new SqlParameter("id", id));

cmd.ExecuteNonQuery();
con.Close();

cmd.Parameters.Clear();

To avoid these sort of issues, it is recommended you utilize the tower of power .

using(var connection = new SqlConnection(dbConnection))
{
     connection.Open();
     using(var command = new SqlCommand(query, connection)
     {

     }

     using(var command = new SqlCommand(query, connection)
     {

     }
}

So the beauty of the tower of power, the using block will implement via within the given code block. So this will make it clear, that both these commands are utilizing the same connection from the using. Also, once the code is out of scope it will implement the IDispose , which will call the garbage collector to free up your resources.

Also, should you choose. The SqlCommand , accepts a parameter array. So if you utilize a method call, you could simply do:

public static GetExample(string query, params SqlParameter[] parameters)
{
     using(var connection = new SqlConnection("YourDbConnection"))
     using(var command = new SqlCommand("YourQuery", connection))
     {
          connection.Open();
          if(parameters != null)
               if(parameters.Any())
                    command.Parameters.Add(parameters);

          command.ExecuteNonQuery();
     }
}

I can't recall if the collection is a add, add range, or concat. But either way the option exist.

cmd = new SqlCommand("UPDATE [guitarItems] SET brand = @brand WHERE id = @id", con1);

将此行替换为此

cmd = new SqlCommand("UPDATE [guitarItems] SET brand = @brand WHERE id = @id", con);

In your code you use Connexion "con1" in both Commands "cmd1" and "cmd". It is OK to use just one connexion for both commands but then you should leave the connexion open until both command are executed. In your case you choose to use a new connexion "con" for the second command but you reopen "con1". So you get an error because "con" is never opened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM