View Single Post
  #1 (permalink)  
Old November 7th, 2010, 06:03 PM
walbalooshi walbalooshi is offline
Authorized User
 
Join Date: Oct 2009
Posts: 22
Thanks: 2
Thanked 10 Times in 7 Posts
Default 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:

Code:
AsyncResult = Command.BeginExecuteReader(new AsyncCallback(CBMethod), Command, CommandBehavior.CloseConnection);
Code:
    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;
        gvOrders.DataBind();
    }
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.

Thanks.