?
Solved

DateTime Culture

Posted on 2007-10-16
5
Medium Priority
?
1,797 Views
Last Modified: 2008-02-26
Hi
I'm getting this error on my webpage:
String was not recognized as a valid DateTime. Its Line 95

If i run it on the server which hosts the site it works ok, if i test it from my home computer i get the error.
Both computer are in the UK on the server the date is displayed as 16/10/2007 on my XP machine date is displayed as 10/16/2007 how can i change this line of code insertSpecialOfferStarts = Convert.ToDateTime(txtOfferStarts.Text);
so that all datetimes are submitted in UK format

 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
Line 94:             {
Line 95:             insertSpecialOfferStarts = Convert.ToDateTime(txtOfferStarts.Text);

Regards
George
0
Comment
Question by:ImInNeedOfHelp
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 20083969
Please try this:

CultureInfo culture = "en-UK";
insertSpecialOfferStarts = Convert.ToDateTime(txtOfferStarts.Text, culture);


I hope this helps!
0
 
LVL 7

Expert Comment

by:bungHoc
ID: 20084198
You can always use

CultureInfo culture = "en-UK";

However there are a couple of things that can still go wrong here:
1. The locale settings for the server may be wrong. When you configure Server for a specific locale, the formatting of dates and times are inherited automatically from those settings. If the locale settings don't match the data being passed to the server -- for instance, if dates are passed as MM/DD/YYYY when they should be DD/MM/YYYY -- then the error will result. -- I believe this is your case.

2. Locale mismatch between the front end and back end. This typically happens if you have the front end (Web) and the back end (server) running on entirely separate machines and the locale settings on the two machines do not match.

My prefered solution (a bit annoying to code though): Select dates from static list or separate input fields for day / month / year.

Hope this helps...
0
 

Author Comment

by:ImInNeedOfHelp
ID: 20084243
Hi
I get this error when trying to use culture

Cannot implicitly convert type 'string' to 'System.Globalization.CultureInfo'
Regards
George
0
 
LVL 12

Accepted Solution

by:
RubenvdLinden earned 2000 total points
ID: 20084274
Sorry, my mistake:

CultureInfo culture = new CultureInfo("en-UK");
0
 

Author Comment

by:ImInNeedOfHelp
ID: 20084686
Hi RubenvdLinden:
Thanks works now, server and website on same machine
Regards
George
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question