Date Time and IFormatProvider

X

XmlAdoNewbie

Hello All,
I am trying to bind a date in the form of "20031218" to a datetime
object. I first try to convert the string using
Convert.ToDateTime("20031218") but i get an invalid date time
exception. I think I should be using the IFormatProvider in one of the
overloaded methods of Convert.ToDateTime but i can't find any
documentation that is easy to understand.. i am a newbie if you will.
Any code examples of how to do this would be great.. this is what i
have so far.

case "System.Windows.Forms.DateTimePicker":
if (ctrl.Name == dt.TableName + dc.ToString())
{
DataRow dr = ds.Tables[dt.TableName].Rows[0];
if (dr[dc].Equals(""))
{
dr[dc] = DateTime.Today;
}
else
{
dr[dc] = Convert.ToDateTime(dr[dc]); //this is where the problem
lies
}
ctrl.DataBindings.Add(new Binding("Value",ds.Table[
dt.TableName],dc.ColumnName));
}
break;

Thanks in advance!!
 
J

Jon Skeet [C# MVP]

XmlAdoNewbie said:
I am trying to bind a date in the form of "20031218" to a datetime
object. I first try to convert the string using
Convert.ToDateTime("20031218") but i get an invalid date time
exception. I think I should be using the IFormatProvider in one of the
overloaded methods of Convert.ToDateTime but i can't find any
documentation that is easy to understand.. i am a newbie if you will.
Any code examples of how to do this would be great.. this is what i
have so far.

I suggest you use

DateTime.ParseExact (value, "yyyyMMdd", CultureInfo.InvariantCulture);
 

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