block scope when using 'using' statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Priess

    #1

    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( "StoredProcedur e", new
    SqlConnection(" ConnectionStrin g")))
    {
    DataTable dt = new DataTable("tabl e");
    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("tabl e");
    using (SqlDataAdapter sda = new SqlDataAdapter( "StoredProcedur e", new
    SqlConnection(" ConnectionStrin g")))
    {
    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-----


  • 100

    #2
    Re: block scope when using 'using' statement

    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.spam@nospam .com> wrote in message
    news:%23$vq0wgq DHA.2404@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > 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[/color]
    know[color=blue]
    > is, just how much block-scope applies to objects created in the using[/color]
    scope.[color=blue]
    >
    > For example:
    >
    > <code>
    > static DataTable getTable()
    > {
    > using (SqlDataAdapter sda = new SqlDataAdapter( "StoredProcedur e", new
    > SqlConnection(" ConnectionStrin g")))
    > {
    > DataTable dt = new DataTable("tabl e");
    > 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("tabl e");
    > using (SqlDataAdapter sda = new SqlDataAdapter( "StoredProcedur e", new
    > SqlConnection(" ConnectionStrin g")))
    > {
    > sda.Fill(dt);
    > }
    > return dt;
    > }
    > </code>
    >
    > Now, when I call this, I also get back zero rows in my table, yet when I[/color]
    run[color=blue]
    > 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++++$[/color]
    O--[color=blue]
    > M V-- PS+ PE+ Y++ PGP++ t++@ 5++@ X++ R+@ tv b++ DI++ D+++>++++ G++ e++
    > h---- r+++ y++++
    > -----END GEEK CODE BLOCK-----
    >
    >[/color]


    Comment

    • Miha Markic

      #3
      Re: block scope when using 'using' statement


      "Bill Priess" <no.spam@nospam .com> wrote in message
      news:%23$vq0wgq DHA.2404@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > 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[/color]
      know[color=blue]
      > is, just how much block-scope applies to objects created in the using[/color]
      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


      Comment

      • 100

        #4
        Re: block scope when using 'using' statement

        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" <100@100.com> wrote in message
        news:%236vN55gq DHA.1408@TK2MSF TNGP11.phx.gbl. ..[color=blue]
        > 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[/color]
        the[color=blue]
        > object. Let the caller decide what to do with it.
        >
        > HTH
        > B\rgds
        > 100
        >
        > "Bill Priess" <no.spam@nospam .com> wrote in message
        > news:%23$vq0wgq DHA.2404@TK2MSF TNGP12.phx.gbl. ..[color=green]
        > > 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[/color]
        > know[color=green]
        > > is, just how much block-scope applies to objects created in the using[/color]
        > scope.[color=green]
        > >
        > > For example:
        > >
        > > <code>
        > > static DataTable getTable()
        > > {
        > > using (SqlDataAdapter sda = new SqlDataAdapter( "StoredProcedur e",[/color][/color]
        new[color=blue][color=green]
        > > SqlConnection(" ConnectionStrin g")))
        > > {
        > > DataTable dt = new DataTable("tabl e");
        > > sda.Fill(dt);
        > > return dt;
        > > }
        > > }
        > > </code>
        > >
        > > I know this would return a table with zero rows becuase it is created[/color][/color]
        and[color=blue][color=green]
        > > destroyed within the scope of the using statement. But...
        > > <code>
        > > static DataTable getTable()
        > > {
        > > DataTable dt = new DataTable("tabl e");
        > > using (SqlDataAdapter sda = new SqlDataAdapter( "StoredProcedur e",[/color][/color]
        new[color=blue][color=green]
        > > SqlConnection(" ConnectionStrin g")))
        > > {
        > > sda.Fill(dt);
        > > }
        > > return dt;
        > > }
        > > </code>
        > >
        > > Now, when I call this, I also get back zero rows in my table, yet when I[/color]
        > run[color=green]
        > > the query in SQL, it comes back fine. Anyone have any ideas or[/color][/color]
        experience[color=blue][color=green]
        > > 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++++$[/color]
        > O--[color=green]
        > > M V-- PS+ PE+ Y++ PGP++ t++@ 5++@ X++ R+@ tv b++ DI++ D+++>++++ G++ e++
        > > h---- r+++ y++++
        > > -----END GEEK CODE BLOCK-----
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Jerry Negrelli

          #5
          block scope when using 'using' statement

          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
          [color=blue]
          >-----Original Message-----
          >Hey gang,
          >
          >Ok, I'm stumped on this one... I am using the using[/color]
          statement to wrap a[color=blue]
          >SqlDataAdapt er that I am using to fill a DataTable. Now,[/color]
          what I need to know[color=blue]
          >is, just how much block-scope applies to objects created[/color]
          in the using scope.[color=blue]
          >
          >For example:
          >
          ><code>
          >static DataTable getTable()
          >{
          > using (SqlDataAdapter sda = new SqlDataAdapter[/color]
          ("StoredProcedu re", new[color=blue]
          >SqlConnection( "ConnectionStri ng")))
          > {
          > DataTable dt = new DataTable("tabl e");
          > sda.Fill(dt);
          > return dt;
          > }
          >}
          ></code>
          >
          >I know this would return a table with zero rows becuase[/color]
          it is created and[color=blue]
          >destroyed within the scope of the using statement. But...
          ><code>
          >static DataTable getTable()
          >{
          > DataTable dt = new DataTable("tabl e");
          > using (SqlDataAdapter sda = new SqlDataAdapter[/color]
          ("StoredProcedu re", new[color=blue]
          >SqlConnection( "ConnectionStri ng")))
          > {
          > sda.Fill(dt);
          > }
          > return dt;
          >}
          ></code>
          >
          >Now, when I call this, I also get back zero rows in my[/color]
          table, yet when I run[color=blue]
          >the query in SQL, it comes back fine. Anyone have any[/color]
          ideas or experience[color=blue]
          >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+++$[/color]
          N++ o K? w++++$ O--[color=blue]
          >M V-- PS+ PE+ Y++ PGP++ t++@ 5++@ X++ R+@ tv b++ DI++[/color]
          D+++>++++ G++ e++[color=blue]
          >h---- r+++ y++++
          >-----END GEEK CODE BLOCK-----
          >
          >
          >.
          >[/color]

          Comment

          • Eric Newton

            #6
            Re: block scope when using 'using' statement

            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(" ConnectionStrin g") )
            {
            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
            eric@cc.ensoft-software.com [remove the first "CC."]

            "Jerry Negrelli" <jerry.negrelli @nospamdatascie ntific.com> wrote in message
            news:0aea01c3aa 29$33c6bf30$a00 1280a@phx.gbl.. .[color=blue]
            > 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
            >[color=green]
            > >-----Original Message-----
            > >Hey gang,
            > >
            > >Ok, I'm stumped on this one... I am using the using[/color]
            > statement to wrap a[color=green]
            > >SqlDataAdapt er that I am using to fill a DataTable. Now,[/color]
            > what I need to know[color=green]
            > >is, just how much block-scope applies to objects created[/color]
            > in the using scope.[color=green]
            > >
            > >For example:
            > >
            > ><code>
            > >static DataTable getTable()
            > >{
            > > using (SqlDataAdapter sda = new SqlDataAdapter[/color]
            > ("StoredProcedu re", new[color=green]
            > >SqlConnection( "ConnectionStri ng")))
            > > {
            > > DataTable dt = new DataTable("tabl e");
            > > sda.Fill(dt);
            > > return dt;
            > > }
            > >}
            > ></code>
            > >
            > >I know this would return a table with zero rows becuase[/color]
            > it is created and[color=green]
            > >destroyed within the scope of the using statement. But...
            > ><code>
            > >static DataTable getTable()
            > >{
            > > DataTable dt = new DataTable("tabl e");
            > > using (SqlDataAdapter sda = new SqlDataAdapter[/color]
            > ("StoredProcedu re", new[color=green]
            > >SqlConnection( "ConnectionStri ng")))
            > > {
            > > sda.Fill(dt);
            > > }
            > > return dt;
            > >}
            > ></code>
            > >
            > >Now, when I call this, I also get back zero rows in my[/color]
            > table, yet when I run[color=green]
            > >the query in SQL, it comes back fine. Anyone have any[/color]
            > ideas or experience[color=green]
            > >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+++$[/color]
            > N++ o K? w++++$ O--[color=green]
            > >M V-- PS+ PE+ Y++ PGP++ t++@ 5++@ X++ R+@ tv b++ DI++[/color]
            > D+++>++++ G++ e++[color=green]
            > >h---- r+++ y++++
            > >-----END GEEK CODE BLOCK-----
            > >
            > >
            > >.
            > >[/color][/color]


            Comment

            Working...