Thursday, December 29, 2005

Batch Config file

Batch Config fileThe new <location> element in .NET 2.0 lets you define the settings for specific location. That includes subdirectories in web applications.

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">;<system.web><!-- Regularconfiguration settings go here. --></system.web><location path="/MySubDirectory"><system.web><!-- Configuration settings for MySubDirectory go here. --></system.web></location></configuration>

Would you really need it ? My preferred method is have it separated into individual config file.

But is has an interesting attribute allowOverride="false".

<location allowOverride="false" >

This would prevent overrides in the config files at lower level and this is useful for system administrators when they would like to retain certain policies throughout and not allow it to be overridden by some application.

Wednesday, December 28, 2005

Validating in Groups

In ASP.NET 2.0 has a new feature called Validation Groups.

If supposing you have two different sections and each of the section needs to be validated separately then Validation Groups is an interesting concept.
Each control can be tied to a specific group by using the ValidationGroup property in the controls.

Here is a simple example in ASP.NET 2.0.
Copy it to an aspx page and run it.

<form id="form1" runat="server"><div><asp:Panel ID="Panel1" runat="server"><asp:TextBox ID="TextBox1" ValidationGroup="FirstGroup" runat="server" /><asp:RequiredFieldValidator ID="RequiredFieldValidator1"ErrorMessage="First Group Validated" ValidationGroup="FirstGroup"runat="server" ControlToValidate="TextBox1" /><asp:Button ID="Button1" Text="Validate Group1"ValidationGroup="FirstGroup" runat="server" /></asp:Panel><br /><asp:Panel Height="94px" ID="Panel2" runat="server" Width="125px"><asp:TextBox ID="TextBox2" ValidationGroup="SecondGroup"runat="server" /><asp:RequiredFieldValidator ID="RequiredFieldValidator2"ErrorMessage="Second Group Validated" ValidationGroup="SecondGroup"ControlToValidate="TextBox2" runat="server" /><asp:Button ID="Button2" Text="Validate Second Group"ValidationGroup="SecondGroup" runat="server" /></asp:Panel></div></form>


Wednesday, December 07, 2005

Exception -al issues in .NET

Quite often I have seen "Object reference not set" error in .net applications. The error is not a developer intended error its just the basic understanding of exceptions not being clear. Whenever an exception is thrown deep in the code the developer intends to highlight this exception and hence re-throws the exception. In .net when you re-throw an exception it creates a new exception on the stack and hence this is the "Object reference not set" a very misleading error.
Therefore instead of doing this

throw new exception("Message")

do only this

throw

Hence the right exception info is obtained when the exception is handled at the interface level. Most large projects should develop their own exception handling mechanism and not depend on this.