p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 (http://p2p.wrox.com/book-professional-asp-net-2-0-special-edition-isbn-978-0-7645-7610-2-isbn-978-0-470-04178-9-229/)
-   -   Listing 12-13 (http://p2p.wrox.com/book-professional-asp-net-2-0-special-edition-isbn-978-0-7645-7610-2-isbn-978-0-470-04178-9/39313-listing-12-13-a.html)

kai March 3rd, 2006 10:41 AM

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

Thanks


devinrader March 3rd, 2006 10:54 AM

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

Devin

kai March 4th, 2006 04:29 PM

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


kai March 4th, 2006 04:34 PM

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


hardysmith March 6th, 2006 08:17 AM

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>



kai March 6th, 2006 06:00 PM

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




monfu July 21st, 2006 08:18 AM

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?

owen_is August 16th, 2006 09:17 AM

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.


Saurabh Aatrey May 10th, 2009 02:59 AM

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();
}

flashmanTom June 14th, 2011 03:49 PM

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


All times are GMT -4. The time now is 03:39 AM.

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