PageAsyncTask Problem

PageAsyncTask Problem

am 08.01.2008 00:55:05 von mmorrison93

I'm trying to set up a test asynchronously executing page that will
call a database stored procedure and then simply add text to a Label
control name mylabel However after executing the BeginEventHandler,
the EndEventHandler is not called and I have no idea why any help is
appreciated. My .aspx file is minimal the only thing on the page is my
label.

In my code behind file i have the following:

protected void Page_Load(object sender, System.EventArgs e)
{
PageAsyncTask task = new PageAsyncTask(
new BeginEventHandler(BeginTask),
new EndEventHandler(EndTask),
null,
null);

RegisterAsyncTask(task);
Page.ExecuteRegisteredAsyncTasks();
}

public IAsyncResult BeginTask(object sender, EventArgs e,
AsyncCallback cb, object state)
{
using (SqlConnection cn = new
SqlConnection(WebConfigurationManager.ConnectionString.
["conn"].ConnectionString))
{
cmd = new SqlCommand("sp_AnheiserBusch_GetWeeks", cn);
cmd.CommandType = CommandType.StoredProcedure;
IAsyncResult ar;

try
{
cn.Open();
ar = cmd.BeginExecuteReader(cb, state);
}
finally
{
cn.Close();
}

return ar;
}
}

public void EndTask(IAsyncResult ar)
{
mylabel.Text = "EndTask";
}

Thanks.

RE: PageAsyncTask Problem

am 08.01.2008 01:48:02 von brucebarker

as you close the connection right after starting the async query, it should
throw an error. you can not close the connection until the results are read
with:

cmd.EndExecuteReader()

which should be called in the end handler.

-- bruce (sqlwork.com)


"mmorrison93@gmail.com" wrote:

> I'm trying to set up a test asynchronously executing page that will
> call a database stored procedure and then simply add text to a Label
> control name mylabel However after executing the BeginEventHandler,
> the EndEventHandler is not called and I have no idea why any help is
> appreciated. My .aspx file is minimal the only thing on the page is my
> label.
>
> In my code behind file i have the following:
>
> protected void Page_Load(object sender, System.EventArgs e)
> {
> PageAsyncTask task = new PageAsyncTask(
> new BeginEventHandler(BeginTask),
> new EndEventHandler(EndTask),
> null,
> null);
>
> RegisterAsyncTask(task);
> Page.ExecuteRegisteredAsyncTasks();
> }
>
> public IAsyncResult BeginTask(object sender, EventArgs e,
> AsyncCallback cb, object state)
> {
> using (SqlConnection cn = new
> SqlConnection(WebConfigurationManager.ConnectionString.
> ["conn"].ConnectionString))
> {
> cmd = new SqlCommand("sp_AnheiserBusch_GetWeeks", cn);
> cmd.CommandType = CommandType.StoredProcedure;
> IAsyncResult ar;
>
> try
> {
> cn.Open();
> ar = cmd.BeginExecuteReader(cb, state);
> }
> finally
> {
> cn.Close();
> }
>
> return ar;
> }
> }
>
> public void EndTask(IAsyncResult ar)
> {
> mylabel.Text = "EndTask";
> }
>
> Thanks.
>

RE: PageAsyncTask Problem

am 08.01.2008 01:59:02 von pbromberg

There is no particular advantage in using Async page methods for a single
database call, since page processing is halted until the End method is
complete. If you have multiple calls that you want to have executed in
parallel, then it makes more sense.
The way you have your code now is nothing more than an "exercise".
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com


"mmorrison93@gmail.com" wrote:

> I'm trying to set up a test asynchronously executing page that will
> call a database stored procedure and then simply add text to a Label
> control name mylabel However after executing the BeginEventHandler,
> the EndEventHandler is not called and I have no idea why any help is
> appreciated. My .aspx file is minimal the only thing on the page is my
> label.
>
> In my code behind file i have the following:
>
> protected void Page_Load(object sender, System.EventArgs e)
> {
> PageAsyncTask task = new PageAsyncTask(
> new BeginEventHandler(BeginTask),
> new EndEventHandler(EndTask),
> null,
> null);
>
> RegisterAsyncTask(task);
> Page.ExecuteRegisteredAsyncTasks();
> }
>
> public IAsyncResult BeginTask(object sender, EventArgs e,
> AsyncCallback cb, object state)
> {
> using (SqlConnection cn = new
> SqlConnection(WebConfigurationManager.ConnectionString.
> ["conn"].ConnectionString))
> {
> cmd = new SqlCommand("sp_AnheiserBusch_GetWeeks", cn);
> cmd.CommandType = CommandType.StoredProcedure;
> IAsyncResult ar;
>
> try
> {
> cn.Open();
> ar = cmd.BeginExecuteReader(cb, state);
> }
> finally
> {
> cn.Close();
> }
>
> return ar;
> }
> }
>
> public void EndTask(IAsyncResult ar)
> {
> mylabel.Text = "EndTask";
> }
>
> Thanks.
>

Re: PageAsyncTask Problem [OT]

am 08.01.2008 10:36:30 von maciek kanski

mmorrison93@gmail.com wrote:
> cmd = new SqlCommand("sp_AnheiserBusch_GetWeeks", cn);
it's better not to name stored procedures with sp_
http://msdn2.microsoft.com/en-us/library/ms190669.aspx