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

block scope when using 'using' statement

Hey gang,

Ok, I'm stumped on this one... I am using the using statement to wrap a
SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know
is, just how much block-scope applies to objects created in the using scope.

For example:

<code>
static DataTable getTable()
{
using (SqlDataAdapter sda = new SqlDataAdapter("StoredProcedure", new
SqlConnection("ConnectionString")))
{
DataTable dt = new DataTable("table");
sda.Fill(dt);
return dt;
}
}
</code>

I know this would return a table with zero rows becuase it is created and
destroyed within the scope of the using statement. But...
<code>
static DataTable getTable()
{
DataTable dt = new DataTable("table");
using (SqlDataAdapter sda = new SqlDataAdapter("StoredProcedure", new
SqlConnection("ConnectionString")))
{
sda.Fill(dt);
}
return dt;
}
</code>

Now, when I call this, I also get back zero rows in my table, yet when I run
the query in SQL, it comes back fine. Anyone have any ideas or experience
with the using statement?
TIA,
Bill P.

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCM/MU/B dpu s--:-- a32 C++++$ ULH+++ P+++ L++ E+ W+++$ N++ o K? w++++$ O--
M V-- PS+ PE+ Y++ PGP++ t++@ 5++@ X++ R+@ tv b++ DI++ D+++>++++ G++ e++
h---- r+++ y++++
-----END GEEK CODE BLOCK-----
Nov 15 '05 #1
5 2682
100
Hi Bill,
SqlDataAdapter's Dispose method is called as soon as using block is exited
(doesn't matter how - return statement, exception thrown or just the block
ends). This means that your adapter is dsiposed just before you return it.
If you want to return the adapter don't use using statement nor dispose the
object. Let the caller decide what to do with it.

HTH
B\rgds
100

"Bill Priess" <no*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hey gang,

Ok, I'm stumped on this one... I am using the using statement to wrap a
SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know is, just how much block-scope applies to objects created in the using scope.
For example:

<code>
static DataTable getTable()
{
using (SqlDataAdapter sda = new SqlDataAdapter("StoredProcedure", new
SqlConnection("ConnectionString")))
{
DataTable dt = new DataTable("table");
sda.Fill(dt);
return dt;
}
}
</code>

I know this would return a table with zero rows becuase it is created and
destroyed within the scope of the using statement. But...
<code>
static DataTable getTable()
{
DataTable dt = new DataTable("table");
using (SqlDataAdapter sda = new SqlDataAdapter("StoredProcedure", new
SqlConnection("ConnectionString")))
{
sda.Fill(dt);
}
return dt;
}
</code>

Now, when I call this, I also get back zero rows in my table, yet when I run the query in SQL, it comes back fine. Anyone have any ideas or experience
with the using statement?
TIA,
Bill P.

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCM/MU/B dpu s--:-- a32 C++++$ ULH+++ P+++ L++ E+ W+++$ N++ o K? w++++$ O-- M V-- PS+ PE+ Y++ PGP++ t++@ 5++@ X++ R+@ tv b++ DI++ D+++>++++ G++ e++
h---- r+++ y++++
-----END GEEK CODE BLOCK-----

Nov 15 '05 #2

"Bill Priess" <no*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hey gang,

Ok, I'm stumped on this one... I am using the using statement to wrap a
SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know is, just how much block-scope applies to objects created in the using

scope.
Hi Bill,

I think that both examples should return filled table.
Check your adapter it is configured properly...

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
Nov 15 '05 #3
100
Ok. I was wrong . I miss to see that you are not returning the adapter
itself. Next time I'll look more carefully ;(

B\rgds
100

"100" <10*@100.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Bill,
SqlDataAdapter's Dispose method is called as soon as using block is exited
(doesn't matter how - return statement, exception thrown or just the block
ends). This means that your adapter is dsiposed just before you return it.
If you want to return the adapter don't use using statement nor dispose the object. Let the caller decide what to do with it.

HTH
B\rgds
100

"Bill Priess" <no*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hey gang,

Ok, I'm stumped on this one... I am using the using statement to wrap a
SqlDataAdapter that I am using to fill a DataTable. Now, what I need to

know
is, just how much block-scope applies to objects created in the using

scope.

For example:

<code>
static DataTable getTable()
{
using (SqlDataAdapter sda = new SqlDataAdapter("StoredProcedure", new SqlConnection("ConnectionString")))
{
DataTable dt = new DataTable("table");
sda.Fill(dt);
return dt;
}
}
</code>

I know this would return a table with zero rows becuase it is created and destroyed within the scope of the using statement. But...
<code>
static DataTable getTable()
{
DataTable dt = new DataTable("table");
using (SqlDataAdapter sda = new SqlDataAdapter("StoredProcedure", new SqlConnection("ConnectionString")))
{
sda.Fill(dt);
}
return dt;
}
</code>

Now, when I call this, I also get back zero rows in my table, yet when I

run
the query in SQL, it comes back fine. Anyone have any ideas or experience with the using statement?
TIA,
Bill P.

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCM/MU/B dpu s--:-- a32 C++++$ ULH+++ P+++ L++ E+ W+++$ N++ o K? w++++$

O--
M V-- PS+ PE+ Y++ PGP++ t++@ 5++@ X++ R+@ tv b++ DI++ D+++>++++ G++ e++
h---- r+++ y++++
-----END GEEK CODE BLOCK-----


Nov 15 '05 #4
Does it work if you DON'T use the "using" statement? I
suspect that the issue isn't necessarily with
your "using" syntax but with something else...

JER
-----Original Message-----
Hey gang,

Ok, I'm stumped on this one... I am using the using statement to wrap aSqlDataAdapter that I am using to fill a DataTable. Now, what I need to knowis, just how much block-scope applies to objects created in the using scope.
For example:

<code>
static DataTable getTable()
{
using (SqlDataAdapter sda = new SqlDataAdapter ("StoredProcedure", newSqlConnection("ConnectionString")))
{
DataTable dt = new DataTable("table");
sda.Fill(dt);
return dt;
}
}
</code>

I know this would return a table with zero rows becuase it is created anddestroyed within the scope of the using statement. But...
<code>
static DataTable getTable()
{
DataTable dt = new DataTable("table");
using (SqlDataAdapter sda = new SqlDataAdapter ("StoredProcedure", newSqlConnection("ConnectionString")))
{
sda.Fill(dt);
}
return dt;
}
</code>

Now, when I call this, I also get back zero rows in my table, yet when I runthe query in SQL, it comes back fine. Anyone have any ideas or experiencewith the using statement?
TIA,
Bill P.

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCM/MU/B dpu s--:-- a32 C++++$ ULH+++ P+++ L++ E+ W+++$ N++ o K? w++++$ O--M V-- PS+ PE+ Y++ PGP++ t++@ 5++@ X++ R+@ tv b++ DI++ D+++>++++ G++ e++h---- r+++ y++++
-----END GEEK CODE BLOCK-----
.

Nov 15 '05 #5
try restructuring your code, where the SqlConnection is the target of the
using instead of the DataAdapter which doesnt actually need to be
disposed...

ie,
using( SqlConnection conn = new SqlCOnnection("ConnectionString") )
{
SqlDataAdapter da = new SqlDataAdapter( blah blah blah )

conn.Open();
da.Fill( dataset );
conn.Close();
} // calls conn.Dispose() even if exception occurs
--
Eric Newton
C#/ASP Application Developer
er**@cc.ensoft-software.com [remove the first "CC."]

"Jerry Negrelli" <je************@nospamdatascientific.com> wrote in message
news:0a****************************@phx.gbl...
Does it work if you DON'T use the "using" statement? I
suspect that the issue isn't necessarily with
your "using" syntax but with something else...

JER
-----Original Message-----
Hey gang,

Ok, I'm stumped on this one... I am using the using

statement to wrap a
SqlDataAdapter that I am using to fill a DataTable. Now,

what I need to know
is, just how much block-scope applies to objects created

in the using scope.

For example:

<code>
static DataTable getTable()
{
using (SqlDataAdapter sda = new SqlDataAdapter

("StoredProcedure", new
SqlConnection("ConnectionString")))
{
DataTable dt = new DataTable("table");
sda.Fill(dt);
return dt;
}
}
</code>

I know this would return a table with zero rows becuase

it is created and
destroyed within the scope of the using statement. But...
<code>
static DataTable getTable()
{
DataTable dt = new DataTable("table");
using (SqlDataAdapter sda = new SqlDataAdapter

("StoredProcedure", new
SqlConnection("ConnectionString")))
{
sda.Fill(dt);
}
return dt;
}
</code>

Now, when I call this, I also get back zero rows in my

table, yet when I run
the query in SQL, it comes back fine. Anyone have any

ideas or experience
with the using statement?
TIA,
Bill P.

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCM/MU/B dpu s--:-- a32 C++++$ ULH+++ P+++ L++ E+ W+++$

N++ o K? w++++$ O--
M V-- PS+ PE+ Y++ PGP++ t++@ 5++@ X++ R+@ tv b++ DI++

D+++>++++ G++ e++
h---- r+++ y++++
-----END GEEK CODE BLOCK-----
.

Nov 15 '05 #6

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
13
by: Guido van Rossum | last post by:
After many rounds of discussion on python-dev, I'm inviting public comments for PEP 343. Rather than posting the entire PEP text here, I'm inviting everyone to read it on line...
18
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
7
by: seamoon | last post by:
Hi, I'm doing a simple compiler with C as a target language. My language uses the possibility to declare variables anywhere in a block with scope to the end of the block. As I remembered it this...
11
by: | last post by:
Is it possible to define a variable in a block in order to make it invisible outside that block? For example, in C I can write { int a .... } then a will only be available inside the curley...
8
by: Andrew Robinson | last post by:
Are these two equivalent? Is one better than the other? I tend to go with #1 but started wondering.... Thanks, 1: using (SqlConnection cn = new SqlConnection(DataConnection)) using...
12
by: reycri | last post by:
While the following is allowed: if (a == b) SomeFunction(); else OtherFunction(); The following is not: try
8
by: ms news group | last post by:
What happens if exception is thown within a fixed block? Will the pinned memory buffer get unpinned? and if the pinning pointer points to a managed memery buffer allocated within the throwing...
16
by: HillBilly | last post by:
This is freaking me out. I'm using Membership and trying to determine if the database is online. The GetConnectionString( ) method returns a connection string as expected but not when used in the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.