Wednesday, March 14, 2007

Date formatting

Some of the formats for date formatting.
specifier type output
dd Day 08
ddd Short Day Name Mon
dddd Full Day Name Monday
hh 2 digit hour 12
HH 2 digit hour (24 hour) 12
mm 2 digit minute 30
MM Month 06
MMM Short Month name Jun
MMMM Month name June
ss seconds 59
tt AM/PM PM
yy 2 digit year 70
yyyy 4 digit year 1970
: seperator, e.g. {0:hh:mm:ss} 12:30:59
/ seperator, e.g. {0:dd/MM/yyyy} 08/06/1970

Can simply be used with string as well.
for example,
string date=DateTime.Now.ToString("yyyy");

In some controls there is a DataFormatString property as well:
DataFormatString="{0:g}"

Thursday, March 1, 2007

TransactionScope

While developing applications we often need to handle transactions. Before .NET 2 various options were available like ADO.NET Transaction model, Enterprise Services Transactions etc but the new TransactionScope has really made it easy to manage transactions.
With transactions scope we can do transactions across multiple application layers as well as the distributed ones.
The good news is that System.Transactions has the unique capability of knowing whether to use a distributed or lightweight transaction. It will use a lightweight transaction if there is a single domain accessing a single database, or it will use a distributed transaction similar to the enterprise services transaction model if there are multiple databases to access.

To use this new transaction functionality we need to add the namespace System.Transactions.

To use the TransactionScope we can simply add it in a using block like this


using (TransactionScope ts = new TransactionScope())
{
// code goes here
//and once we are done to complete the transaction
ts.Complete();

}

Different options are available with TransactionScope which could be exploited according to needs.

One thing to be taken into consideration is that if applications or components involved in a transaction are not residing on the same machine we need to do some settings else an exception like the given below could be raised.

{"The transaction has already been implicitly or explicitly committed or aborted."}
System.Transactions.TransactionException: {"The transaction has already been implicitly or explicitly committed or aborted."}

To overcome this problem we need to set up MSDTC in both the systems.

Setup both the systems to allow transactions by following these steps

• Open the Control Panel and select Administrative Tools.
• From Administrative Tools select Component Services.
• Open the Component Services tree to display My Computer.
• Right click My Computer and select properties to display the properties dialog.
• In the dialog select the MSDTC tab and click the Security Configuration button.
• In the Security Configuration dialog, make sure Allow Inbound, Allow Outbound and No Authentication Required are selected.

Considering an application running on Win2003 and communicating with SQL server on a machine with Win2k there are not security setting required for Win2k. In that case “No Authentication Required” should be selected on the application server.

For more details on Transactions: http://msdn2.microsoft.com/en-us/library/w97s6fw4.aspx