hi you can try this code:
Implement the IDisposable interface class ClassName:IDisposable { ...... IDisposable.Dispose() { yourConnection.Close(); } } or you can use string Connectionstring = ""; //TO DO precise your connection string between cotes using(xxxConnection cn = new Connection(ConnectionString)) { //TO DO :Put your code here } Once the connection is not used it will be closed automatically