Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6040005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:27:06+00:00 2026-05-23T06:27:06+00:00

Ive been doing some research today on when an how to use the using

  • 0

Ive been doing some research today on when an how to use the “using” statement to dispose of my sql objects.
However I’m still confused about when and how to catch unforseen errors. I have a simple method here and would appreciate any input on wheter its correct or I’m doing something wrong?

private BindingList<My_Object> Search(int ID)
{
   string strSelectStatement = 
     "SELECT 'coloumns' " +
     "FROM 'table' " +
     "WHERE ID = @ID;";

     DataTable dt = new DataTable();
     try
     {
        using (SqlConnection sqlConn = new SqlConnection(m_SQLConnectionString))
        {
          using (SqlCommand cmd = new SqlCommand(strSelectStatement, sqlConn))
          {
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
            using (SqlDataAdapter adpt = new SqlDataAdapter(cmd))
            {
               adpt.Fill(dt);
            }
          }
        }

        My_Object myObject;
        BindingList<My_Object> myObjectList = new BindingList<My_Object>();

        foreach (DataRow row in dt.Rows)
        {
          myObject = new My_Object();
          //Fill/set myObject properties and add to myObject list
        }

        return myObjectList;
     }
     catch (Exception)
     {
        //throw the the exception with its stack trace up to the main call
        throw;
     }
}

So what my catch here would do is catch an error if anything went wrong when running adapter.Fill, or while building myObject/list for example.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:27:07+00:00Added an answer on May 23, 2026 at 6:27 am

    In C# . The using statement defines the scope of an item to be disposed. This can be called for any object which implements the IDisposable interface.

    http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

    So if you had to not use using blocks you would call the dispose method on the class to release/clean up resources created by the object.

    When calling a class that implements the IDisposable interface, the try/finally pattern make sure that unmanaged resources are disposed of even if an exception interrupts your application.

    If an exception is thrown in the case of a using statement the dispose will still be called. You can also stack using statements

    using (SqlConnection sqlConn = new SqlConnection(m_SQLConnectionString))
    using (SqlCommand cmd = new SqlCommand(strSelectStatement, sqlConn))
              {
                cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
                using (SqlDataAdapter adpt = new SqlDataAdapter(cmd))
                {
                   adpt.Fill(dt);
                }
              }
    

    with regards to exception handling. It is not wise to catch all exceptions try to catch the specific exceptions thrown by the class or method. You can view exception details on msdn so SQLConnection : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.open.aspx

    InvalidOperationException
    Cannot open a connection without specifying a data source or server.

    or

    The connection is already open.

    SqlException
    A connection-level error occurred while opening the connection. If the Number property contains the value 18487 or 18488, this indicates that the specified password has expired or must be reset. See the ChangePassword method for more information.

    So these are the exceptions you should cater for. Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been doing some research on whether what best practice is regarding the use
I've been doing some research into finally automating our Development builds and still have
I've just been doing some research into sql data services, and found out that
I've been doing some research on using RMI and in pretty much every case
Ive been doing some research and I cant seem to find anything on writing
I've been doing some research to find a solution to use $.ajax with JSON
Recently I've been doing some research into SEO and how URIs that use hyphens
All, I've been doing some research on when (and when not) to use Memcached.
I've been doing some research on how to globally handle errors in my ASP.NET
After doing some research on the subject, I've been experimenting a lot with patterns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.