473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Closing a datareader

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
4 1413
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
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
<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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steven Blair | last post by:
Hi, I am writing an application using a 3 tier model (Client, Business Layer and DB) The DB layer creates a OdbcDataReader object back up to the Client where the data is read and displayed on...
7
by: DS | last post by:
Is there a way to automatically close the data reader connection? I'm using the MS Data Access Application block to substantially {entirely} separate the data access layer (DAL) from the business...
0
by: Brent | last post by:
I wrote a simple class to supply a datareader object (code at bottom of post) back to a page. I access it on the APSX page like this: Dim myDR as New getDataReader(strSQL) If myDR.ResultFailed...
13
by: Simon Harvey | last post by:
Hi All, I have a colleague that I wprk with that develops using ASP. I develop using ASP.net. He seems to make sites much faster than me and I am wondering if its because of the two different...
3
by: Paolo Pignatelli | last post by:
I have a Function in a Class File that uses ApplicationBlocks: Public Function GetProductsByCategory(ByVal CategoryID As Integer) Dim myConnection As SqlConnection = New...
7
by: Arsalan | last post by:
I have a function which return datareader Public Shared Function ReturnDReader(ByVal query As String) As OleDbDataReader Dim Connection_String As String =...
7
by: Tumurbaatar S. | last post by:
Is it so important to close database connections? As I understand, after processing a request and sending a response, page object destroyed. So all used connections also destroyed. Yes?
2
by: tshad | last post by:
I have a section of code that is giving me an error: ***************************************************************** Invalid attempt to Read when reader is closed. Description: An unhandled...
3
by: rockdale | last post by:
I am using Microsoft.Practices.Enterprise.Library When I use IDataReader I saw this line "It is the responsibility of the caller to close the connection and reader when finished." I was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.