Closing a datareader

Discussion in 'ASP .Net' started by m.fisher@uku.co.uk, Jun 9, 2007.

  1. Guest

    Hi

    I am looking at the starter kits from asp.net 1.1. I have read that a
    datareader returned from a DAL should be closed. In the code below,
    copied from the Commerce starter kit, how would the datareader be
    closed? I know there is a .close() command, but where would it go.

    Thanks

    ----------------
    DAL
    ----------------
    Public Function GetProductCategories() As SqlDataReader

    ' Create Instance of Connection and Command Object
    Dim myConnection As SqlConnection = New
    SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
    Dim myCommand As SqlCommand = New
    SqlCommand("CMRC_ProductCategoryList", myConnection)

    ' Mark the Command as a SPROC
    myCommand.CommandType = CommandType.StoredProcedure

    ' Execute the command
    myConnection.Open()
    Dim result As SqlDataReader =
    myCommand.ExecuteReader(CommandBehavior.CloseConnection)

    ' Return the datareader result
    Return result

    End Function


    ---------------
    ..aspx page
    ---------------

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)


    ' Obtain categoryId from QueryString
    Dim categoryId As Integer = CInt(Request.Params("CategoryID"))

    ' Obtain products and databind to an asp:datalist control
    Dim productCatalogue As ASPNET.StarterKit.Commerce.ProductsDB = New
    ASPNET.StarterKit.Commerce.ProductsDB()

    MyList.DataSource = productCatalogue.GetProducts(categoryId)
    MyList.DataBind()

    End Sub
     
    , Jun 9, 2007
    #1
    1. Advertising

  2. On Jun 9, 11:56 am, wrote:
    > Hi
    >
    > I am looking at the starter kits from asp.net 1.1. I have read that a
    > datareader returned from a DAL should be closed. In the code below,
    > copied from the Commerce starter kit, how would the datareader be
    > closed? I know there is a .close() command, but where would it go.
    >
    > Thanks
    >
    > ----------------
    > DAL
    > ----------------
    > Public Function GetProductCategories() As SqlDataReader
    >
    > ' Create Instance of Connection and Command Object
    > Dim myConnection As SqlConnection = New
    > SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
    > Dim myCommand As SqlCommand = New
    > SqlCommand("CMRC_ProductCategoryList", myConnection)
    >
    > ' Mark the Command as a SPROC
    > myCommand.CommandType = CommandType.StoredProcedure
    >
    > ' Execute the command
    > myConnection.Open()
    > Dim result As SqlDataReader =
    > myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    >
    > ' Return the datareader result
    > Return result
    >
    > End Function
    >
    > ---------------
    > .aspx page
    > ---------------
    >
    > Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    >
    > ' Obtain categoryId from QueryString
    > Dim categoryId As Integer = CInt(Request.Params("CategoryID"))
    >
    > ' Obtain products and databind to an asp:datalist control
    > Dim productCatalogue As ASPNET.StarterKit.Commerce.ProductsDB = New
    > ASPNET.StarterKit.Commerce.ProductsDB()
    >
    > MyList.DataSource = productCatalogue.GetProducts(categoryId)
    > MyList.DataBind()
    >
    > End Sub


    you have to close it

    ((SqlDataReader)MyList.DataSource).Close();
     
    Alexey Smirnov, Jun 9, 2007
    #2
    1. Advertising

  3. On Jun 9, 12:49 pm, Alexey Smirnov <> wrote:
    > On Jun 9, 11:56 am, wrote:
    >
    >
    >
    >
    >
    > > Hi

    >
    > > I am looking at the starter kits from asp.net 1.1. I have read that a
    > > datareader returned from a DAL should be closed. In the code below,
    > > copied from the Commerce starter kit, how would the datareader be
    > > closed? I know there is a .close() command, but where would it go.

    >
    > > Thanks

    >
    > > ----------------
    > > DAL
    > > ----------------
    > > Public Function GetProductCategories() As SqlDataReader

    >
    > > ' Create Instance of Connection and Command Object
    > > Dim myConnection As SqlConnection = New
    > > SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
    > > Dim myCommand As SqlCommand = New
    > > SqlCommand("CMRC_ProductCategoryList", myConnection)

    >
    > > ' Mark the Command as a SPROC
    > > myCommand.CommandType = CommandType.StoredProcedure

    >
    > > ' Execute the command
    > > myConnection.Open()
    > > Dim result As SqlDataReader =
    > > myCommand.ExecuteReader(CommandBehavior.CloseConnection)

    >
    > > ' Return the datareader result
    > > Return result

    >
    > > End Function

    >
    > > ---------------
    > > .aspx page
    > > ---------------

    >
    > > Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    >
    > > ' Obtain categoryId from QueryString
    > > Dim categoryId As Integer = CInt(Request.Params("CategoryID"))

    >
    > > ' Obtain products and databind to an asp:datalist control
    > > Dim productCatalogue As ASPNET.StarterKit.Commerce.ProductsDB = New
    > > ASPNET.StarterKit.Commerce.ProductsDB()

    >
    > > MyList.DataSource = productCatalogue.GetProducts(categoryId)
    > > MyList.DataBind()

    >
    > > End Sub

    >
    > you have to close it
    >
    > ((SqlDataReader)MyList.DataSource).Close();- Hide quoted text -
    >
    > - Show quoted text -


    brrr... sorry, you can close it, but it's already closed after it has
    been bound to the control. Connection will be closed as well because
    of CommandBehavior.CloseConnection
     
    Alexey Smirnov, Jun 9, 2007
    #3
  4. Mark Rae Guest

    <> wrote in message
    news:...

    > I have read that a datareader returned from a DAL should be closed.


    Most definitely!

    > In the code below, copied from the Commerce starter kit, how would the
    > datareader be closed? I know there is a .close() command, but where would
    > it go.


    > MyList.DataSource = productCatalogue.GetProducts(categoryId)
    > MyList.DataBind()

    ((SqlDataReader)MyList.DataSource).Close();


    --
    http://www.markrae.net
     
    Mark Rae, Jun 9, 2007
    #4
  5. Mark Rae Guest

    "Alexey Smirnov" <> wrote in message
    news:...

    > but it's already closed after it has been bound to the control.


    You're correct - please ignore my previous post...


    --
    http://www.markrae.net
     
    Mark Rae, Jun 9, 2007
    #5
    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. Paolo Pignatelli

    Closing DataReader using ApplicationBlocks

    Paolo Pignatelli, Feb 25, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,597
    Joe Fallon
    Feb 26, 2005
  2. Arsalan
    Replies:
    7
    Views:
    761
    Rob Nicholson
    Apr 2, 2005
  3. rockdale

    DataReader and DB Connection Closing

    rockdale, Sep 21, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    13,034
    Rafael Neris
    Jan 31, 2012
  4. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    661
    Thomas Hansen
    Feb 15, 2007
  5. thomas
    Replies:
    0
    Views:
    235
    thomas
    Oct 23, 2003
Loading...

Share This Page