p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=554)
-   -   Callback Asynchronous Command Execution using ADO.Net (http://p2p.wrox.com/showthread.php?t=81512)

walbalooshi November 7th, 2010 06:03 PM

Callback Asynchronous Command Execution using ADO.Net
I am having problems with the Callback Approach section of Chapter 8. Using the code provided in Listing 8-35 page 368-369.

1. There is no Class of type SQLAsyncResult as is used in the callback method CBMethod and searching through the internet shows that it existed in the ADO.Net 2 days, but isn't available in the ADO.Net 4.

2. There is no overload of SqlCommand.BeginExecuteReader that takes a callback function and commandbehavior only.

So, the absence of a SQLAsyncResult class meant that I had to change a few things in the code and I ended up with the following:


AsyncResult = Command.BeginExecuteReader(new AsyncCallback(CBMethod), Command, CommandBehavior.CloseConnection);

    public void CallbackMethod(IAsyncResult ar)
        SqlDataReader OrdersReader;
        SqlCommand cmd = (SqlCommand)ar.AsyncState;

        // Retrieving result from the asynchronous process
        OrdersReader = cmd.EndExecuteReader(ar);

        // Displaying result on the screen
        gvOrders.DataSource = OrdersReader;

So, the problem I am having with this code is that I am not always getting the grid to show up. Most of the times I am getting a blank page, sometimes I get a Grid with a few fields filled in, and rarely I get the full Grid.

So, if anyone can please give me some insight on what I might be doing wrong, I would be highly appreciative.


mhanson May 29th, 2011 02:43 PM

Callback Asynchronous Command Execution using ADO.Net

I was having the same problem. I found the answer in another Wrox forum by Pegasus40218:

BOOK: Professional ASP.NET 3.5 SP1 Edition: In C# and VB,

It is in VB but easy to convert to C#. This is an event issue. The Page_Load event completes before the data is bound to the grid. This is why we sometimes see the data. Add a loop to delay the Page_Load event. I did so my adding a bool AsyncDone at the class level. Adding a While(AsyncDone == false) loop at the end of the Page_Load event. And finally adding the AsyncDone field at the end of the CBMethod callback. The added lines of code are bold. Below is the listing. I hope this helps


public partial class Listing_08_35a : System.Web.UI.Page
bool AsyncDone = false;
protected void Page_Load(object sender, EventArgs e)
SqlConnection DBCon;
SqlCommand Command = new SqlCommand();
IAsyncResult ASyncResult;
DBCon = new SqlConnection();
Command = new SqlCommand();
DBCon.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
// 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 = DBCon;
// Starting the asynchronous processing
ASyncResult = Command.BeginExecuteReader(new AsyncCallback(CBMethod), Command, CommandBehavior.CloseConnection);
while(AsyncDone == false)
public void CBMethod(IAsyncResult ar)
SqlCommand command = (SqlCommand)ar.AsyncState;
// Retrieving result from the asynchronous process
SqlDataReader OrdersReader = command.EndExecuteReader(ar);
// Displaying result on the screen
gvOrders.DataSource = OrdersReader;
AsyncDone = true;

This is frustrating to me. I can't believe we are the only ones experiencing this problem. Also, this problem existed in ASP.NET 3.5 SP1. Why wasn't this corrected in subsequent editions.

Not sure what the CODE and QUOTE tags do. I thought they would format the code. Sorry.

sayan751 September 4th, 2012 01:19 PM

I know it is a bit late, but I've found a workaround for this problem. My solution is same like as described by mhanson. But instead of stalling the entire code execution in while loop, I only delayed the rendering of the gridview. I've explained the same in my blog. Anyone interested can check this link: http://programersnotebook.blogspot.i...oach-in_4.html

MaryStrobel April 13th, 2013 07:53 AM

i guess you need to look out for the pointers you have used.

All times are GMT -4. The time now is 06:09 PM.

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