Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

Closing a datareader

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
m.fisher@uku.co.uk - 09 Jun 2007 10:56 GMT
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
Alexey Smirnov - 09 Jun 2007 11:49 GMT
On Jun 9, 11:56 am, m.fis...@uku.co.uk wrote:
> Hi
>
[quoted text clipped - 46 lines]
>
> End Sub

you have to close it

((SqlDataReader)MyList.DataSource).Close();
Alexey Smirnov - 09 Jun 2007 12:01 GMT
> On Jun 9, 11:56 am, m.fis...@uku.co.uk wrote:
>
[quoted text clipped - 54 lines]
>
> - 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
Mark Rae - 09 Jun 2007 12:22 GMT
> but it's already closed after it has been bound to the control.

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

Signature

http://www.markrae.net

Mark Rae - 09 Jun 2007 12:02 GMT
> 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();

Signature

http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.