Archive for October, 2005

Going to the async way and doing things Async way realted to new threads and improper way of doing the same leads to amny poor design and has significant issues .On the other hand doing things the async way provides a lot more flexibility and improve the performance a lot .Micosoft has keep this in mind and wisely provided many enhanced features of doing things the async way in vs2005-and asp.net2.0
Now you have the async way of executing ur reader usign the beginexecuting reader,the backgroundworker class and ASync pages as well as you have now the new anonymous methods.
Out of all these the challenges to handle the Async Calls in ASP.NET was always have some new overheads and doing the same in ASP.NET in an efficient manner is assocoiated with more overhead.If you want an efficient mechanism in asp.net 1.1 Fritz onion ‘s article on the same you should must go thru
Async pages in Asp.net 2.0 is one of the great features and For sure it is gonna goign to be a  must for every efficient Architect and developer .The async pages is a very neat and cool feature and declaring the Async=true in page directive gives  you all the necessary interfaces..and later you need to implement ur mechanism of async call based on it.
I am not going to write in details of the async pages wrokign here  as Jeff Prosise  ‘s article  on MSDNMAG here is describing the same in dept.
Now about the small gotcha:-If you are following the same article and runnign the code you ll found that :-
The Databind.aspx is not returnign the result.The problem was however due to the fact that the Page_PreRenderComplete method is getting called twice and this behavior is there for all pages including the webservice calls.I ve the RC build and I am now goign to send mail to verify this behavior.if you ve faced similar problem here is the workaround:-

to restrict the Page_PreRenderComplete  get called twice you can use

void

EndAsyncOperation(IAsyncResult ar)

{

_reader = _command.EndExecuteReader(ar);

this.PreRenderComplete -= new EventHandler(Page_PreRenderComplete);

}

 else make check on empty datareader

protected

void Page_PreRenderComplete(object sender, EventArgs e){

if (_reader.Read())

{

Gridview1.DataSource = _reader;

Gridview1.DataBind(); 

}

 }

but still the page_prerendercomplete ll invoked twice .
However the functionality of providing Asynchronous access is not within Page and you can have the AsyncTask as well.Read the same in the above Article from JEff as well as in fritz’s post here.
The uniqueness of the AsyncTask is that you can make multiple call,have a control over the timeout ,pass the state .
Further the above mentioned problem of  Page_PreRenderComplete firing twice for the Async Pages didn’t happens there.
(GOT THE ROOT OF THE PROBLEM IT SEEMS THAT THE PAGE_PRERENDER HOOKED AUTOMATICALLY ONCE YOU SET ASYNC=TRUE THUS WHEN i MYSELF IN CODE HOOKED THE SAME IT FIRED TWICE.THIS was not documneted but i should have noticed it before  ..thansks to olson for pointing the same)

if

(!IsPostBack){

// Hook PreRenderComplete event for data binding//this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);// Register async methodsAddOnPreRenderCompleteAsync(

newBeginEventHandler(BeginAsyncOperation),

newEndEventHandler(EndAsyncOperation));

}

The Prerender now  fired once only .
 
 
 
Here you go for the async tasks :note i am not using timeout here:-
 
 

private

SqlConnection _connection;privateSqlCommand _command;

privateSqlDataReader _reader;

protectedvoid Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

PageAsyncTask task = newPageAsyncTask

(

newBeginEventHandler(BeginAsyncOperation),

newEndEventHandler(EndAsyncOperation),

null,

null

);

RegisterAsyncTask(task);

}

}

IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)

{

string connect = WebConfigurationManager.ConnectionStrings[“PubsConnectionString”].ConnectionString;

_connection =

newSqlConnection(connect);

_connection.Open();

_command =

newSqlCommand(“SELECT title_id, title, price FROM titles”, _connection);

return _command.BeginExecuteReader(cb, state);

}

void EndAsyncOperation(IAsyncResult ar)

{

_reader = _command.EndExecuteReader(ar);

}

protectedvoid Page_PreRenderComplete(object sender, EventArgs e)

{

GridView1.DataSource = _reader;

GridView1.DataBind();

}

There is a unique way to take ur asp.net application offline now :-
How can you take ur asp.net  application offline the trick is really  simple and well built in.Thanks eric for the finding and scott for providing a brief info on the behavior.The trick is really cool and you only  need to place a file called app_offline.htm  in the webapp root directory and once you remove ur app is back online.read more about it here
 
 Updates:-
Are you getting into a Gotcha When the app_Offline.htm is less then 512 bytes ? See Scott’s Entry here

Uisng asp.net 2.0 profile

Posted: October 3, 2005 in Uncategorized

 

Configure the web.config to point to the DBProfile Database and add 2 properties to the profile

 

<

configurationxmlns=http://schemas.microsoft.com/.NetConfiguration/v2.0><

connectionStrings>

<

addname=myConnectionStringconnectionString=Server=sreeman;Trusted_Connection=true;database=DBProfile

providerName=System.Data.SqlClient />

</

connectionStrings>

<appSettings/>

<

system.web>

<

profiledefaultProvider=MyProfileProvider>

<

providers>

<

addname=MyProfileProvidertype=System.Web.Profile.SqlProfileProviderconnectionStringName=myConnectionString/>

</

providers>

<

properties><addname=FirstNametype=System.StringdefaultValue=Shreeman/><addname=LastNametype=System.StringdefaultValue=dash/></properties>

</

profile>

<

compilationdebug=true/>

</

system.web>

</

configuration>

 

if

(!IsPostBack)

{

Profile.FirstName = “Ash”;

Profile.LastName =

“rai”;

}

TextBox1.Text = Profile.FirstName;

TextBox2.Text = Profile.LastName;

string strusername = Profile.UserName;

ProfileManager.DeleteProfile(strusername);

 

 

Craete a simple application put a datatgridview and load it with viewstate enabled.

code:-

 

protected

 

void Page_Load(object sender, EventArgs e){

if (!Page.IsPostBack)

{

string[] arr = newstring[1300];

for (int i = 0; i <= 1299; i++)

{

arr[i] =

“shreeman”;

}

GridView1.DataSource = arr;

GridView1.DataBind();

 

}

}

 
Now See the viewstate size get generated for each postback.
 
Now we ll create an adapetr and store the viewstate in Session .
 
 
 
 
Here is the adapter Code:-

using

System;using

System.Collections.Generic;using

System.Text;namespace

ClsDemoAdapter{

publicclassviewstateadapter : System.Web.UI.Adapters.PageAdapter

{

publicoverride System.Web.UI.PageStatePersister GetStatePersister()

{

returnnew System.Web.UI.SessionPageStatePersister(this.Page);

 

}

}

}

 

 

Now in the same application add a reference to the Above assembly ClsDemoadapter.viewstateadapter.

add a App_Broiwser Folder and add a Browser file Viewstateadapter.browser

and chnage the content of the browser as the following:-

 

<

browsers>

<browserrefID=Default>

<

controlAdapters>

<

adapter

controlType=System.Web.UI.Page

adapterType=ClsDemoAdapter.viewstateadapter

/>

</controlAdapters>

</

browser>

</browsers>

 

Now Runt he application and see the Viewstate by Right Click –>view Source and you are viewstate get stored in Session .

 

 also check out the interestign behavior

So what’s new in ASP.NET 2.0? There’s a new attribute in the <pages> section named maxPageStateFieldLength

<pages maxPageStateFieldLength=”5″ />

 

Read more here in dino’s blog here  http://weblogs.asp.net/despos/archive/2005/09/28/426137.aspx