Introduction
We all know that to retrieve values from a database we have to pass on a "Select" query to it (in case you didn't, then this is not the place where you should be).
Though it works perfectly in the localhost for all datatypes, if uploaded to the server, a DateTime
field may sometimes throw a
FormatException
while parsing it to a .NET DateTime
variable. The reason for this is that
the database may not return the date in compliant to the server. Therefore, we have to set the format through coding.
Background
You need to have a clear understanding of ASP.NET and SQL Server.
Using the Code
Here we will use the class CultureInfo
defined in the namespace System.Globalization
.
Now in the Page_Load
event, write the following:
System.Globalization.CultureInfo vCulture =
(System.Globalization.CultureInfo)System.Globalization.CultureInfo.CreateSpecificCulture("en-US").Clone();
vCulture.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";
System.Threading.Thread.CurrentThread.CurrentUICulture = vCulture;
While retrieving data from a DataReader
(or you can also use a DataAdapter
), first convert the
DateTime
value in the format defined in the Page_Load
event, then cast it into a .NET DateTime
variable.
string temp = String.Format("{0:d}", dr["Datefixed"]);
BDPlite1.SelectedDate = Convert.ToDateTime(temp);
The format of the small 'd' has been specified above using the vCulture
object because d
represents ShortDatePattern
.
For details, see the table below..
Points of Interest
This is indeed a very tricky situation as this error is caught only when we run the application on the
server. Therefore your application will run smoothly on localhost whereas a FormatException
will be thrown while attempting
to run it on the server.
Following is the list of date formats supported in C#:
Specifier DateTimeFormatInfo property Pattern value(for en-us culture)
---------------------------------------------------------------------------------------------------
t ShortTimePattern h:mm tt
d ShortDatePattern M/d/yyyy
T LongTimePattern h:mm:ss tt
D LongDatePattern dddd, MMMM dd, yyyy
f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt
F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt
g (combination of d and t) M/d/yyyy h:mm tt
G (combination of d and T) M/d/yyyy h:mm:ss tt
m, M MonthDayPattern MMMM dd
y, Y YearMonthPattern MMMM, yyyy
r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*)
s SortableDateTimePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*)
u UniversalSortableDateTimePattern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*)
The sample output is displayed below:
String.Format("{0:t}", dt); String.Format("{0:d}", dt); String.Format("{0:T}", dt); String.Format("{0:D}", dt); String.Format("{0:f}", dt); String.Format("{0:F}", dt); String.Format("{0:g}", dt); String.Format("{0:G}", dt); String.Format("{0:m}", dt); String.Format("{0:y}", dt); String.Format("{0:r}", dt); String.Format("{0:s}", dt); String.Format("{0:u}", dt);
For more info on Standard Date and Time Format Strings, click here.