Wednesday, January 27, 2010

Asp.net - How to find last error which occurred?


Exception LastError;
String ErrMessage;
LastError = Server.GetLastError();
if (LastError != null)
{
ErrMessage = LastError.Message;
}
else
{
ErrMessage = "No Errors";
}

Response.Write("Last Error = " + ErrMessage);

Monday, January 25, 2010

What is the difference between CONST and READONLY?

A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor.
Therefore readonly fields can have different values depending on the constructor used.For eg.
readonly int a;
public class()
{
a=1;
}
public class(string s)
{
a=5;
}
public class(string s, int i)
{
a=i;
}
A const field is a compile-time constant meaning that it is evaluated at compile time, the readonly field can be used for runtime constants which means value gets evaluated during runtime.

Monday, January 4, 2010

using directive vs using statement :

Create an instance in a using statement to ensure that Dispose is called on the object when the using statement is exited. A using statement can be exited either when the end of the using statement is reached or if foreg an exception is thrown and control leaves the statement block before the end of the statement.

Using directive has two uses:
Create an alias for a namespace.
Permit the use of types in a namespace such that you do not have to qualify the use of a type in that namespace .