Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

how to close sqldatareader when error occur

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mimi - 20 Jul 2005 22:53 GMT
Hi

Please help me out, I can't find a way to close a sqldatareader when error
occur at statement cmd.ExecuteReader().  I can't close it in catch because
it is local in try scope and I can't declare it outside try scope either
since we have to call cmd.executeReader to create sqldatareader

public string GetLogs(int logID)

{

   string notes = String.Empty;

// cannot declare like    SqlDataReader dr;

   try

   {

           SqlCommand cmd = new SqlCommand("GetLogs", oConn);

           cmd.CommandType = CommandType.StoredProcedure;

           cmd.Parameters.Add("@ID",SqlDbType.Int).Value = logID;

           SqlDataReader dr = cmd.ExecuteReader();

           if (dr.Read())

           {

                   notes = dr["Notes"].ToString();

           }

           dr.Close();

   }

   catch(SqlException ex)

   {

       //can't call dr.Close() because it's not global

   }

   return notes;

}
Chris Carter - 21 Jul 2005 00:00 GMT
You can declare this:

SqlDataReader dr = null;

you can let the framework handle that though:

using(SqlConnection oConn = new SqlConnection("My Conn String"))
            using(SqlCommand cmd = new SqlCommand("GetLogs", oConn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@ID", SqlDbType.Int).Value = logID;
                using(SqlDataReader dr = cmd.ExecuteReader())
                {
                    if(dr.Read())
                        notes = dr["Notes"].ToString();
                }
            }

> Hi
>
[quoted text clipped - 46 lines]
>
> }
Kan Coon - 21 Jul 2005 02:32 GMT
Yes you can declare it out side the try block:

Dim dr as datareader

Try
   dr = cmd.ExecuteReader();
Catch ex as SQlException

Finally
   dr.close
End try

You should also be closing that datareader in the finally section of your
exception handler.

> Hi
>
[quoted text clipped - 46 lines]
>
> }
Kevin Spencer - 21 Jul 2005 12:36 GMT
Hi mimi,

You certainly CAN declare it outside the try scope. All you are declaring is
a variable, not a SqlDataReader. The variable is like a box to put the
SqlDataReader IN. You should also set it to null, and check for null when
you attempt to close it. And you should close it in the finally block, which
ALWAYS executes. Example:

public string GetLogs(int logID)

{

   string notes = String.Empty;

   SqlDataReader dr = null;

   try

   {

           SqlCommand cmd = new SqlCommand("GetLogs", oConn);

           cmd.CommandType = CommandType.StoredProcedure;

           cmd.Parameters.Add("@ID",SqlDbType.Int).Value = logID;

           dr = cmd.ExecuteReader();

           if (dr.Read())

           {

                   notes = dr["Notes"].ToString();

           }

           dr.Close();

   }

   catch(SqlException ex)

   {
       ...
   }
   finally
   {
       if (dr != null)
           dr.Close()
       return notes;
   }

}

Signature

HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
The sun never sets on
the Kingdom of Heaven

> Hi
>
[quoted text clipped - 46 lines]
>
> }
mimi - 21 Jul 2005 20:12 GMT
thanks.  It works for me with setting SqlDataReader to null

public string GetLogs(int logID)
{
    string notes = String.Empty;
    SqlDataReader dr = null;

   try
   {
           SqlCommand cmd = new SqlCommand("GetLogs", oConn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add("@ID",SqlDbType.Int).Value = logID;
           dr = cmd.ExecuteReader();

           if (dr.Read())
           {
                  notes = dr["Notes"].ToString();
            }
   }
    catch(SqlException ex)
    {
        ...
    }
    finally
    {
        if (dr != null)
            dr.Close()
    }
   return notes;

}

> Hi mimi,
>
[quoted text clipped - 100 lines]
> >
> > }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.