Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 20th, 2007, 09:55 AM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default [RESOLVED]Asynchronous Pages and SqlDataAdapter

Hey all,

I just recently read a great article by Jeff Prosise on Asynchronous Programming. I'm developing an external website for our company and using asynchronous pages seems to be the way to go to get efficient use out of my threads, especially as the website will be making some pretty heavy duty database calls. I'm still fairly new to ASP.NET 2.0 and I've run across a problem that I could really use some help with.

Here is the example code the author uses:

Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim task As New PageAsyncTask(New BeginEventHandler(AddressOf BeginAsyncOperation), _
            New EndEventHandler(AddressOf EndAsyncOperation), _
            New EndEventHandler(AddressOf TimeoutAsyncOperation), _
            Nothing)
        RegisterAsyncTask(task)
    End Sub

    Function BeginAsyncOperation(ByVal sender As Object, ByVal e As EventArgs, ByVal cb As AsyncCallback, ByVal state As Object) As IAsyncResult
        Dim connect As String = WebConfigurationManager.ConnectionStrings("AsyncPubs").ConnectionString
        _connection = New SqlConnection(connect)
        _connection.Open()
        _command = New SqlCommand("SELECT title_id, title, price FROM titles", _connection)
        Return _command.BeginExecuteReader(cb, state)
    End Function

    Sub EndAsyncOperation(ByVal ar As IAsyncResult)
        _reader = _command.EndExecuteReader(ar)
    End Sub

    Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As EventArgs)
        Output.DataSource = _reader
        Output.DataBind()
    End Sub


This actually works quite well, except for my purposes, my grid uses pagination. You can't use pagination with an SqlDataReader. I would have to use an SqlDataAdapter and fill a DataSet and then bind the DataSet to the grid. My problem is I'm not sure how I would do this.

It seems to me, the way this works is the SqlDataReader begins the execution in Function BeginAsyncOperation and the result is passed back to it in the Sub EndAsyncOperation. How would I adapt this to use the SqlDataAdapter instead of the SqlDataReader? The key for this to work seems to be in the BeginExecuteReader and EndExecuteReader methods. I'm not sure if there is a match for these methods with SqlDataAdapter.

Thanks,
Tal
 
Old August 22nd, 2007, 02:05 PM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

For anyone interested, the solution to my problem was quite simple. I used a DataTable and then used the Load method to fill it with the results of the Reader. So basically, I just changed

Code:
Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As EventArgs)
    Output.DataSource = _reader
    Output.DataBind()
End Sub
to

Code:
Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As EventArgs)
    _datatable.Load(_reader)
    Output.DataSource = _datatable
    Output.DataBind()
End Sub
Doing it this way gets around the whole problem of the SqlDataReader not supporting pagination.

Tal





Similar Threads
Thread Thread Starter Forum Replies Last Post
sqldataadapter vgsgowrisankar C# 2005 7 April 23rd, 2008 04:32 PM
SqlDataAdapter DanRoche .NET Framework 1.x 3 August 20th, 2007 04:04 AM
SqlDataAdapter does not have its properties thaopham215 ASP.NET 2.0 Basics 13 December 6th, 2006 05:15 PM
SqlDataAdapter Issue Tom Wing VB.NET 2002/2003 Basics 2 February 6th, 2006 01:43 PM
sqlDataAdapter [email protected] ADO.NET 1 April 8th, 2004 12:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.