Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 March 3rd, 2006, 09:41 AM
kai kai is offline
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Listing 12-13

I cannot make Listing 12-13 work? Any ideas?

Thanks

Reply With Quote
  #2 (permalink)  
Old March 3rd, 2006, 09:54 AM
Wrox Author
 
Join Date: Dec 2005
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Posting a little more info about the error your seeing would be extremely helpful.

Devin
Reply With Quote
  #3 (permalink)  
Old March 4th, 2006, 03:29 PM
kai kai is offline
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #4 (permalink)  
Old March 4th, 2006, 03:34 PM
kai kai is offline
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #5 (permalink)  
Old March 6th, 2006, 07:17 AM
Registered User
 
Join Date: Mar 2005
Location: , , United Kingdom.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>


Reply With Quote
  #6 (permalink)  
Old March 6th, 2006, 05:00 PM
kai kai is offline
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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



Reply With Quote
  #7 (permalink)  
Old July 21st, 2006, 07:18 AM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
Reply With Quote
  #8 (permalink)  
Old August 16th, 2006, 08:17 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Reply With Quote
  #9 (permalink)  
Old May 10th, 2009, 01:59 AM
Registered User
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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();
}
Reply With Quote
  #10 (permalink)  
Old June 14th, 2011, 02:49 PM
Authorized User
Points: 222, Level: 4
Points: 222, Level: 4 Points: 222, Level: 4 Points: 222, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2007
Location: Phoenix, AZ, USA.
Posts: 48
Thanks: 4
Thanked 0 Times in 0 Posts
Default Dataless Page

I tried what you suggested, but I am getting a page with no information on it. Here is my code:

Code:
<%@PageLanguage="C#" %>
<%@ImportNamespace="System.Data" %>
<%@ImportNamespace="System.Data.SqlClient" %>
<%@ImportNamespace="System.Configuration" %>
<%@ImportNamespace="System.Threading" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
SqlCommand Command = newSqlCommand();
IAsyncResult AsyncResult;
protectedvoid Page_Load(object sender, EventArgs e)
{
SqlConnection DBCon;
DBCon = newSqlConnection();
DBCon.ConnectionString =
ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
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();
AsyncResult = Command.BeginExecuteReader(newAsyncCallback(CBMethod), CommandBehavior.CloseConnection);
Thread.Sleep(5000);
DBCon.Close();
}
publicvoid CBMethod(IAsyncResult ar)
{
SqlDataReader OrdersReader;
OrdersReader = Command.EndExecuteReader(AsyncResult);
gvOrders.DataSource = OrdersReader;
gvOrders.DataBind();
}
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Untitled Page</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="gvOrders"runat="server"AutoGenerateColumns="False"Width="100%">
<Columns>
<asp:BoundFieldDataField="CompanyName"HeaderText="Company Name"/>
<asp:BoundFieldDataField="ContactName"HeaderText="Contact Name"/>
<asp:BoundFieldDataField="OrderDate"DataFormatString="{0:d}"HeaderText="Order Date"/>
<asp:BoundFieldDataField="RequiredDate"DataFormatString="{0:d}"HeaderText="Required Date"/>
<asp:BoundFieldDataField="ShippedDate"HeaderText="Shipped Date"/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Any help will be greatly appreciated

Tom
__________________
Thomas G Magaro
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
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 11:36 AM
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 01:00 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 06: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 09:53 AM



All times are GMT -4. The time now is 08:55 AM.


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