ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
What am I doing wrong here? I'm assuming you can reuse the connection?
Thanks for any help!
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString()))
{
cn.Open();
// If we are reverting to an old type
if (pageAction == "revert")
{
debug.Text = "FLAG 1";
// Get the revert ID
int revertingID = int.Parse(Request.QueryString["revID"]);
bool rowsReturned = false;
debug.Text = "FLAG 2 - " + revertingID.ToString();
// Set all to 0
using (SqlCommand cmd = new SqlCommand("SELECT ID FROM tblSiteSettings WHERE ID = " + revertingID, cn))
{
// If it exists
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (rdr.Read())
{
rowsReturned = true;
}
rdr.Close();
}
debug.Text = "FLAG 3 - " + rowsReturned.ToString();
// Set new active and reset others
if (rowsReturned == true)
{
using (SqlCommand cmd = new SqlCommand("UPDATE tblSiteSettings SET isActive = 1 WHERE ID = " + revertingID, cn))
{
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd = new SqlCommand("UPDATE tblSiteSettings SET isActive = 0 WHERE ID <> " + revertingID, cn))
{
cmd.ExecuteNonQuery();
}
}
//debug.Text = "FLAG 4 - ";
}
Solution 2sqlConnection = new SqlConnection(connStr); openConnection(); If you now got an exception in openConnection() it is "eaten" by the empty catch handler and ExecuteNonQuery() finally complains about a not opened connection. If you want to know the reason, report it in the catch handlers.
ExecuteNonQuery: Use this operation to execute any arbitrary SQL statements in SQL Server if you do not want any result set to be returned. You can use this operation to create database objects or change data in a database by executing UPDATE, INSERT, or DELETE statements.
The connection's current state is closed. Save this question.
The ExecuteNonQuery method returns an integer that represents the number of rows affected by the statement or stored procedure that was executed. If multiple statements are executed, the value returned is the sum of the records affected by all of the statements executed.
Your problem is:
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
You should just call cmd.ExecuteReader()'
if you want to use the connection again prior to "getting rid" of it. If you want to get an understanding for what the CommandBehaviour.CloseConnection
part does/means then the documentation for SqlCommand.ExecuteReader is a good bet. There's also documentation to tell you what all the possible values of the CommandBehaviour enumeration are. Essentially CommandBehaviour.CloseConnection
does the following:
When the command is executed, the associated Connection object is closed when the associated DataReader object is closed.
If you have no special need to specify a CommandBehaviour, then either specify CommandBehaviour.Default
, or don't specify one at all. CommandBehaviour.Default is:
The query may return multiple result sets. Execution of the query may affect the database state. Default sets no CommandBehavior flags, so calling ExecuteReader(CommandBehavior.Default) is functionally equivalent to calling ExecuteReader().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With