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. Other Discussions
  3. The Weird and The Wonderful
  4. Tight disposing of objects in an old project

Tight disposing of objects in an old project

Scheduled Pinned Locked Moved The Weird and The Wonderful
database
6 Posts 5 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.
  • S Offline
    S Offline
    Sanjay K Gupta
    wrote on last edited by
    #1

    using (SqlConnection Connection = Sql.GetConnectionString())
    {
    using (SqlCommand command = new SqlCommand("*****", Connection))
    {
    command.CommandType = CommandType.StoredProcedure;
    try
    {
    Connection.Open();
    intID = (int) command.ExecuteScalar();
    }
    catch (Exception ex)
    {
    command.Dispose();
    Connection.Close();
    Connection.Dispose();
    }
    finally
    {
    command.Dispose();
    Connection.Close();
    Connection.Dispose();
    }
    }
    }

    ___ ___ ___
    |__ |_| |\ | | |_| \ /
    __| | | | \| |__| | | /

    D P B D 4 Replies Last reply
    0
    • S Sanjay K Gupta

      using (SqlConnection Connection = Sql.GetConnectionString())
      {
      using (SqlCommand command = new SqlCommand("*****", Connection))
      {
      command.CommandType = CommandType.StoredProcedure;
      try
      {
      Connection.Open();
      intID = (int) command.ExecuteScalar();
      }
      catch (Exception ex)
      {
      command.Dispose();
      Connection.Close();
      Connection.Dispose();
      }
      finally
      {
      command.Dispose();
      Connection.Close();
      Connection.Dispose();
      }
      }
      }

      ___ ___ ___
      |__ |_| |\ | | |_| \ /
      __| | | | \| |__| | | /

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

      Well, at least they're being sure the stuff is disposed of. Too bad they also disposed of their ability to understand how Try/Catch works too.

      A guide to posting questions on CodeProject

      How to debug small programs
      Dave Kreskowiak

      B 1 Reply Last reply
      0
      • S Sanjay K Gupta

        using (SqlConnection Connection = Sql.GetConnectionString())
        {
        using (SqlCommand command = new SqlCommand("*****", Connection))
        {
        command.CommandType = CommandType.StoredProcedure;
        try
        {
        Connection.Open();
        intID = (int) command.ExecuteScalar();
        }
        catch (Exception ex)
        {
        command.Dispose();
        Connection.Close();
        Connection.Dispose();
        }
        finally
        {
        command.Dispose();
        Connection.Close();
        Connection.Dispose();
        }
        }
        }

        ___ ___ ___
        |__ |_| |\ | | |_| \ /
        __| | | | \| |__| | | /

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Pretty clear developer intent. Yet it's also a scream for help.

        You'll never get very far if all you do is follow instructions.

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          Well, at least they're being sure the stuff is disposed of. Too bad they also disposed of their ability to understand how Try/Catch works too.

          A guide to posting questions on CodeProject

          How to debug small programs
          Dave Kreskowiak

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          Dave Kreskowiak wrote:

          disposed of their ability to understand how Try/Catch works

          and using also...

          1 Reply Last reply
          0
          • S Sanjay K Gupta

            using (SqlConnection Connection = Sql.GetConnectionString())
            {
            using (SqlCommand command = new SqlCommand("*****", Connection))
            {
            command.CommandType = CommandType.StoredProcedure;
            try
            {
            Connection.Open();
            intID = (int) command.ExecuteScalar();
            }
            catch (Exception ex)
            {
            command.Dispose();
            Connection.Close();
            Connection.Dispose();
            }
            finally
            {
            command.Dispose();
            Connection.Close();
            Connection.Dispose();
            }
            }
            }

            ___ ___ ___
            |__ |_| |\ | | |_| \ /
            __| | | | \| |__| | | /

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            Bloody beginners!

                    using (SqlConnection Connection = Sql.GetConnectionString())
                    {
                        using (SqlCommand command = new SqlCommand("\*\*\*\*\*", Connection))
                        {
                            command.CommandType = CommandType.StoredProcedure;
                            try
                            {
                                Connection.Open();
                                intID = (int) command.ExecuteScalar();
                                // Do not forget to dispose!
                                command.Dispose();
                                Connection.Close();
                                Connection.Dispose();
                            }
                            catch (Exception ex)
                            {
                                //there is a catch:
                                intID = 22;
                                command.Dispose();
                                Connection.Close();
                                Connection.Dispose();
                            }
                            finally
                            {
                                command.Dispose();
                                Connection.Close();
                                Connection.Dispose();
                            }
                        }
                    }
            

            Also a great idea that Sql.GetConnectionString() does not return a string but a SqlConnection.

            1 Reply Last reply
            0
            • S Sanjay K Gupta

              using (SqlConnection Connection = Sql.GetConnectionString())
              {
              using (SqlCommand command = new SqlCommand("*****", Connection))
              {
              command.CommandType = CommandType.StoredProcedure;
              try
              {
              Connection.Open();
              intID = (int) command.ExecuteScalar();
              }
              catch (Exception ex)
              {
              command.Dispose();
              Connection.Close();
              Connection.Dispose();
              }
              finally
              {
              command.Dispose();
              Connection.Close();
              Connection.Dispose();
              }
              }
              }

              ___ ___ ___
              |__ |_| |\ | | |_| \ /
              __| | | | \| |__| | | /

              D Offline
              D Offline
              Dennis_E
              wrote on last edited by
              #6

              Maybe they came from Java and someone told them "you have to use using". So they put a using in there...

              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