Close Connection
-
This should be really simple, but im having one of those mornings... Im trying to close my connection, but its staying open if (Border.BLL.Inventory.SqlConnect.GetConnection().State == ConnectionState.Open) { Border.BLL.Inventory.SqlConnect.CloseConnection(); } //BLL public static SqlCeConnection CloseConnection() { return DAL.Helpers.SQLconnect.CloseConnection(); } //DAL public static SqlCeConnection GetConnection() { SqlCeConnection conn = new SqlCeConnection(); try { conn.ConnectionString = Helpers.ConnectionCommand.ConnectionString; conn.Open(); return conn; } catch(Exception e) { if (conn.State == ConnectionState.Open) { conn.Close(); } Border.Model.Helpers.FileWriter.Write(e.ToString(), true); } return conn; } public static SqlCeConnection CloseConnection() { try { if (GetConnection() != null) { if (GetConnection().State == ConnectionState.Open) { GetConnection().Close(); } } return GetConnection(); } catch (Exception e) { if (GetConnection().State == ConnectionState.Open) { GetConnection().Close(); } Border.Model.Helpers.FileWriter.Write(e.ToString(), true); } return GetConnection(); }
-
This should be really simple, but im having one of those mornings... Im trying to close my connection, but its staying open if (Border.BLL.Inventory.SqlConnect.GetConnection().State == ConnectionState.Open) { Border.BLL.Inventory.SqlConnect.CloseConnection(); } //BLL public static SqlCeConnection CloseConnection() { return DAL.Helpers.SQLconnect.CloseConnection(); } //DAL public static SqlCeConnection GetConnection() { SqlCeConnection conn = new SqlCeConnection(); try { conn.ConnectionString = Helpers.ConnectionCommand.ConnectionString; conn.Open(); return conn; } catch(Exception e) { if (conn.State == ConnectionState.Open) { conn.Close(); } Border.Model.Helpers.FileWriter.Write(e.ToString(), true); } return conn; } public static SqlCeConnection CloseConnection() { try { if (GetConnection() != null) { if (GetConnection().State == ConnectionState.Open) { GetConnection().Close(); } } return GetConnection(); } catch (Exception e) { if (GetConnection().State == ConnectionState.Open) { GetConnection().Close(); } Border.Model.Helpers.FileWriter.Write(e.ToString(), true); } return GetConnection(); }
Please put your code in <pre> tags, it is currently difficult to read. consider using the following snippet:
using(SqlCeConnection connection = GetConnection())
{
//Use connection here
}as this will close the connection automagically for you, even if the database call throws an exception.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
This should be really simple, but im having one of those mornings... Im trying to close my connection, but its staying open if (Border.BLL.Inventory.SqlConnect.GetConnection().State == ConnectionState.Open) { Border.BLL.Inventory.SqlConnect.CloseConnection(); } //BLL public static SqlCeConnection CloseConnection() { return DAL.Helpers.SQLconnect.CloseConnection(); } //DAL public static SqlCeConnection GetConnection() { SqlCeConnection conn = new SqlCeConnection(); try { conn.ConnectionString = Helpers.ConnectionCommand.ConnectionString; conn.Open(); return conn; } catch(Exception e) { if (conn.State == ConnectionState.Open) { conn.Close(); } Border.Model.Helpers.FileWriter.Write(e.ToString(), true); } return conn; } public static SqlCeConnection CloseConnection() { try { if (GetConnection() != null) { if (GetConnection().State == ConnectionState.Open) { GetConnection().Close(); } } return GetConnection(); } catch (Exception e) { if (GetConnection().State == ConnectionState.Open) { GetConnection().Close(); } Border.Model.Helpers.FileWriter.Write(e.ToString(), true); } return GetConnection(); }
Hi, I went through your code, and got a point to tell you if that helps. I noticed that in catch block in DAL you are closing the connection only if the connection state is OPEN. I remember there is another option that you may also check before you are closing the connection and that is BROKEN. So what I am suggesting is ,
if ((GetConnection().State == ConnectionState.Open)||((GetConnection().State == ConnectionState.Broken))
{
GetConnection().Close();}
or
if (GetConnection().State != ConnectionState.Closed)
{
GetConnection().Close();}
Give a try and let me know.
Thanks, Arindam D Tewary
-
Hi, I went through your code, and got a point to tell you if that helps. I noticed that in catch block in DAL you are closing the connection only if the connection state is OPEN. I remember there is another option that you may also check before you are closing the connection and that is BROKEN. So what I am suggesting is ,
if ((GetConnection().State == ConnectionState.Open)||((GetConnection().State == ConnectionState.Broken))
{
GetConnection().Close();}
or
if (GetConnection().State != ConnectionState.Closed)
{
GetConnection().Close();}
Give a try and let me know.
Thanks, Arindam D Tewary
Arindam Tewary wrote:
if ((GetConnection().State == ConnectionState.Open)||((GetConnection().State == ConnectionState.Broken))
This isn't a good idea since his code is returning a new connection object on every call to GetConnection. It's not returning the same connection object, but a new one every time. So, you're comparing the first connections .State to Open OR the second connection objects state to Broken. It's not doing what you think it's doing.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Arindam Tewary wrote:
if ((GetConnection().State == ConnectionState.Open)||((GetConnection().State == ConnectionState.Broken))
This isn't a good idea since his code is returning a new connection object on every call to GetConnection. It's not returning the same connection object, but a new one every time. So, you're comparing the first connections .State to Open OR the second connection objects state to Broken. It's not doing what you think it's doing.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...