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

后端 未结 5 1200
情书的邮戳
情书的邮戳 2021-02-07 11:55

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

5条回答
  •  耶瑟儿~
    2021-02-07 12:02

    Right here, your SqlDataReader will close the connection when it completes:

    // 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(); 
    } 
    

    Later, the "Set new active and reset others" section will fail, because the connection is closed.

提交回复
热议问题