DataGrid & datasouce close

Discussion in 'ASP .Net Datagrid Control' started by PaulMac, Oct 23, 2003.

  1. PaulMac

    PaulMac Guest

    Hello All,

    I have a webpage containing a DataGrid control. The datasource is a
    DataReader. The code is like so:

    AsaCommand Cmd = new AsaCommand( "SELECT ... FROM ...", Db.Connection );
    DataGridNet.DataSource = Cmd.ExecuteReader();
    DataGridNet.DataBind();

    Is it necessary to do an explicit close on the DataReader? In which case,
    the code could be this:

    AsaCommand Cmd = new AsaCommand( "SELECT ... FROM ...", Db.Connection );
    AsaDataReader Reader = Cmd.ExecuteReader();
    DataGridNet.DataSource = Reader;
    DataGridNet.DataBind();
    Reader.Close();

    Thanks,
    Paul
     
    PaulMac, Oct 23, 2003
    #1
    1. Advertising

  2. PaulMac

    David Guest

    In article <#>, PaulMac wrote:
    >
    >
    > Is it necessary to do an explicit close on the DataReader?


    Yes, you should always close the DataReader explicitly. And you also
    should close the connection, although you can specify this in the
    ExecuteReader call.

    >In which case,
    > the code could be this:


    >
    > AsaCommand Cmd = new AsaCommand( "SELECT ... FROM ...", Db.Connection );
    > AsaDataReader Reader = Cmd.ExecuteReader();
    > DataGridNet.DataSource = Reader;
    > DataGridNet.DataBind();
    > Reader.Close();
    >


    Either,

    ASADataReader = Cmd.ExecuteReader()
    DataGridNet.DataSource = Reader;
    DAtaGridNet.DataBind();
    Reader.Close();
    Db.Connection.Close();

    or....
    AsaDataReader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
    DataGridNet.DataSource = Reader;
    DataGridNet.DataBind();
    Reader.Close;


    --
    David
    dfoster at
    hotpop dot com
     
    David, Oct 23, 2003
    #2
    1. Advertising

  3. PaulMac

    PaulMac Guest

    Thank you, David. I appreciate your help.

    Paul

    "David" <dfoster@127.0.0.1> wrote in message
    news:...
    > In article <#>, PaulMac wrote:
    > >
    > >
    > > Is it necessary to do an explicit close on the DataReader?

    >
    > Yes, you should always close the DataReader explicitly. And you also
    > should close the connection, although you can specify this in the
    > ExecuteReader call.
    >
    > >In which case,
    > > the code could be this:

    >
    > >
    > > AsaCommand Cmd = new AsaCommand( "SELECT ... FROM ...", Db.Connection );
    > > AsaDataReader Reader = Cmd.ExecuteReader();
    > > DataGridNet.DataSource = Reader;
    > > DataGridNet.DataBind();
    > > Reader.Close();
    > >

    >
    > Either,
    >
    > ASADataReader = Cmd.ExecuteReader()
    > DataGridNet.DataSource = Reader;
    > DAtaGridNet.DataBind();
    > Reader.Close();
    > Db.Connection.Close();
    >
    > or....
    > AsaDataReader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
    > DataGridNet.DataSource = Reader;
    > DataGridNet.DataBind();
    > Reader.Close;
    >
    >
    > --
    > David
    > dfoster at
    > hotpop dot com
     
    PaulMac, Oct 23, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denon
    Replies:
    1
    Views:
    593
    Saravana [MVP]
    Nov 14, 2003
  2. Replies:
    1
    Views:
    323
  3. Daniel Albisser
    Replies:
    1
    Views:
    1,133
    GaryM
    Apr 7, 2004
  4. =?Utf-8?B?V2FyYW4=?=

    Multiple DataSouce for a DataGrid

    =?Utf-8?B?V2FyYW4=?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    400
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    May 9, 2006
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    921
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page