Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Close Connection

Close Connection

Scheduled Pinned Locked Moved C#
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    ziwez0
    wrote on last edited by
    #1

    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(); }

    K A 2 Replies Last reply
    0
    • Z ziwez0

      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(); }

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • Z ziwez0

        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(); }

        A Offline
        A Offline
        Arindam Tewary
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • A Arindam 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

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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...

          Z 1 Reply Last reply
          0
          • D Dave Kreskowiak

            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...

            Z Offline
            Z Offline
            ziwez0
            wrote on last edited by
            #5

            doh! thanks that makes perfect sense.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups