I've been having some issues which seem really really strange to me and I cannot event begin to comprehend why this is happening.
Basically, I'm trying to do a DateTime.ParseExact which if working on one case but not on the other.
Let's say I have this string as the date string:
"11/02/2015 11:59:06:313"
If I parse by giving the method the explicit declaration of the string, i.e. the next code, everything works fine:
DateTime.ParseExact("11/02/2015 11:59:06:313", "dd/MM/yyyy HH:mm:ss:fff", null);
Now, when placing this as a dynamic value (which is what I want) I get the "String not recognized as a valid DateTime format", in this code:
DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:fff", null);
I've also tried the Convert.ToDateTime method (which threw the same exception):
Convert.ToDateTime(item.original).ToString("dd/MM/yyyy HH:mm:ss:fff");
The 'item.original' variable is coming from a Class (it's a string property of that class, which is defined as
public class XmlData
{
public string tableName { get; set; }
public string columnName { get; set; }
public string original { get; set; }
public int required { get; set; }
public string status { get; set; }
public string type { get; set; }
public string newValue { get; set; }
}
I'm really lost here. Does it make sense to anyone why this is happening?
EDIT
Decided to give a little more information on how this is being used because maybe the problem comes from further behind.
I have a class that I'm using to only define properties that, using reflection will create a DataTable.
The class has this property:
public DateTime last_date { get; set; }
Then I'm using this method to build the DataTable:
public DataTable CreateEmptyDataTable(Type myType)
{
DataTable dt = new DataTable();
foreach (PropertyInfo info in myType.GetProperties())
{
dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}
return dt;
}
After initializing the DataTable, I'm reading the values from my XmlData class and using the following code to assing the value to the last_date column like this:
//Set the original state
foreach (XmlData item in collection)
{
if (item.tableName == "vehicle")
{
if (item.original == "--NULL--")
dr[item.columnName.Substring(item.tableName.Length + 1)] = DBNull.Value;
else
{
if (item.type == "DT") //DateTime in format "dd/MM/yyyy HH:mm:ss:fff"
dr[item.columnName.Substring(item.tableName.Length + 1)] = DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:FFF", null);
else
dr[item.columnName.Substring(item.tableName.Length + 1)] = Convert.ChangeType(item.original, dt.Columns[item.columnName.Substring(item.tableName.Length + 1)].DataType);
}
}
}