Tuesday, October 06, 2009
#
One can easily list all the primary keys of a table by running simple query…
SELECT Col.Column_Name
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type = 'PRIMARY KEY '
AND Col.Table_Name = '<your table name>'
Hope this help... 
Monday, August 17, 2009
#
“Validation Summary” is very handy to use when one is using the ASP.NET validation controls. We can make it more useful by adding sever side errors to it, it is bit trick but not impossible. By doing this one can avoid the overhead of adding label displaying the server side errors on the page.
All validation controls implements the IValidator interface and add themselves to the Page.Validators collection at page creation time. IValidator has three important members: IsValid, ErrorMessage and Validate(). When validation occurs, the Validate() method of each control is called which in turn sets the IsValid and ErrorMessage property. At render time the ValidationSummary cycles through the Validators collection and looks for controls where IsValid is set to false. If that's the case, the value of the ErrorMessage property is rendered to the summary.
Since IValidator is abstract interface so it better to use BaseValidator class whose functionality is same as of IValidator interface.
To add a new error message, you simply have to add an BaseValidator derived class to the Validators collection (with IsValid = false) and let the summary pick it up. The following small helper class accomplishes that:
// adds an error message to a ValidationSummary control
public class ErrorSummary : BaseValidator
{
public static void AddError(string message, Page page)
{
AddError(message, string.Empty, page);
}
public static void AddError(string message, string validationGroup, Page page)
{
ErrorSummary error = new ErrorSummary(message,validationGroup);
page.Validators.Add(error);
}
private ErrorSummary(string message, string validationGroup)
{
ErrorMessage = message;
ValidationGroup = validationGroup;
base.IsValid = false;
}
public new bool IsValid
{
get { return base.IsValid; }
}
public new void Validate()
{
base.IsValid = EvaluateIsValid();
}
protected override bool EvaluateIsValid()
{
return false;
}
}You can use the class in your code like that:
catch (Exception ex)
{
// logging
ErrorSummary.AddError("error message", this);
return;
}
Friday, July 31, 2009
#
When I was newbiee to ASP.Net, the a question click on my mind again and again, that why .Net provide it own web controls when there are HTML controls already exists.
After tossing through various tech site and blogs I finally got the answer and this makes my first entry to geekswithblogs.
Reason:
ASP.NET Web controls are adaptive in nature because of which they are sometimes called adaptive control.
By adaptive we mean their nature of rendering which depends upon the browser requeting .
For example if client browser IE 6.0 is requesting a page from server then it will render in HTML 4.0 compliant markup and say if same page is requested by Netscape browser then will render in HTML 3.5 complaint markup.
Let me explain it in more specific example by taking <asp:Label> contol into picture.
Server code contains the following line in page: <asp:Label ID="lblMessage" runat="server" ForeColor="red"> Label Text </asp:Label>
Now, when IE browser send request for the above label then the control will render in such way: <span id="lblMessage" style="color: red">Label Text</span> [this is HTML 4.0 compatible markup]
And if Netscape browser send the request then it will render in <span id=" lblMessage"><font color="red">Label Text</font></span> manner [this is HTML 3.2 compatible markup].
This makes ASP.Net more compatible with all the browsers but sometimes ASP.NET's default implementation can be a bit frustrating since for modern browsers - such as recent versions of Mozilla, Firefox, Netscape, and Opera - ASP.NET controls render, by default, HTML 3.2-compliant HTML rather than HTML 4.0-compliant HTML.
But we overcome this frustration by configuring your Web application (or the entire Web server) to render HTML 4.0-compliant HTML for these modern, non-Microsoft browsers.
I my next post I will explain on how to configure our site and application to render X.X-compliant markup.