Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > BOOK: Professional ASP.NET 3.5 SP1 Edition: In C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET 3.5 SP1 Edition: In C# and VB
This is the forum to discuss the Wrox book Professional ASP.NET 3.5 SP1 Edition: In C# and VB by Bill Evjen, Scott Hanselman, and Devin Rader; ISBN: 9780470478264
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 3.5 SP1 Edition: In C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 2nd, 2010, 04:54 PM
Authorized User
Points: 71, Level: 1
Points: 71, Level: 1 Points: 71, Level: 1 Points: 71, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2009
Posts: 17
Thanks: 0
Thanked 1 Time in 1 Post
Default Problems in Listing 8-35: Asynchronous command processing using the callback approach

I've run into a number of problems with listing 8-35 (in vb.net). Following is a list:

Code:
Dim AsyncResult as SqlAsyncResult
I don't believe there is an SqlAsyncResult class in the .Net framework. I believe, based upon the prior code and useage that this should be IAsyncResult

Code:
AsyncResult = Command.BeginExecuteReader(New AsyncCallback(AddressOf CBMethod), CommandBehavior.CloseConnection)
I had some trouble with this code as well. I finally figured out that my problem was the result of naming my page "AsyncCallback" which created a class of the same name, which conflicted with the System.AsyncCallback class.

In the CBMethod() subroutine, there's also a problem:
Code:
OrderReader = ar.EndExecuteReader(ar)
In this case, they're expecting ar to act as both a SQLCommand object (with the EndExecuteReader() method) and an object that implements the IAsyncResult interface (as the argument to the EndExecuteReader).

I haven't quite figured this one out yet. If they want to use the SQLCommand object from the Page_Load, then it's declaration will have to be moved outside of the Page_Load procedure; otherwise, it won't be available to the CBMethod. I've done this, and it compiles, but my grid doesn't populate.

My CBMethod() sub looks like this (vb):
Code:
    
Public Sub CBMethod(ByVal ar As IAsyncResult)
        Dim OrdersReader As SqlDataReader

        OrdersReader = command.EndExecuteReader(ar)

        'If OrdersReader.HasRows Then
        '   Do While OrdersReader.Read()
        '       Dim x As Label = New Label
        '       x.Text = OrdersReader("CompanyName").ToString()
        '       Me.Controls.Add(x)
        '   Loop
        'End If

        gvOrders.DataSource = OrdersReader
        gvOrders.DataBind()
End Sub
I've included some commented out code that I used to confirm, when stepping through in the debugger, that the CBMethod was, in fact being called, and that the OrdersReader was populated with data. In spite of confirming that the data reader does have data, the grid still wasn't populating with the OrdersReader was bound to it...

Just to be complete, here's the rest of my codebehind (just for the class):
Code:
    Dim command As SqlCommand

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dbConnection As SqlConnection = New SqlConnection()
        Dim AsyncResult As IAsyncResult

        dbConnection.ConnectionString = ConfigurationManager.ConnectionStrings("DSN_Northwind").ConnectionString

        command = New SqlCommand()
        'Selecting top 5 records from the Orders table
        command.CommandText = "SELECT TOP 5 Customers.CompanyName, Customers.ContactName, Orders.OrderID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate FROM Orders, Customers WHERE Orders.CustomerID = Customers.CustomerID ORDER BY Customers.CompanyName, Customers.ContactName"
        command.CommandType = CommandType.Text
        command.Connection = dbConnection

        dbConnection.Open()

        'Starting the asynchronous processing
        AsyncResult = command.BeginExecuteReader(New AsyncCallback(AddressOf CBMethod), CommandBehavior.CloseConnection)
    End Sub
If anyone has gotten this to work, I'd love to see the corrected code.

Thanks in advance,

C.

Last edited by Pegasus40218; July 2nd, 2010 at 06:33 PM.
  #2 (permalink)  
Old July 7th, 2010, 09:59 AM
Authorized User
Points: 71, Level: 1
Points: 71, Level: 1 Points: 71, Level: 1 Points: 71, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2009
Posts: 17
Thanks: 0
Thanked 1 Time in 1 Post
Default

Well, I solved my own problem. Prior to posting this, I had proven to myself that the callback was working -- the callback function was being called, and that the datareader was getting data; but, for the life of me, I couldn't figure out why the data was not displaying. After a couple of hours, of tinkering, I had decided to move on with the rest of the book.

Then, this morning, the solution came to me: it was a sequence of events problem. The page was being rendered BEFORE the data was being bound to the grid. Because the database call is being executed asynchronously, processing for the rest of the page continues without regard to the status of that asynchronous call. So, to get the data being bound to the grid to display properly, I had to delay rendering of the page until the data was bound. So, here's what I did:

1. Add a class-level boolean variable (inside the class, but not inside any procedures) called AsyncDone, and initialize it to false.

Code:
Dim AsyncDone as Boolean = False
2. In the Page_Load event handler, at the very end (after the BeginExecuteReader is called), I added a loop to delay processing until the asynchronous callback had been completed.

Code:
While Not AsyncDone
end while
3. In the callback method (CBMethod), I added a line of code at the end of the procedure to indicate that the Asynchronous callback had been completed.
Code:
AsyncDone = True
Voila! The page's pre-render and render events are delayed until the asynchronous callback has been completed, and the data in the datareader is displayed on the grid!
  #3 (permalink)  
Old May 29th, 2011, 03:20 PM
Authorized User
Points: 73, Level: 1
Points: 73, Level: 1 Points: 73, Level: 1 Points: 73, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2007
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems in Listing 8-35: Asynchronous command processing using the callback approach

Thanks for posting this solution. I was having the same problem with ASP.NET 4 for C#. A reader in that forum posted the same question. I will prvide a link to your solution in that forum.
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Spring based Dao with callback Vs without callback kasipandian Spring 0 November 9th, 2009 08:26 AM
Page 35 Errata LongJohnSilver BOOK: Professional Visual Basic 2008 ISBN: 978-0-470-19136-1 2 January 9th, 2009 09:26 AM
Page 35 error. Patrick Cambria BOOK: Professional Visual Basic 2008 ISBN: 978-0-470-19136-1 0 December 24th, 2008 07:36 AM
Asynchronous Command Execution in ADO.NET 2.0 suresh_ala Visual Studio 2005 1 February 8th, 2007 10:31 AM



All times are GMT -4. The time now is 07:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.