Wrox Programmer Forums

Need to download code?

View our list of code downloads.

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

You are currently viewing the BOOK: Professional ASP.NET 4 : 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
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 7th, 2010, 05:03 PM
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.
Reply With Quote
  #2 (permalink)  
Old May 29th, 2011, 02:43 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 Callback Asynchronous Command Execution using ADO.Net

Hello

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,
http://p2p.wrox.com/book-professiona...tion-c-vb-497/

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

Code:
 
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;
DBCon.Open();
 
// 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;
gvOrders.DataBind();
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.
Reply With Quote
  #3 (permalink)  
Old September 4th, 2012, 01:19 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
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
Reply With Quote
  #4 (permalink)  
Old April 13th, 2013, 07:53 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


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
Problems in Listing 8-35: Asynchronous command processing using the callback approach Pegasus40218 BOOK: Professional ASP.NET 3.5 SP1 Edition: In C# and VB 2 May 29th, 2011 02:20 PM
Spring based Dao with callback Vs without callback kasipandian Spring 0 November 9th, 2009 07:26 AM
Asynchronous Command Execution in ADO.NET 2.0 suresh_ala Visual Studio 2005 1 February 8th, 2007 09:31 AM
Shell command execution error toshesh VB Databases Basics 3 January 7th, 2006 02:17 AM
asynchronous messaging in .Net jimbeam36 .NET Web Services 0 November 9th, 2004 06:40 AM



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


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