Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 3rd, 2006, 10:41 AM
kai kai is offline
Registered User
 
Join Date: Jan 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Listing 12-13

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

Thanks

 
Old March 3rd, 2006, 10:54 AM
Wrox Author
 
Join Date: Dec 2005
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
 
Old March 4th, 2006, 04:29 PM
kai kai is offline
Registered User
 
Join Date: Jan 2006
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

 
Old March 4th, 2006, 04:34 PM
kai kai is offline
Registered User
 
Join Date: Jan 2006
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

 
Old March 6th, 2006, 08:17 AM
Registered User
 
Join Date: Mar 2005
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>


 
Old March 6th, 2006, 06:00 PM
kai kai is offline
Registered User
 
Join Date: Jan 2006
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



 
Old July 21st, 2006, 07:18 AM
Authorized User
 
Join Date: Nov 2005
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?
 
Old August 16th, 2006, 08:17 AM
Registered User
 
Join Date: Aug 2006
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.

 
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();
}
 
Old June 14th, 2011, 02:49 PM
Authorized User
 
Join Date: Dec 2007
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





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 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 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





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