473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to close sqldatareader when error occur

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;

}
Nov 19 '05 #1
4 2350
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();
}
}
"mimi" wrote:
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;

}

Nov 19 '05 #2
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.
"mimi" <mh****@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
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;

}

Nov 19 '05 #3
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;
}
}

--
HTH,

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

"mimi" <mh****@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
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;

}

Nov 19 '05 #4
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;

}

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:OH**************@TK2MSFTNGP15.phx.gbl...
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;
}
}

--
HTH,

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

"mimi" <mh****@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
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;

}


Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Bryan | last post by:
i'm curious to know how others handle the closing of files. i seem to always end up with this pattern even though i rarely see others do it. f1 = file('file1') try: # process f1 finally:...
3
by: sam | last post by:
Hello group, I have a function which is used to initiate sqlDataReader object. I was trying to invoke the close method on the DataReader object but cant really do that as the function returns a...
1
by: huzz | last post by:
I have a method that gets sqldatareader as shown below.. my question is how do i close the sqlconnection (objConn) object within this method? If i put the objconn.Close(); after the return then i...
3
by: branton ellerbee | last post by:
I am looping through a datareader and building a table. However, no matter what the resultset, the datareader skips the first row of data, then builds the rest of the resultset. I have seen this...
3
by: Woody Splawn | last post by:
I have some code which I will show below that opens a SQLDataReader, gets some information and then closes. My question is, what is the proper way to close? Do I close the reader first and then...
5
by: jjmraz | last post by:
Hi, I have a situation where in a dll a SqlDataReader is created on a function call but is never closed. The datareader gets passed back to the asp.net page calling it. How should I close the...
17
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data,...
4
by: Learner | last post by:
Hello, This my a method to call a stored proce and uses a DataReader to read the data in the below method I am trying to assign a null value to my datareader variable Dim datareader As...
5
by: jobs | last post by:
re: try catch finally to close and dispose, but still want Application_error to fire 1. If catch an exception to to dispose and close of a ado connect, how can I allow the exception to still...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.