PC Review


Reply
Thread Tools Rate Thread

Closing a datareader

 
 
m.fisher@uku.co.uk
Guest
Posts: n/a
 
      9th Jun 2007
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

 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      9th Jun 2007
On Jun 9, 11:56 am, m.fis...@uku.co.uk 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();

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      9th Jun 2007
On Jun 9, 12:49 pm, Alexey Smirnov <alexey.smir...@gmail.com> wrote:
> On Jun 9, 11:56 am, m.fis...@uku.co.uk 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

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      9th Jun 2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      9th Jun 2007
"Alexey Smirnov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataReader and DB Connection Closing rockdale Microsoft ASP .NET 3 21st Sep 2006 08:09 PM
DataReader and DB connection closing? rockdale.green@gmail.com Microsoft ADO .NET 2 21st Sep 2006 03:17 PM
Closing DataReader using ApplicationBlocks Paolo Pignatelli Microsoft ASP .NET 3 26th Feb 2005 09:27 PM
closing DataReader in another layer DS Microsoft C# .NET 7 27th Nov 2004 10:08 AM
Beginner : closing DataReader NotYetaNurd Microsoft Dot NET Framework Forms 8 24th Dec 2003 09:24 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:41 PM.