473,387 Members | 1,440 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,387 software developers and data experts.

Beginner : closing DataReader

try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of scope
in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens
.......
Nov 15 '05 #1
10 3936
Can you not do something like :

SqlDataReader dread = null;
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
if (dread != null)
{ //or some sensible checking, i can't say what it should be... ;)
dread.Close();
}
sqlcom.Connection.Close();
}
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Of****************@TK2MSFTNGP11.phx.gbl...
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of scope in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens
......

Nov 15 '05 #2
Hi Not

SqlDataReader dread=sqlcom.ExecuteReader();
try
{
}
finally
{
dread.Close();
sqlcom.Connection.Close();
}

Your code should looks like the code above.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Of****************@TK2MSFTNGP11.phx.gbl...
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of scope in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens
......

Nov 15 '05 #3
Since dread variable is local only to the try block it will not be available
in the finally block. Only option is to move the data reader declaration
outside try block or have the Close() method inside try.

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of scope
in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens
.......

Nov 15 '05 #4
Or even better:

try
{
using (SqlDataReader dread = sqlcom.ExecuteReader())
{
// use reader here - no need to call Close in the finally block
}
}
finally
{
sqlcom.Connection.Close();
}

Further, if you put the SqlConnection into a using block too you can get rid
of the try/finally completely.

Regards,
Sami

"Saurabh" <sa*****@nagpurcity.net> wrote in message
news:eh**************@tk2msftngp13.phx.gbl...
Can you not do something like :

SqlDataReader dread = null;
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
if (dread != null)
{ //or some sensible checking, i can't say what it should be... ;)
dread.Close();
}
sqlcom.Connection.Close();
}
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Of****************@TK2MSFTNGP11.phx.gbl...
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of

scope
in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens ......


Nov 15 '05 #5
Thanks saurabh ..

"Saurabh" <sa*****@nagpurcity.net> wrote in message
news:eh**************@tk2msftngp13.phx.gbl...
Can you not do something like :

SqlDataReader dread = null;
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
if (dread != null)
{ //or some sensible checking, i can't say what it should be... ;)
dread.Close();
}
sqlcom.Connection.Close();
}
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Of****************@TK2MSFTNGP11.phx.gbl...
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of

scope
in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens ......


Nov 15 '05 #6
Actually it should be as below.... in the previous post i had the
declaration outside as well as inside the try block. My mistake in copy +
paste ;)

SqlDataReader dread = null;
try
{
dread=sqlcom.ExecuteReader();
}

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:ey**************@TK2MSFTNGP11.phx.gbl...
Thanks saurabh ..

"Saurabh" <sa*****@nagpurcity.net> wrote in message
news:eh**************@tk2msftngp13.phx.gbl...
Can you not do something like :

SqlDataReader dread = null;
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
if (dread != null)
{ //or some sensible checking, i can't say what it should be... ;)
dread.Close();
}
sqlcom.Connection.Close();
}
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Of****************@TK2MSFTNGP11.phx.gbl...
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of

scope
in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens ......



Nov 15 '05 #7
Or you could even do:

using (SqlDataReader dread =
sqlcom.ExecuteReader(CommandBehavior.CloseConnecti on))
{
// use reader here - no need to call Close in the finally block
...
}

Regards,
Dan

"Sami Vaaraniemi" <sa***************@jippii.fi> wrote in message
news:bs**********@phys-news1.kolumbus.fi...
Or even better:

try
{
using (SqlDataReader dread = sqlcom.ExecuteReader())
{
// use reader here - no need to call Close in the finally block
}
}
finally
{
sqlcom.Connection.Close();
}

Further, if you put the SqlConnection into a using block too you can get rid of the try/finally completely.

Regards,
Sami

Nov 15 '05 #8
hi miha but i wanted to capture any exception that happens in
SqlDataReader dread=sqlcom.ExecuteReader();
so i have to put it inside try block....
"Miha Markic" <miha at rthand com> wrote in message
news:eW**************@TK2MSFTNGP10.phx.gbl...
Hi Not

SqlDataReader dread=sqlcom.ExecuteReader();
try
{
}
finally
{
dread.Close();
sqlcom.Connection.Close();
}

Your code should looks like the code above.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Of****************@TK2MSFTNGP11.phx.gbl...
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of

scope
in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens ......


Nov 15 '05 #9

okey :-)
"Saurabh" <sa*****@nagpurcity.net> wrote in message
news:u2**************@tk2msftngp13.phx.gbl...
Actually it should be as below.... in the previous post i had the
declaration outside as well as inside the try block. My mistake in copy +
paste ;)

SqlDataReader dread = null;
try
{
dread=sqlcom.ExecuteReader();
}

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:ey**************@TK2MSFTNGP11.phx.gbl...
Thanks saurabh ..

"Saurabh" <sa*****@nagpurcity.net> wrote in message
news:eh**************@tk2msftngp13.phx.gbl...
Can you not do something like :

SqlDataReader dread = null;
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
if (dread != null)
{ //or some sensible checking, i can't say what it should be... ;)
dread.Close();
}
sqlcom.Connection.Close();
}
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Of****************@TK2MSFTNGP11.phx.gbl...
> try
> {
> SqlDataReader dread=sqlcom.ExecuteReader();
> }
>
> finally
> {
> dread.Close();
> sqlcom.Connection.Close();
> }
>
> here is my problem..
> the above code does not compile becoz dread (SqlDataReader) is out of scope
> in finally block
> i cant declare dread (SqlDataReader) outside try block .....
> how do i close data reader dread (SqlDataReader) if any exception

happens
> ......
>
>



Nov 15 '05 #10
Hi,

Yes, you might put it inside another try/catch block or do like this:
SqlDataReader dread;
try
{
dread = =sqlcom.ExecuteReader();
}
finally
{
if (dread != null)
dread.Close();
sqlcom.Connection.Close();
}

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
hi miha but i wanted to capture any exception that happens in
SqlDataReader dread=sqlcom.ExecuteReader();
so i have to put it inside try block....
"Miha Markic" <miha at rthand com> wrote in message
news:eW**************@TK2MSFTNGP10.phx.gbl...
Hi Not

SqlDataReader dread=sqlcom.ExecuteReader();
try
{
}
finally
{
dread.Close();
sqlcom.Connection.Close();
}

Your code should looks like the code above.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Of****************@TK2MSFTNGP11.phx.gbl...
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of

scope
in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens ......



Nov 15 '05 #11

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

Similar topics

7
by: DS | last post by:
Is there a way to automatically close the data reader connection? I'm using the MS Data Access Application block to substantially {entirely} separate the data access layer (DAL) from the business...
4
by: Jeff User | last post by:
Hi all I am using an OleDbDataReader. I need to establish and then keep the connection that I use, but I do not need to keep the data reader, after this operation is over. Therefore, regardless...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
3
by: Paolo Pignatelli | last post by:
I have a Function in a Class File that uses ApplicationBlocks: Public Function GetProductsByCategory(ByVal CategoryID As Integer) Dim myConnection As SqlConnection = New...
7
by: Arsalan | last post by:
I have a function which return datareader Public Shared Function ReturnDReader(ByVal query As String) As OleDbDataReader Dim Connection_String As String =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.