For exception-handling purposes, you wish to set cn.close() in the finally method. However, calling this function would fail if the connection is not opened, i.e. exception occurs when trying to open connection.
This is how your finally code can look like:
finally { if (cn.State != ConnectionState.Closed) cn.Close(); }There are other vague states of a connection (opening, fetching, etc) hence checking that it is not closed would be a safer indication that the connection can be closed.
No comments:
Post a Comment