Polling

OK Andrew Seven has been kind to put me on the right track for the DAL and General Network Error issues.

He recommend to use more Dataset than Datareader and yes he's right because you don't have to think closing your connections with Dataset.

Andrew mentioned in one of his post a great article The .NET Connection Pool Lifeguard written by William Vaughn.

I found there a lot of great ideas. Read it !

I was able to fix one of the method causing errors. Something anyway I don't understand is that the connection was closed properly in  my code.

But now it works well by just doing one change, closing the connection in the ExecuteReader property. Weird !

And I still keep the connection close at the end without causing any error.

So now instead of dtr = cmdSelect.ExecuteReader() I have

dtr = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection).

And still dtr.close() and Myconnection.close() at the end.

 

5 Comments

  • And what about dtr.Close() at the end? This is the time when the connection is closed with CommandBehavior.CloseConnection

  • Ooops not in my post but yes of course I do a dtr.close() at the end :-)

  • We use the following approach to make sure that the reader is closed after we are finished with it. I'm not copying and pasting this, so apologies if the syntax is a bit off:



    SqlDataReader rdr = null;



    using(rdr = cmd.ExecuteReader("connstring", CommandType.StoredProcedure . . .))

    {

    // process your reader

    // no need to close

    // as this will automatically

    // "dispose" and close the reader.

    // It is like a built in "finally" block

    // for classes that are IDisposable.

    // This gets called whether it completes

    // successfully or exceptions out.

    }

  • Paschal,

    The reason why you get no errors when calling MyConnection.Close() several times is in MSDN: "An application can call Close more than one time. No exception is generated".

  • Hehe... a day late and a dollar short on my comment in the other thread... glad to know my recollection about the CommandBahavior was correct. I found that closing was not enough, a call to Dispose() (which also happens to close for you, so you only have to remember to do the Dispose) does the trick.

Comments have been disabled for this content.