I use ADO disconnected mode to get data from database by filling dataset ds. All data come true except the date field
string strDate = ds.Tables[0].Rows[0]["H_DT"].ToString();
it throws an exception says:
Specified time is not supported in this calendar. It should be between 04/30/1900 00:00:00 (Gregorian date) and 11/16/2077 23:59:59 (Gregorian date), inclusive.
I tried to write this code
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-sa");
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-sa");
to change the culture to Arabic but without any luck.
Update
The following is screenshot of quick watch for the variable
From
DateTime.ToString
methodYour
ar-sa
culture's default calender isUmAlQuraCalendar
calender.And from
UmAlQuraCalendar.MinSupportedDateTime
PropertySince your
DateTime
is1, 1, 1398
, it is too normal to throwsArgumentOutOfRangeException
.You can solve your problem to provide parameter an
IFormatProvider
in yourDateTime.ToString()
method which hasGregorianCalendar
by default. You can useInvariantCulture
for example.A
DateTime
belongs on Gregorian calendar by default. FromDateTime
structure;That means your
ds.Tables[0].Rows[0]["H_DT"]
datetime is Gregorian calender by default. But since you using.ToString()
method without any parameter, your method uses yourCurrentCulture
which isar-sa
since you wrote it in your web.config. And that culture hasUmAlQuraCalendar
calender by default. Since your datetime out of range in this calender, your code throws exception.Remember, you have a
DateTime
with1318
as a year in Gregorian calender, not1318
as a year inUmAlQuraCalendar
calender.As an example;
throws
ArgumentOutOfRangeException
exception because it is exactly the same case of yours. This is a DateTime which is a1318
year in a Gregorian calender, but there is no representation onUmAlQuraCalendar
calender of this datetime because inUmAlQuraCalendar
calender, years start with1900
in Gregorian calender.Take a look at how
UmAlQuraCalendar
calender implemented;