By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,501 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,501 IT Pros & Developers. It's quick & easy.

Error converting dd/MM/yyyy to sql server format

P: n/a
Hi,

I am trying to convert a UK formated date in a textbox to sql server
format. dd/MM/yyyy -> yyyyMMdd.

Here's the code:

IFormatProvider format = new System.Globalization.CultureInfo("en-GB",
true);
DateTime dateUK = DateTime.ParseExact(DOBTxtBx.Text, "dd/MM/yyyy",
format);
string dateSQL = dateUK.ToString("yyyyMMdd");

// Insert data to dataset table row
patientRow["pntDOB"] = dateSQL;

If, for instance DOBTxtBx.Text = 21/09/1909

Stepping through with debugger gives:
dateUK = 9/21/1909 (this does not seem to be correct, it's MM/dd/yyyy
???)
dateSQL = 19090921 (this does seem OK!)

But the program then errors on patientRow["pntDOB"] = dateSQL; saying
'String was not recognised as a valid DateTime'.

Any ideas what the problem is?

Many Thanks

May 18 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

Do not use string , use a DateTime:

IFormatProvider format = new System.Globalization.CultureInfo("en-GB",
true);
DateTime dateUK = DateTime.ParseExact(DOBTxtBx.Text, "dd/MM/yyyy", format);
// Insert data to dataset table row
patientRow["pntDOB"] = dateUK;
Just make sure that pntDOB is of type DateTime
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Assimalyst" <c_******@hotmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hi,

I am trying to convert a UK formated date in a textbox to sql server
format. dd/MM/yyyy -> yyyyMMdd.

Here's the code:

IFormatProvider format = new System.Globalization.CultureInfo("en-GB",
true);
DateTime dateUK = DateTime.ParseExact(DOBTxtBx.Text, "dd/MM/yyyy",
format);
string dateSQL = dateUK.ToString("yyyyMMdd");

// Insert data to dataset table row
patientRow["pntDOB"] = dateSQL;

If, for instance DOBTxtBx.Text = 21/09/1909

Stepping through with debugger gives:
dateUK = 9/21/1909 (this does not seem to be correct, it's MM/dd/yyyy
???)
dateSQL = 19090921 (this does seem OK!)

But the program then errors on patientRow["pntDOB"] = dateSQL; saying
'String was not recognised as a valid DateTime'.

Any ideas what the problem is?

Many Thanks

May 18 '06 #2

P: n/a
> Do not use string , use a DateTime:

IFormatProvider format = new System.Globalization.CultureInfo("en-GB",
true);
DateTime dateUK = DateTime.ParseExact(DOBTxtBx.Text, "dd/MM/yyyy", format);


That assumes you can be sure the user entered the value in the correct format.

If you're using WinForms it's better to use a DateTimePicker.

Otherwise you should require the value be in an ISO 8601 compliant format.
(And use a regular expression to validate it before accepting it?)
May 18 '06 #3

P: n/a
Assimalyst wrote:
Hi,

I am trying to convert a UK formated date in a textbox to sql server
format. dd/MM/yyyy -> yyyyMMdd.

Here's the code:

IFormatProvider format = new System.Globalization.CultureInfo("en-GB",
true);
DateTime dateUK = DateTime.ParseExact(DOBTxtBx.Text, "dd/MM/yyyy",
format);
string dateSQL = dateUK.ToString("yyyyMMdd");

// Insert data to dataset table row
patientRow["pntDOB"] = dateSQL;

If, for instance DOBTxtBx.Text = 21/09/1909

Stepping through with debugger gives:
dateUK = 9/21/1909 (this does not seem to be correct, it's MM/dd/yyyy
???)
dateSQL = 19090921 (this does seem OK!)

But the program then errors on patientRow["pntDOB"] = dateSQL; saying
'String was not recognised as a valid DateTime'.

Any ideas what the problem is?

Many Thanks

Try formatting it as the iso standard which is YYYY-MM-DD and
YYYY-MM-DDTHH:mm:ss.
Or as others have said, use the intrinsic type instead of a conversion.
May 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.