Closing a datareader

M

m.fisher

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
 
A

Alexey Smirnov

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

Alexey Smirnov

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
 
M

Mark Rae

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top