Thursday, September 27, 2007

Trusted WebService : ASP.NET

When you publish a WebService on IIS with Integrated Security. Maybe you got the following error: "The request failed with HTTP status 401: Access Denied". You need a twice of lines to include the credential of intranet client.

I read this article before:
http://geekswithblogs.net/ranganh/archive/2006/02/21/70212.aspx

That's it.

Tuesday, September 18, 2007

Detect Page Refresh : ASP.NET

In ASP.NET after page has done first postback, if the user makes a Refresh, the behavior is repeat the last postback. Let us imagine that we inserted data or we debit of a credit card, etc; one becomes a problem. Therefore one becomes important to handle this eventuality.

In Summary To handle a Session Flag and ViewState Flag, comparing them to know if one is about a Postback or a Refresh. For more details, see these articles.

http://www.codeproject.com/aspnet/Detecting_Refresh.asp
http://www.joel.net/code/refresh_capture.aspx
http://jarednevans.typepad.com/technoblog/2005/01/jareds_techno_b.html


Test it

Session.Abandon() : On browser is closed

Some times it is necessary to block the access of multiple users to same information. A route to do this is to register the entrance and the exit of the user to the application (DB, Application Object). It happens then that if the user closes browser without closing session in the application for some reason the Session_End does not happen. An alternative is to use the ScriptManager (MS AJAX) and a pair of lines of Javascript to notify the servant when it is closed in browser.

In summary

  1. Subscribe to the OnUnload event of the Body (<body OnUnload="Logout (); ">)
  2. Invoke the Static WebMethod (it will close session) from the function Javascript (PageMethods.AbandonSession ();)
  3. As when sailing to another page also it invokes the OnUnload event, it is necessary to make a last adjustment. All the pages of the application will be sailed from a IFRAME from a Content.aspx page. The trick is, the IFRAME makes navigation to other pages whereas Content.aspx this statics.

I read these articles before:
http://aspalliance.com/520
http://aspalliance.com/1294_CodeSnip_Handle_Browser_Close_Event_on_the_ServerSide
http://www.codeproject.com/csharp/Detect_closing_navigator.asp
http://geeks.ms/blogs/lruiz/archive/2007/02/27/controlar-cuando-el-usuario-nos-cierra-el-navegador-es-posible.aspx
http://www.programmersheaven.com/mb/ASPNET/303808/303808/readmessage.aspx

It works.

Team Explorer on Visual Studio Professional

It isn't necessary migrate to Visual Studio Team Suite for integrate with Team Server. You only install the Team Explorer in your Visual Studio Professional copy.


Look it...

Hidden Columns with values: asp:GridView

If you set Column Visible property to false, this column won't rendered. But if you want these values available, What will you do?

My trick was, set HeaderText to empty, convert the BoundField in TemplateField, and use a HiddenField control. The effect the column won't be visible. Also you can use the controls array to access to value property.

<Columns><asp:BoundField DataField="CompanyCode"
HeaderText="Company" SortExpression="CompanyCode" />
...
<Columns>
<asp:TemplateField><ItemTemplate><asp:HiddenField
id="hf1" Value='<%# Bind("CompanyCode") %>'
runat="server"></asp:HiddenField></ItemTemplate>
...
// accesing the value property
int tmpID =
Convert.ToInt32(((HiddenField)GridView1.SelectedRow.Cells[3].Controls[1]).Value);


And run it.

Cached Authentication Issue : Windows Authentication

My friend Jose Atencio, worked with Web Application managing Windows Roles and Users. After some testing, The changes are applied on client when they log off. While they are logged the changes are not reflected. It seems Windows Authentication information is cached on client at time when user log in.

Don't forget this issue in your testing.

Maybe, Do somebody have a solution?

IsNumeric Method : Regular Expression

Using int.Parse(expression) method throw an error if expression doesn't numeric; Instead of that, an alternative, is use custom method regular expression based. It returns a boolean value.

public static bool IsNumeric(string theValue){
Regex _isNumber = new
Regex(@"^\d+$");
Match m = _isNumber.Match(theValue);
return
m.Success;}

And run it.

Application_Error, Global.asax : Exception Handling

When I tried manage exceptions in Application_Error event, sometimes throw an error:

Cannot redirect after http headers have been sent

My trick for avoid this exception:

void Application_Error(object sender, EventArgs e)
{
// handle generic application errors
Exception ex = Server.GetLastError();
SiteHelper.HandleExceptionInLog(ex);
Server.ClearError();
Response.Clear();
Server.Execute("Error.aspx");
}

I read these articles before:
http://blogs.msdn.com/kaevans/archive/2003/07/07/9791.aspx
http://www.codeguru.com/csharp/.net/net_asp/miscellaneous/article.php/c12385/
http://forums.asp.net/t/44586.aspx

It works for me.

Thursday, September 13, 2007

Protected members are ignored : XmlSerialization

After some XmlSerialization testing, I saw, the child class, with protected members, the serialization result ignore those members. In sumary all serializable members most be public in the base class.

Does exist another lesson learned related?

Wednesday, September 12, 2007

Try...Finally Behavior

I needed to close a port after I finished to use it. The problem was if the communication fail, after the port was opened; I would like force it to close. The Try...Finally behavior is after Exception happened the immediately Finally code segment is executed and the parent Catch get the execution control.

In summary: It closes port always, but doesn't handle exceptions. It works for me.

Do have someone another alternative?
Google