Sunday, September 5, 2010

.NET Custom DateTime Format Strings

To have a more flexible DateTime format the DateTime object's ToString function can be used to pass custom strings like in the following snippet:

DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");

The part in between the open and close parentheses can be customized depending on the requirements.


Following is the different characters available for the custom format. This can also be seen on the MSDN website: http://msdn.microsoft.com/en-us/library/8kb3ddd4(VS.71).aspx.

The following table describes the custom format specifiers and the results they produce. The output of these format specifiers is influenced by the current culture and the settings in the Regional Options control panel.

Format specifierDescription
dDisplays the current day of the month, measured as a number between 1 and 31, inclusive. If the day is a single digit only (1-9), then it is displayed as a single digit.Note that if the 'd' format specifier is used alone, without other custom format strings, it is interpreted as the standard short date pattern format specifier. If the 'd' format specifier is passed with other custom format specifiers or the '%' character, it is interpreted as a custom format specifier.
ddDisplays the current day of the month, measured as a number between 1 and 31, inclusive. If the day is a single digit only (1-9), it is formatted with a preceding 0 (01-09).
dddDisplays the abbreviated name of the day for the specified DateTime. If a specific valid format provider (a non-null object that implements IFormatProvider with the expected property) is not supplied, then the AbbreviatedDayNames property of the DateTimeFormatand its current culture associated with the current thread is used. Otherwise, theAbbreviatedDayNames property from the specified format provider is used.
dddd (plus any number of additional "d" characters)Displays the full name of the day for the specified DateTime. If a specific valid format provider (a non-null object that implements IFormatProvider with the expected property) is not supplied, then the DayNames property of the DateTimeFormat and its current culture associated with the current thread is used. Otherwise, the DayNames property from the specified format provider is used.
fDisplays seconds fractions represented in one digit.Note that if the 'f' format specifier is used alone, without other custom format strings, it is interpreted as the full (long date + short time) format specifier. If the 'f' format specifier is passed with other custom format specifiers or the '%' character, it is interpreted as a custom format specifier.
ffDisplays seconds fractions represented in two digits.
fffDisplays seconds fractions represented in three digits.
ffffDisplays seconds fractions represented in four digits.
fffffDisplays seconds fractions represented in five digits.
ffffffDisplays seconds fractions represented in six digits.
fffffffDisplays seconds fractions represented in seven digits.
g or gg (plus any number of additional "g" characters)Displays the era (A.D. for example) for the specified DateTime. If a specific valid format provider (a non-null object that implements IFormatProvider with the expected property) is not supplied, then the era is determined from the calendar associated with theDateTimeFormat and its current culture associated with the current thread.Note that if the 'g' format specifier is used alone, without other custom format strings, it is interpreted as the standard general format specifier. If the 'g' format specifier is passed with other custom format specifiers or the '%' character, it is interpreted as a custom format specifier.
hDisplays the hour for the specified DateTime in the range 1-12. The hour represents whole hours passed since either midnight (displayed as 12) or noon (also displayed as 12). If this format is used alone, then the same hour before or after noon is indistinguishable. If the hour is a single digit (1-9), it is displayed as a single digit. No rounding occurs when displaying the hour. For example, a DateTime of 5:43 returns 5.
hh, hh (plus any number of additional "h" characters)Displays the hour for the specified DateTime in the range 1-12. The hour represents whole hours passed since either midnight (displayed as 12) or noon (also displayed as 12). If this format is used alone, then the same hour before or after noon is indistinguishable. If the hour is a single digit (1-9), it is formatted with a preceding 0 (01-09).
HDisplays the hour for the specified DateTime in the range 0-23. The hour represents whole hours passed since midnight (displayed as 0). If the hour is a single digit (0-9), it is displayed as a single digit.
HH, HH (plus any number of additional "H" characters)Displays the hour for the specified DateTime in the range 0-23. The hour represents whole hours passed since midnight (displayed as 0). If the hour is a single digit (0-9), it is formatted with a preceding 0 (01-09).
mDisplays the minute for the specified DateTime in the range 0-59. The minute represents whole minutes passed since the last hour. If the minute is a single digit (0-9), it is displayed as a single digit.Note that if the 'm' format specifier is used alone, without other custom format strings, it is interpreted as the standard month day pattern format specifier. If the 'm' format specifier is passed with other custom format specifiers or the '%' character, it is interpreted as a custom format specifier.
mm, mm (plus any number of additional "m" characters)Displays the minute for the specified DateTime in the range 0-59. The minute represents whole minutes passed since the last hour. If the minute is a single digit (0-9), it is formatted with a preceding 0 (01-09).
MDisplays the month, measured as a number between 1 and 12, inclusive. If the month is a single digit (1-9), it is displayed as a single digit.Note that if the 'M' format specifier is used alone, without other custom format strings, it is interpreted as the standard month day pattern format specifier. If the 'M' format specifier is passed with other custom format specifiers or the '%' character, it is interpreted as a custom format specifier.
MMDisplays the month, measured as a number between 1 and 12, inclusive. If the month is a single digit (1-9), it is formatted with a preceding 0 (01-09).
MMMDisplays the abbreviated name of the month for the specified DateTime. If a specific valid format provider (a non-null object that implements IFormatProvider with the expected property) is not supplied, the AbbreviatedMonthNames property of the DateTimeFormatand its current culture associated with the current thread is used. Otherwise, theAbbreviatedMonthNames property from the specified format provider is used.
MMMMDisplays the full name of the month for the specified DateTime. If a specific valid format provider (a non-null object that implements IFormatProvider with the expected property) is not supplied, then the MonthNames property of the DateTimeFormat and its current culture associated with the current thread is used. Otherwise, the MonthNames property from the specified format provider is used.
sDisplays the seconds for the specified DateTime in the range 0-59. The second represents whole seconds passed since the last minute. If the second is a single digit (0-9), it is displayed as a single digit only.Note that if the 's' format specifier is used alone, without other custom format strings, it is interpreted as the standard sortable date/time pattern format specifier. If the 's' format specifier is passed with other custom format specifiers or the '%' character, it is interpreted as a custom format specifier.
ss, ss (plus any number of additional "s" characters)Displays the seconds for the specified DateTime in the range 0-59. The second represents whole seconds passed since the last minute. If the second is a single digit (0-9), it is formatted with a preceding 0 (01-09).
tDisplays the first character of the A.M./P.M. designator for the specified DateTime. If a specific valid format provider (a non-null object that implements IFormatProvider with the expected property) is not supplied, then the AMDesignator (or PMDesignator) property of the DateTimeFormat and its current culture associated with the current thread is used. Otherwise, the AMDesignator (or PMDesignator) property from the specifiedIFormatProvider is used. If the total number of whole hours passed for the specifiedDateTime is less than 12, then the AMDesignator is used. Otherwise, the PMDesignator is used.Note that if the 't' format specifier is used alone, without other custom format strings, it is interpreted as the standard long time pattern format specifier. If the 't' format specifier is passed with other custom format specifiers or the '%' character, it is interpreted as a custom format specifier.
tt, tt (plus any number of additional "t" characters)Displays the A.M./P.M. designator for the specified DateTime. If a specific valid format provider (a non-null object that implements IFormatProvider with the expected property) is not supplied, then the AMDesignator (or PMDesignator) property of the DateTimeFormatand its current culture associated with the current thread is used. Otherwise, theAMDesignator (or PMDesignator) property from the specified IFormatProvider is used. If the total number of whole hours passed for the specified DateTime is less than 12, then theAMDesignator is used. Otherwise, the PMDesignator is used.
yDisplays the year for the specified DateTime as a maximum two-digit number. The first two digits of the year are omitted. If the year is a single digit (1-9), it is displayed as a single digit.Note that if the 'y' format specifier is used alone, without other custom format strings, it is interpreted as the standard short date pattern format specifier. If the 'y' format specifier is passed with other custom format specifiers or the '%' character, it is interpreted as a custom format specifier.
yyDisplays the year for the specified DateTime as a maximum two-digit number. The first two digits of the year are omitted. If the year is a single digit (1-9), it is formatted with a preceding 0 (01-09).
yyyyDisplays the year for the specified DateTime, including the century. If the year is less than four digits in length, then preceding zeros are appended as necessary to make the displayed year four digits long.
zDisplays the time zone offset for the system's current time zone in whole hours only. The offset is always displayed with a leading sign (zero is displayed as "+0"), indicating hours ahead of Greenwich mean time (+) or hours behind Greenwich mean time (-). The range of values is –12 to +13. If the offset is a single digit (0-9), it is displayed as a single digit with the appropriate leading sign. The setting for the time zone is specified as +X or –X where X is the offset in hours from GMT. The displayed offset is affected by daylight savings time.
zzDisplays the time zone offset for the system's current time zone in whole hours only. The offset is always displayed with a leading or trailing sign (zero is displayed as "+00"), indicating hours ahead of Greenwich mean time (+) or hours behind Greenwich mean time (-). The range of values is –12 to +13. If the offset is a single digit (0-9), it is formatted with a preceding 0 (01-09) with the appropriate leading sign. The setting for the time zone is specified as +X or –X where X is the offset in hours from GMT. The displayed offset is affected by daylight savings time.
zzz, zzz (plus any number of additional "z" characters)Displays the time zone offset for the system's current time zone in hours and minutes. The offset is always displayed with a leading or trailing sign (zero is displayed as "+00:00"), indicating hours ahead of Greenwich mean time (+) or hours behind Greenwich mean time (-). The range of values is –12:00 to +13:00. If the offset is a single digit (0-9), it is formatted with a preceding 0 (01-09) with the appropriate leading sign. The setting for the time zone is specified as +X or –X where X is the offset in hours from GMT. The displayed offset is affected by daylight savings time.
:Time separator.
/Date separator.
"Quoted string. Displays the literal value of any string between two quotation marks preceded by the escape character (/).
'Quoted string. Displays the literal value of any string between two " ' " characters.
%cWhere c is both a standard format specifier and a custom format specifier, displays the custom format pattern associated with the format specifier.Note that if a format specifier is used alone as a single character, it is interpreted as a standard format specifier. Only format specifiers consisting of two or more characters are interpreted as custom format specifiers. In order to display the custom format for a specifier defined as both a standard and a custom format specifier, precede the specifier with a % symbol.
\cWhere c is any character, the escape character displays the next character as a literal. The escape character cannot be used to create an escape sequence (like "\n" for new line) in this context.
Any other characterOther characters are written directly to the result string as literals.

Tuesday, August 31, 2010

Crystal Reports for Web Paging Problem

Noticed how the paging of Crystal Reports for Visual Studio works, that when the report is more than 2 pages, the navigator gets stuck at page 2?



Here's a simple solution.
If the ReportDocument loading codes are placed in the Page_Load event of the page...


protected void Page_Load(object sender, EventArgs e)
{
    .
    .
    .
    ReportViewer.ReportSource = reportDocument;
}

...create a new Page_Init event and move the codes there.


protected void Page_Init(object sender, EventArgs e)
{
    .
    .
    .
    ReportViewer.ReportSource = reportDocument;
}


Now the paging will work as intended.









-elyk

Thursday, May 13, 2010

IIS 6.0 and Session State Problems

Have you tried publishing a website in a development server without problems only to see them once deployed on a production server? Here's one problem you might encounter. Why does the session expire quickly when you explicitly set the timeout property in the website's web.config?

<sessionState mode="InProc" cookieless="false" timeout="30"/>

Here's a good post that explains why: WHY DO I LOSE ASP SESSION STATE ON IIS6

Now after digesting the post, I'll give a simple solution you may try to solve that problem.

Step 1:

Change the sessionState in the web.config to an out of process like StateServer.

<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424" cookieless="false" timeout="30"/>

We'll use the StateServer mode which is ASP.NET's special session handling method. Don't change the stateConnectionString, that is simply the port that will be used by the ASP.NET State Service.

Step 2:

Now let's configure the ASP.NET State Service.

In the server hosting the website, go to Start > Run > Type services.msc


Select ASP.NET State Service and right click it and select Properties.


Make sure to select Automatic in Startup type, so the service will start automatically everytime the server reboots. Then start the service.


Step 3:


Now try running the website and see if it already follows the timeout period you set in the web.config. If it does and you haven't encountered any problems then all is good, congrats!. But if you encountered this sneaky error:

Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500


Then your problem might be serialization. Because sessions that will be saved in the ASP.NET State Service must be serialized. Two things to make sure is to first serialize all your local classes like this:


[Serializable]
public class SampleClass
{


}

Place a Serializable tag before the declaration of the class.

Second is to make sure you're not using classes that cannot be serialized like Web Controls (Button, Textbox, Table, etc.). Just search
msdn for other non-serializable classes.

If you followed these steps your website should be running okay by now.

Here's some topics you might be interested:
Session-State Modes 

Serialization






-elyk

Saturday, April 17, 2010

ASP.NET Enable Session in Web Service Methods

Ever tried accessing session variables in Web Service methods? It won't work unless you add a little property in the WebMethod header of the functions.

For example, the basic hello world:

[WebMethod(EnableSession=true)]
public void HelloWorld()
{
    ...
}

Just add the statement EnableSession=true and you'll be able to access session variables found normally in aspx pages like the following sample.

string userid = Session["userid"].ToString();

Thursday, April 15, 2010

ASP.NET C# Set Value to Password Textbox in Code Behind

If ever you've tried setting value in code-behind to a textbox with TextMode property set to "Password", you would notice it doesn't display the masked text on the page. This is simply the way it is for security purposes, but there is a workaround so you can show the user there is actually text in the textbox.


Here is the snippet:


txt1.Text = "sample_password";


if (txt1.TextMode == TextBoxMode.Password) 
{
    txt1.Attributes.Add("value", txt1.Text);
}


Just set the same text value to the javascript attribute "value" and you're done.