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

后端 未结 5 1117
情书的邮戳
情书的邮戳 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:20

    It appears you are doing a read before doing your ExecuteNonQuery. In your first call to SqlCommand (for the SELECT), you are closing the connection after the read is complete.

    SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    

    Remove the command behavior, you should be good to go, or re-open the connection in your next if statement.

    This

    SqlDataReader rdr = cmd.ExecuteReader();
    

    Or this

    if (rowsReturned == true){
       cn.open();
    

提交回复
热议问题