|
|
 |
BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9  | This is the forum to discuss the Wrox book Professional ASP.NET 2.0 Special Edition by Bill Evjen, Scott Hanselman, Devin Rader, Farhan Muhammad, Srinivasa Sivakumar; ISBN: 9780470041789 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

March 3rd, 2006, 10:41 AM
|
|
Registered User
|
|
Join Date: Jan 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Listing 12-13
I cannot make Listing 12-13 work? Any ideas?
Thanks
|

March 3rd, 2006, 10:54 AM
|
|
Wrox Author
|
|
Join Date: Dec 2005
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Posting a little more info about the error your seeing would be extremely helpful.
Devin
|

March 4th, 2006, 04:29 PM
|
|
Registered User
|
|
Join Date: Jan 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by devinrader
Posting a little more info about the error your seeing would be extremely helpful.
Devin
|
Hi,Devin
On line 3: SqlAsyncResult ASyncResult;
SqlAsyncResult is not a valid class name, I replaced it with "IAsyncResult" in line 3 and in
"public void CBMethod(SQLAsyncResult ar)"
still does not run. I added "Asyncronous Precessing=True" in the connection string>
Error is:
Server Error in '/CS' Application.
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'AsyncResult' does not exist in the current context
Source Error:
Line 31:
Line 32: // Starting the asynchronous processing
Line 33: AsyncResult = Command.BeginExecuteReader(new AsyncCallback(CBMethod),
Line 34: CommandBehavior.CloseConnection);
Line 35: }
Source File: c:\Microsoft Press\Pro ASP.NET2.0 Wrox\Chapter 12\CS\Listing 12-13.aspx Line: 33
Please help, I have been strugglling with this example. When I search the Web, I tried more ten Asyn Callback in ASP.NET 2.0, they don't work.
I am using your ASP.NET 2.0 book for my Advanced ASP.NET programming class this term, it is an excellent book.
Kai
|

March 4th, 2006, 04:34 PM
|
|
Registered User
|
|
Join Date: Jan 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by devinrader
Posting a little more info about the error your seeing would be extremely helpful.
Devin
|
Hi, Devin
The following is the code I modified:
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["DSN_NorthWind"].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),
CommandBehavior.CloseConnection);
}
public void CBMethod(IAsyncResult ar)
{
SqlDataReader OrdersReader;
// Retrieving result from the asynchronous process
OrdersReader = ar.EndExecuteReader(ar);
// Displaying result on the screen
gvOrders.DataSource = OrdersReader;
gvOrders.DataBind();
}
Thanks
Kai
|

March 6th, 2006, 08:17 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Location: , , United Kingdom.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Working version of the script here:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<script runat="server">
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["DSN_NorthWind"].ConnectionString;
// Selecting top 5 records from the Orders table
Command.CommandText =
"SELECT 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();
AsyncCallback callback = new AsyncCallback(CBMethod);
ASyncResult = Command.BeginExecuteReader(callback, Command);
}
public void CBMethod(IAsyncResult result)
{
SqlCommand command = (SqlCommand)result.AsyncState;
SqlDataReader dr = command.EndExecuteReader(result);
// Displaying result on the screen
gvOrders.DataSource = dr;
gvOrders.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>The Call Back Approach</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvOrders" Width="100%" AutoGenerateColumns="False"
Runat="server">
<Columns>
<asp:BoundField HeaderText="Company Name"
DataField="CompanyName"></asp:BoundField>
<asp:BoundField HeaderText="Contact Name"
DataField="ContactName"></asp:BoundField>
<asp:BoundField HeaderText="Order Date" DataField="orderdate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Required Date" DataField="requireddate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Shipped Date" DataField="shippeddate"
DataFormatString="{0:d}"></asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|

March 6th, 2006, 06:00 PM
|
|
Registered User
|
|
Join Date: Jan 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by hardysmith
Working version of the script here:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<script runat="server">
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["DSN_NorthWind"].ConnectionString;
// Selecting top 5 records from the Orders table
Command.CommandText =
"SELECT 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();
AsyncCallback callback = new AsyncCallback(CBMethod);
ASyncResult = Command.BeginExecuteReader(callback, Command);
}
public void CBMethod(IAsyncResult result)
{
SqlCommand command = (SqlCommand)result.AsyncState;
SqlDataReader dr = command.EndExecuteReader(result);
// Displaying result on the screen
gvOrders.DataSource = dr;
gvOrders.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>The Call Back Approach</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvOrders" Width="100%" AutoGenerateColumns="False"
Runat="server">
<Columns>
<asp:BoundField HeaderText="Company Name"
DataField="CompanyName"></asp:BoundField>
<asp:BoundField HeaderText="Contact Name"
DataField="ContactName"></asp:BoundField>
<asp:BoundField HeaderText="Order Date" DataField="orderdate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Required Date" DataField="requireddate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Shipped Date" DataField="shippeddate"
DataFormatString="{0:d}"></asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
Hi, hardysmith
Thanks for posting the code.
It runs without error, but data was not displayed on the page. Did I miss some steps? I add "Asynchronous Processing=true" in the connection string.
Thanks
Kai
|

July 21st, 2006, 08:18 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi hardysmith
I tried your code, but i got the following compilation error:-
Unable to cast object of type 'System.Data.CommandBehavior' to type 'System.Data.SqlClient.SqlCommand'.
The error is here:-
SqlCommand command = (SqlCommand)result.AsyncState;
SqlDataReader dr = command.EndExecuteReader(result);
Any help?
|

August 16th, 2006, 09:17 AM
|
|
Registered User
|
|
Join Date: Aug 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Problem is that the Command.BeginExecuteReader call lets us continue dow n the page. The DB is not getting enough time to respond before the page finishes loading. The DataBind code in the callback function is therefore called AFTER the page has finished loading. Ahhh threads...
First chagne the SqlAsyncResult to IAsyncResult as mentioned above. Then when you get a blank screen of results do this...:
directly below the code of:
// starting the aynchronous process with a callback
ASyncResult = Command.BeginExecuteReader(CBMethod, Command);
Add the following:
System.Threading.Thread.Sleep(100);
DBCon.Close();
That should be enough to let the DB respond to our query so we can databind before page finished loading.
|

May 10th, 2009, 02:59 AM
|
|
Registered User
|
|
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
making thread sleep does not work properly & sometime it give the error on EndExecuteReader that is it's already ended. So try it with blank loop & declare a bool Flag variable on class level.
protected void Button5_Click(object sender, EventArgs e)
{
SqlConnection dbCon = new SqlConnection();
SqlCommand dbCmd = new SqlCommand();
IAsyncResult AsyncResult;
dbCon.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString + ";Asynchronous Processing = true;";
dbCmd.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";
dbCmd.CommandType = CommandType.Text;
dbCmd.Connection = dbCon;
Label2.Text = "<b>Connection String </b>for Async Processing : " + dbCon.ConnectionString;
dbCon.Open();
//AsyncCallback callback = new AsyncCallback(Callback_Method);
//ASyncResult = dbCmd.BeginExecuteReader(callback, dbCmd);
AsyncResult = dbCmd.BeginExecuteReader(new AsyncCallback(Callback_Method),dbCmd);
int ctr = 0;
while (!flag)
{
++ctr;
}
Label1.Text = "<b>Loop Count </b>while till the AsyncResult is not completed : " + ctr.ToString();
}
private void Callback_Method(IAsyncResult AsyncResult)
{
flag = true;
SqlCommand dbCommand = (SqlCommand)AsyncResult.AsyncState;
SqlDataReader dbReader = dbCommand.EndExecuteReader(AsyncResult);
GridView1.DataSource = dbReader;
GridView1.DataBind();
dbCommand.Connection.Close();
//dbCon.Close();
}
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| DataReader error on Listing 12-19 |
cJeffreywang |
BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 |
0 |
January 3rd, 2008 12:36 PM |
| Listing 12-1 |
cJeffreywang |
BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 |
3 |
August 16th, 2007 02:00 PM |
| Listing 3.12 |
ojm38 |
BOOK: Professional ASP.NET 2.0 AJAX ISBN: 978-0-470-10962-5 |
1 |
July 25th, 2007 06:08 PM |
| Listing 12 - 8 "Mars" |
Asp.Net |
BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 |
0 |
March 5th, 2007 07:33 AM |
| Listing 12-1 |
topm |
BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 |
1 |
March 3rd, 2006 10:53 AM |
|
 |