DateTime

  • Thread starter Thread starter Philip Poole
  • Start date Start date
P

Philip Poole

Hello everyone,

Last week I had one of my main hardrives fail. I have now reinstalled my
computer and loaded back all of my software and program I was developing.

I have been writing a program which connects to a database that is located
on a different computer, however my program is having lots of difficulties
loading and inserting data to the database.

After some investigation I have found it is do with the datetime format.
When using an OleDbDataReader if the reader[3] is reading upload date in the
database it now throws an exception if I try and reinsert this into another
table due to the char format. I then discovered that the reader is
autmotically converting the type to a c# datetime object if I call
reader[3].ToString().

For example, before my hardrive failure, this worked: -

string datetime = reader[2].ToString().Trim()
//before hardrive failure datetime was in the format: dd/MM/yyyy hh:mm:ss tt
//after hardrive failure datetime is now in the format: dd/MM/yyyy
hh:mm:ss:T

Now I have got to use the below: -
string UploadDate = ( (DateTime) reader[3]).ToString("dd/MM/yyyy hh:mm:ss
tt");

This means that I am left with many lines of code spread out in a very large
program that need to be rewritten. Does anyone know why this is happening
and how I can get it back to the old functionailty so I do not have rewrite
all these lines of code.

Please help

Phil
 
Philip Poole said:
Last week I had one of my main hardrives fail. I have now reinstalled my
computer and loaded back all of my software and program I was developing.

I have been writing a program which connects to a database that is located
on a different computer, however my program is having lots of difficulties
loading and inserting data to the database.

<snip>

Well, this is probably just a regional settings issue - look at your
Windows regional settings to say what country you're in.

However, there's a bigger issue here - you shouldn't *care* what the
format of the DateTime is as a string. You should be using
OleDbParameters and setting the value directly to the DateTime value,
and getting the driver to do any formatting required.

Using parameters rather than putting values directly into SQL gives
better performance, no quoting/formatting problems, and no
vulnerability to a SQL injection attack.
 
The formatting in the first instance is going to be based on our your
system defines the formatting. You can change this globally, or as
you've noticed programmatically. Personally, I would modify the code if
you depend on the formatting, or write the application so that it is
not dependent on a specific format.

Check the Formatting Overview article in the MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide
/html/cpconformattingoverview.asp


--
Xavier Pacheco
Xapware Technologies Inc

manage your projects: www.xapware.com/ActiveFocus.htm
the blog: www.xavierpacheco.com/xlog
the book: www.amazon.com/exec/obidos/ASIN/0672324431//xavierpacheco-20
 
Hello Jon,

Thankyou for your comments and help, but am not sure I know what you mean
when use talk about OleDbParemeters.

Could you expand on this please?

Phil
 
Philip Poole said:
Thankyou for your comments and help, but am not sure I know what you mean
when use talk about OleDbParemeters.

Could you expand on this please?

Have a look at the docs for OleDbCommand.Parameters. Parameters should
be used just about everywhere.

I don't currently have an article about parameters, but I'm sure there
are plenty around - if you look at just about any decent ADO.NET
tutorial, there should be a section on them.
 
Just a quick follow up on the regional settings.

I have tried changing the regional information and it does not seem to make
a different. HOwever, When I installed the computer I did chnage customise
the time display how I like it before installing allof the other programs
including The .NET Framework.

However I am now can to go thorugh and cnahge the code as it does show a big
shortcomming in it.

If there any other ideas why this change has happened please post them.

Thanks for the continuing help

Phil
 
Philip Poole said:
Just a quick follow up on the regional settings.

I have tried changing the regional information and it does not seem to make
a different.

It should. For instance, try this program:

using System;

public class Test
{
static void Main(string[] args)
{
Console.WriteLine (DateTime.Now);
}
}

I live in England, so by default it comes up with:
05/10/2004 15:46:36

Now going into Settings | Regional and Language Options and changing to
English (United States) instead of English (United Kingdom), then
hitting apply, the program then gives:

10/5/2004 3:47:47 PM


That kind of thing really does sound like the most likely culprit...
 
Hello John

I get a different outputs as well with that program, however the ouput from
the OleDbDataReader stays the same. It always gives a format like this: -

"05/10/2004 04:00:13:P"

Do you have any ideas why this would be the case.

Philip

Jon Skeet said:
Philip Poole said:
Just a quick follow up on the regional settings.

I have tried changing the regional information and it does not seem to make
a different.

It should. For instance, try this program:

using System;

public class Test
{
static void Main(string[] args)
{
Console.WriteLine (DateTime.Now);
}
}

I live in England, so by default it comes up with:
05/10/2004 15:46:36

Now going into Settings | Regional and Language Options and changing to
English (United States) instead of English (United Kingdom), then
hitting apply, the program then gives:

10/5/2004 3:47:47 PM


That kind of thing really does sound like the most likely culprit...
 
Hello

Slight update here, have created a program with the following line: -
Console.WriteLine ( System.Globalization.CultureInfo.CurrentUICulture );

And I get en-US returned. WHy??? My regional settings as set from the
Control Panel as United Kingdon, why would .NET assume i want US, is there a
regional setting for .net that I am missing

Philip
 
Philip Poole said:
Slight update here, have created a program with the following line: -
Console.WriteLine ( System.Globalization.CultureInfo.CurrentUICulture );

And I get en-US returned. WHy??? My regional settings as set from the
Control Panel as United Kingdon, why would .NET assume i want US, is there a
regional setting for .net that I am missing

Is this in a web application? If so, the problem could be that your
*browser* is sending the wrong regional information.
 
Hi John

Its a web applica\tion connecting to a web service (which I am coding as
well) and the part I am having trouble with is my Web Service.

Philip
 
Philip Poole said:
Its a web applica\tion connecting to a web service (which I am coding as
well) and the part I am having trouble with is my Web Service.

Hmm... I don't know what that takes the CurrentUICulture from, to be
honest. As I said before, you should be avoiding the issue entirely by
not formatting the DateTime at all, but it would be interesting to work
out what's going on.

What web browser are you using, and what settings have you got in it?
For instance, in Mozilla there's the Preferences/Navigator/Languages
dialog which shows the order of preference. If you've only got
English[en] or you you've got English/United States [en-us] at the top
of the list, that might explain things.
 
Philip,

Did you maybe install a dotNet framework in another language than before or
not the right SP?

Cor
 
Hello everyone,

justa quick update. I went through the computer wiht a fine picked comb and
could not find a reason for the chnage. However all thorugh testing the
program I been copying the code over to another server to compare and the
code was not working.

This program points at another database, the production database, and after
looking at the database entires I found the reason for the ocde not working
was that the database entry was inocrrect causing an exception. The code
was correct and was being consistent acorss the two servers.

I have no updated the code to ensure the correct formated data is input, or
output from the database.

Many thanks for everyones help and input.

Philip

P.S. My problems with CurrentUICulture were a wild goose chase, it always
seems set at en-us but the CurrentCUlture was correctly set as en-uk.
 
Back
Top