By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,969 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,969 IT Pros & Developers. It's quick & easy.

Closing a datareader

P: n/a
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("C onnectionString"))
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.CloseConne ction)

' 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 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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("C onnectionString"))
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.CloseConne ction)

' 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();

Jun 9 '07 #2

P: n/a
On Jun 9, 12:49 pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
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("C onnectionString"))
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.CloseConne ction)
' 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

Jun 9 '07 #3

P: n/a
<m.******@uku.co.ukwrote in message
news:11**********************@q66g2000hsg.googlegr oups.com...
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

Jun 9 '07 #4

P: n/a
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
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

Jun 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.