Friday, January 20, 2012

Exception handling difference between .NET Framework 1.1 and 2.0

What happens if there is an application exception thrown and is not caught by application.

In .NET framework 1.1, it impacts the current application (web or windows)

In .NET Framework 2.0, it goes upto worker process (beyond our current application), and after a couple of uncaught exceptions, the Website will be stopped automatically (Dangerous !!).

Tuesday, March 15, 2011

Serialize a class to XML

public string GetXML()
{
XmlSerializer objXMLSerializer = null;
StringWriter objStrW = null; // String writer to serialize the object to xml
String strXML; // Stores the serialized XML string
try
{
//Initilize the serializer
objXMLSerializer = new XmlSerializer(this.GetType()); // The serializer object
// Serialize the object
objStrW = new StringWriter();
objXMLSerializer.Serialize(objStrW, this);
// Retrieve the XML
strXML = objStrW.ToString().Replace(CommonConstants.CONST_UTF16, CommonConstants.CONST_UTF8);
return strXML;
}
catch (Exception ex)
{
//throw the exception to the calling function
throw ex;
}
finally
{
//Release the objects
objStrW.Close();
objXMLSerializer = null;
}
}

Wednesday, July 28, 2010

Stored Procedures Performance Tuning

Recently I was given to optimize an SQL Query & would like to post my experience on that.


Indexes : Indexes are the only best way to improve the performance of select query.

Even though we have indexes, we might not be using them properly. For example, if the select statement has a where condition, we have to check if all the columns in where clause have an index. 
Even if one column doesn't have an index, that causes lot of performance degradation. We can even have index on more than one column. Even this helps some times... (covered index - when all the columns in select query have an index, its called covered index)


SQL server has an option even to specify which index we can use when one column has more than one index on it, one for the column alone , one could be on a group of columns.


2. In the where clause if there is any aggregation - for ex: if you have convert(varchar,date1,101) in where clause - then the Index on date1 field doesn't work even though there is an index on it.

Never use functions in the where clause - this causes lot of performance degradation.


Friday, March 20, 2009

Regular Expressions















Regular Expressions

CharacterDefinition
^Start of a string.
$End of a string.
.Any character (except \n newline)
|Alternation.
{...}Explicit quantifier notation.
[...]Explicit set of characters to match.
(...)Logical grouping of part of an expression.
*0 or more of previous expression.
+1 or more of previous expression.
?0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string.
\Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below.

















Character Classes http://tinyurl.com/5ck4ll

Char ClassDescription
.Matches any character except \n. If modified by the Singleline option, a period character matches any character. For more information, see Regular Expression Options.
[aeiou]Matches any single character included in the specified set of characters.
[^aeiou]Matches any single character not in the specified set of characters.
[0-9a-fA-F]Use of a hyphen (–) allows specification of contiguous character ranges.
\p{name}Matches any character in the named character class specified by {name}. Supported names are Unicode groups and block ranges. For example, Ll, Nd, Z, IsGreek, IsBoxDrawing.
\P{name}Matches text not included in groups and block ranges specified in {name}.
\wMatches any word character. Equivalent to the Unicode character categories
[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \w is equivalent to [a-zA-Z_0-9].
\WMatches any nonword character. Equivalent to the Unicode categories [^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \W is equivalent to [^a-zA-Z_0-9].
\sMatches any white-space character. Equivalent to the Unicode character categories [\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \s is equivalent to [ \f\n\r\t\v].
\SMatches any non-white-space character. Equivalent to the Unicode character categories [^\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \S is equivalent to [^ \f\n\r\t\v].
\dMatches any decimal digit. Equivalent to \p{Nd} for Unicode and [0-9] for non-Unicode, ECMAScript behavior.
\DMatches any nondigit. Equivalent to \P{Nd} for Unicode and [^0-9] for non-Unicode, ECMAScript behavior.

Monday, February 23, 2009

Custom Validator to validate Date

This is a custom validation that tests if the given date is in 30 days from today. (This validator throws error when the date is less than today or when the date is greater than 30 days from today)

--------------------------------------------------------------------------------------
This post is purely for my personal reference. :-)
-----------------------------------------------------------------------------------------


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function datevalidate(sender, args)
{
var dd = document.getElementById("<%=TextBox1.ClientID%>").value;
var mm = document.getElementById("<%=TextBox2.ClientID%>").value;
var yy = document.getElementById("<%=TextBox3.ClientID%>").value;
//Why can't I take date from Javascript ? (I want server's date as per my requirement)
var ddnow = document.getElementById("<%=hdnDay.ClientID%>").value;
var mmnow = document.getElementById("<%=hdnMonth.ClientID%>").value;
var yynow = document.getElementById("<%=hdnYear.ClientID%>").value;
mm = mm-1; // javascript month range 0 - 11
mmnow = mmnow-1; //Month ranges from 0-11 in Javascript
var tempDate = new Date(yy,mm,dd);
var today = new Date(2009,1,23);
var d = new Date();
d.setFullYear(yynow,mmnow,ddnow+30);
//First check if the given date is valid.
if ( (yy == tempDate.getFullYear()) && (mm == tempDate.getMonth()) && (dd == tempDate.getDate()) )
{
if(tempDate < today)
{
args.IsValid = false;
}
else
{
if(tempDate < d)
args.IsValid = true;
else
args.IsValid = false;
}
}
// if ( (yy == tempDate.getFullYear()) && (mm == tempDate.getMonth()) && (dd == tempDate.getDate()) )
// {
// args.IsValid=true;
// } else
// {
// args.IsValid=false;
// }
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="TextBox1" runat="server"></asp:TextBox>
<asp:textbox id="TextBox2" runat="server"></asp:TextBox>
<asp:textbox id="TextBox3" runat="server"></asp:TextBox>
<asp:customvalidator id="custdate" runat="server" controltovalidate="TextBox3" clientvalidationfunction="datevalidate" errormessage="Invalid date"></asp:CustomValidator>
<br />
<asp:button id="Button1" runat="server" text="Button">
<input type="hidden" id="hdnDay" runat="server">
<input type="hidden" id="hdnMonth" runat="server">
<input type="hidden" id="hdnYear" runat="server">
</div>
</form>
</body>
</html>


-----------------------------------------------------------------
Code-behind
-----------------------------------------------------------------


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
hdnDay.Value = DateTime.Now.ToString("dd");
hdnMonth.Value = DateTime.Now.ToString("MM"); //m is for minutes. M is for month.
hdnYear.Value = DateTime.Now.ToString("yyyy");
}
}

Thursday, November 27, 2008

Printing Date in different formats

DateTime.ToString()
Assume dt is a datetime object.
dt.ToString("MM/dd/yyyy") prints 05/05/2005
In the same way, try out the following options.

0 MM/dd/yyyy 05/05/2005
1 dddd, dd MMMM yyyy Thursday, 05 May 2005
2 dddd, dd MMMM yyyy HH:mm Thursday, 05 May 2005 06:30
3 dddd, dd MMMM yyyy hh:mm tt Thursday, 05 May 2005 06:30 AM
4 dddd, dd MMMM yyyy H:mm Thursday, 05 May 2005 6:30
5 dddd, dd MMMM yyyy h:mm tt Thursday, 05 May 2005 6:30 AM
6 dddd, dd MMMM yyyy HH:mm:ss Thursday, 05 May 2005 06:30:07
7 MM/dd/yyyy HH:mm 05/05/2005 06:30
8 MM/dd/yyyy hh:mm tt 05/05/2005 06:30 AM
9 MM/dd/yyyy H:mm 05/05/2005 6:30
10 MM/dd/yyyy h:mm tt 05/05/2005 6:30 AM
10 MM/dd/yyyy h:mm tt 05/05/2005 6:30 AM
10 MM/dd/yyyy h:mm tt 05/05/2005 6:30 AM
11 MM/dd/yyyy HH:mm:ss 05/05/2005 06:30:07
12 MMMM dd May 05
13 MMMM dd May 05
14 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2005-05-05T06:30:07.7199222-04:00
15 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2005-05-05T06:30:07.7199222-04:00
16 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Thu, 05 May 2005 06:30:07 GMT
17 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Thu, 05 May 2005 06:30:07 GMT
18 yyyy'-'MM'-'dd'T'HH':'mm':'ss 2005-05-05T06:30:07
19 HH:mm 06:30
20 hh:mm tt 06:30 AM
21 H:mm 6:30
22 h:mm tt 6:30 AM
23 HH:mm:ss 06:30:07
24 yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2005-05-05 06:30:07Z
25 dddd, dd MMMM yyyy HH:mm:ss Thursday, 05 May 2005 06:30:07
26 yyyy MMMM 2005 May
27 yyyy MMMM 2005 May

The patterns for DateTime.ToString ( 'd' ) :
0 MM/dd/yyyy 05/05/2005

The patterns for DateTime.ToString ( 'D' ) :
0 dddd, dd MMMM yyyy Thursday, 05 May 2005

The patterns for DateTime.ToString ( 'f' ) :
0 dddd, dd MMMM yyyy HH:mm Thursday, 05 May 2005 06:30
1 dddd, dd MMMM yyyy hh:mm tt Thursday, 05 May 2005 06:30 AM
2 dddd, dd MMMM yyyy H:mm Thursday, 05 May 2005 6:30
3 dddd, dd MMMM yyyy h:mm tt Thursday, 05 May 2005 6:30 AM

The patterns for DateTime.ToString ( 'F' ) :
0 dddd, dd MMMM yyyy HH:mm:ss Thursday, 05 May 2005 06:30:07

The patterns for DateTime.ToString ( 'g' ) :
0 MM/dd/yyyy HH:mm 05/05/2005 06:30
1 MM/dd/yyyy hh:mm tt 05/05/2005 06:30 AM
2 MM/dd/yyyy H:mm 05/05/2005 6:30
3 MM/dd/yyyy h:mm tt 05/05/2005 6:30 AM

The patterns for DateTime.ToString ( 'G' ) :
0 MM/dd/yyyy HH:mm:ss 05/05/2005 06:30:07

The patterns for DateTime.ToString ( 'm' ) :
0 MMMM dd May 05

The patterns for DateTime.ToString ( 'r' ) :
0 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Thu, 05 May 2005 06:30:07 GMT

The patterns for DateTime.ToString ( 's' ) :
0 yyyy'-'MM'-'dd'T'HH':'mm':'ss 2005-05-05T06:30:07

The patterns for DateTime.ToString ( 'u' ) :
0 yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2005-05-05 06:30:07Z

The patterns for DateTime.ToString ( 'U' ) :
0 dddd, dd MMMM yyyy HH:mm:ss Thursday, 05 May 2005 06:30:07

The patterns for DateTime.ToString ( 'y' ) :
0 yyyy MMMM 2005 May

Building a custom DateTime.ToString Patterns
The following details the meaning of each pattern character. Not the K and z character.

d Represents the day of the month as a number from 1 through 31. A single-digit day is formatted without a leading zero
dd Represents the day of the month as a number from 01 through 31. A single-digit day is formatted with a leading zero
ddd Represents the abbreviated name of the day of the week (Mon, Tues, Wed etc)
dddd Represents the full name of the day of the week (Monday, Tuesday etc)
h 12-hour clock hour (e.g. 7)
hh 12-hour clock, with a leading 0 (e.g. 07)
H 24-hour clock hour (e.g. 19)
HH 24-hour clock hour, with a leading 0 (e.g. 19)
m Minutes
mm Minutes with a leading zero
M Month number
MM Month number with leading zero
MMM Abbreviated Month Name (e.g. Dec)
MMMM Full month name (e.g. December)
s Seconds
ss Seconds with leading zero
t Abbreviated AM / PM (e.g. A or P)
tt AM / PM (e.g. AM or PM
y Year, no leading zero (e.g. 2001 would be 1)
yy Year, leadin zero (e.g. 2001 would be 01)
yyy Year, (e.g. 2001 would be 2001)
yyyy Year, (e.g. 2001 would be 2001)
K Represents the time zone information of a date and time value (e.g. +05:00)
z With DateTime values, represents the signed offset of the local operating system's time zone from Coordinated Universal Time (UTC), measured in hours. (e.g. +6)
zz As z but with leadin zero (e.g. +06)
zzz With DateTime values, represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. (e.g. +06:00)
f Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value.
ff Represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value.
fff Represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.
ffff Represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value. While it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
fffff Represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value. While it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
ffffff Represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value. While it is possible to display the millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
fffffff Represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value. While it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
F Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value. Nothing is displayed if the digit is zero.
: Represents the time separator defined in the current DateTimeFormatInfo..::.TimeSeparator property. This separator is used to differentiate hours, minutes, and seconds.
/ Represents the date separator defined in the current DateTimeFormatInfo..::.DateSeparator property. This separator is used to differentiate years, months, and days.
" Represents a quoted string (quotation mark). Displays the literal value of any string between two quotation marks ("). Your application should precede each quotation mark with an escape character (\).
' Represents a quoted string (apostrophe). Displays the literal value of any string between two apostrophe (') characters.
%c Represents the result associated with a c custom format specifier, when the custom date and time format string consists solely of that custom format specifier. That is, to use the d, f, F, h, m, s, t, y, z, H, or M custom format specifier by itself, the application should specify %d, %f, %F, %h, %m, %s, %t, %y, %z, %H, or %M. For more information about using a single format specifier, see Using Single Custom Format Specifiers.

Friday, May 30, 2008

Regular Expression that accepts 0-9,space,/,+,- only

^([0-9][\ ][\/][\+][\-])*$

The below link can be used to test your regular expressions:
http://www.dotnetcoders.com/web/Learning/Regex/RegexTester.aspx