datetime column problem

S

SSP

Hi all,

I am trying to insert a date into the datetime column of a SQL CE database
and it behaves in a very strange way.

Whenever I try to insert the date such as "4/3/1975", the date that gets
entered in the column looks like the following when view using the query
analyser:

2004-03-04 00:00:00

Here are is code that I am using:
....
cmd.Parameters["@cDOB"].Value =
Convert.ToDateTime(cDOB_tb.Text).ToShortDateString();
....

where cDOB_tb.Text is 4/3/1975

Any ideas?

SSP
 
M

Mark Johnson

This depends on which CultureInfo you may be using at the time since this is
not a unique date.

//-- Support for MonthDayPattern needed in InitMonthContextMenu()
// - this is Maschine specific
System.Globalization.CultureInfo uici =
(CultureInfo)System.Globalization.CultureInfo.CurrentUICulture.Clone();
if (uici.DateTimeFormat.MonthDayPattern == "MMMM dd") // US-English and
Swahili(Kenia)
ip_MonthDayPattern = 0; // Month Day Year
if (uici.DateTimeFormat.MonthDayPattern == "dd MMMM") // Most of the
World
ip_MonthDayPattern = 1; // Day Month Year
if ((uici.DateTimeFormat.MonthDayPattern == "d MMMM") || // Polish
(uici.DateTimeFormat.MonthDayPattern == "MMMM d.") || // Hungray
(uici.DateTimeFormat.MonthDayPattern == "d. MMMM") || //
Lettland
(uici.DateTimeFormat.MonthDayPattern == "'den 'd MMMM")) // Sweden
ip_MonthDayPattern = 2; // Year Month Day
//-- Support for Canada-English : MonthDayPattern = "MMMM dd" but dates
are written dd.mm.yyy
if (uici.Name == "en-CA") // Canada-French needs no correction
ip_MonthDayPattern = 1; // Day Month Year
//--

If you are expecting a MM/DD/YYYY result your MonthDayPattern should be
"MMMM dd".
It looks as if yours is at the moment is "dd MMMM" since the output is
DD/MM/YYYY.

if you are doing something like this :

dset_Row[sa_MainFrame00Rows[14]] = Convert.ToDateTime(textBox00131.Text);

the Converting will be done as set in CultureInfo at the time of the
convert.

See
http://www.mj10777.de/NETFramework/Desktop/DateTimePicker/EnglishFrenchGerman.htm

to get an idea of the formats expected at convert accourding to the Culure
set.

Since "4/3/1975" is a valid date in en-us (April, 3, 1975 - 2004-04-03
00:00:00) as well as en-can (4 March 1975 - 2004-03-04 00:00:00) you cannot
use a try for this.

Using a DateTimePicker would avoid this problem.

http://www.mj10777.de/NETFramework/Compact/DateTimePicker/index.htm

Hope this helps.

Mark Johnson, Berlin Germany
(e-mail address removed)
 
S

SSP

Thanks Mark,

I haven't tried it as yet, but I sure can see where your sugges. will lead.

I'll let you know.

SSP

Mark Johnson said:
This depends on which CultureInfo you may be using at the time since this is
not a unique date.

//-- Support for MonthDayPattern needed in InitMonthContextMenu()
// - this is Maschine specific
System.Globalization.CultureInfo uici =
(CultureInfo)System.Globalization.CultureInfo.CurrentUICulture.Clone();
if (uici.DateTimeFormat.MonthDayPattern == "MMMM dd") // US-English and
Swahili(Kenia)
ip_MonthDayPattern = 0; // Month Day Year
if (uici.DateTimeFormat.MonthDayPattern == "dd MMMM") // Most of the
World
ip_MonthDayPattern = 1; // Day Month Year
if ((uici.DateTimeFormat.MonthDayPattern == "d MMMM") || // Polish
(uici.DateTimeFormat.MonthDayPattern == "MMMM d.") || // Hungray
(uici.DateTimeFormat.MonthDayPattern == "d. MMMM") || //
Lettland
(uici.DateTimeFormat.MonthDayPattern == "'den 'd MMMM")) // Sweden
ip_MonthDayPattern = 2; // Year Month Day
//-- Support for Canada-English : MonthDayPattern = "MMMM dd" but dates
are written dd.mm.yyy
if (uici.Name == "en-CA") // Canada-French needs no correction
ip_MonthDayPattern = 1; // Day Month Year
//--

If you are expecting a MM/DD/YYYY result your MonthDayPattern should be
"MMMM dd".
It looks as if yours is at the moment is "dd MMMM" since the output is
DD/MM/YYYY.

if you are doing something like this :

dset_Row[sa_MainFrame00Rows[14]] = Convert.ToDateTime(textBox00131.Text);

the Converting will be done as set in CultureInfo at the time of the
convert.

See
http://www.mj10777.de/NETFramework/Desktop/DateTimePicker/EnglishFrenchGerman.htm

to get an idea of the formats expected at convert accourding to the Culure
set.

Since "4/3/1975" is a valid date in en-us (April, 3, 1975 - 2004-04-03
00:00:00) as well as en-can (4 March 1975 - 2004-03-04 00:00:00) you cannot
use a try for this.

Using a DateTimePicker would avoid this problem.

http://www.mj10777.de/NETFramework/Compact/DateTimePicker/index.htm

Hope this helps.

Mark Johnson, Berlin Germany
(e-mail address removed)





SSP said:
Hi all,

I am trying to insert a date into the datetime column of a SQL CE database
and it behaves in a very strange way.

Whenever I try to insert the date such as "4/3/1975", the date that gets
entered in the column looks like the following when view using the query
analyser:

2004-03-04 00:00:00

Here are is code that I am using:
...
cmd.Parameters["@cDOB"].Value =
Convert.ToDateTime(cDOB_tb.Text).ToShortDateString();
...

where cDOB_tb.Text is 4/3/1975

Any ideas?

SSP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top