none
String to DateTime Conversion issue RRS feed

  • Question

  •  

    Hello,
    I have a strange problem that is driving me nuts here, hopefully someone can help.
    I am working on an application that pulls data from an xml file and loads it into a dataset.  I then want to use the data in the dataset for some other things.  The problem I am having is converting from a string in data format into datetime.
    If I run the following code, my dates print out correctly -

     

    foreach (DataRow dRow in ds.Tables["task"].Rows)

    {

    Console.WriteLine(dRow["time"].ToString());

    }

     

    If I then add in the conversion, I get a "String was not recognized as a valid DateTime -

     

    foreach (DataRow dRow in ds.Tables["task"].Rows)

    {

    Console.WriteLine(dRow["time"].ToString());

     

    DateTime myTime = Convert.ToDateTime(dRow["time"].ToString());

    Console.WriteLine(myTime);

    }

     

    The strange part with this is that I would expect at least the first time to display since that line comes before the attempted conversion, but nothing displays at all in the console window and then it errors out.  It appears that a carriage return took place, because the cursor is on the second line of the console, but there is no data on the first line.

     

    I have tried using DateTime.Parse and DateTime.ParseExact, and they also give me the error.  I also tried changing the date in the xml file to different formats, but I get the same error for everything.  It almost seems like when I add the Convert line, the data is coming across as null, but without that line, I am getting the correct data.

     

    Hope that makes sense.

     

    Does anyone have any idea what might be happening?

    Thanks for any thoughts.

    Thursday, May 8, 2008 9:00 PM

Answers

  • Maybe it is too obvious, but could it be that when the XML file is read into a dataset, the framework will create DateTime objects instead of strings.

    What would really help is:

    1. A small piece of the XML file
    2. Any schema file for the XML file?
    3. The output of the following code:

    foreach (DataRow dRow in ds.Tables["task"].Rows)

    {

       object data = dRow["time"];


       if (data == null)

          Console.WriteLine("NULL");

       else

          Console.WriteLine("[" + data.GetType() + "] : " + data);

    }

     


    Friday, May 9, 2008 2:25 PM
  •  

    i think you really need to provide the XML data ....

     

    maybe .toString() is not working as expected

    Friday, May 9, 2008 2:32 PM

All replies

  • try the static DateTime function Parse

     

    static void Main(string[] args)

    {

    string s = "2008 May 9";

    Console.WriteLine(s);

    DateTime dt = new DateTime();

    dt = DateTime.Parse(s);

    Console.WriteLine(dt);

    Console.ReadKey();

    }

    Thursday, May 8, 2008 10:08 PM
  • Please post some sample data so that we can see what kind of data you are trying to convert.

     

    Thursday, May 8, 2008 10:40 PM
  • If i'm not mistaken, by default the following format is used in Xml for datetime:

    Code Snippet

    string xmlDateTimeFormat = "yyyy-MM-ddTHH:mm:ss%K";
    DateTime dt = DateTime.ParseExact(dRow["time"], xmlDateTimeFormat, CultureInfo.InvariantCulture);

    Friday, May 9, 2008 6:37 AM
  • If the data is from xml, I would use:

     

    Code Snippet
    DateTime when = XmlConvert.ToDateTime(...);

     

     

    That way, the appropriate format (and culture) will be used, without having to pepper your code with these subtleties... this class also offers conversions for most other common data-types.

     

    Friday, May 9, 2008 7:46 AM
  • I tried the code offered in this reply to no avail.

    I run the code as is and it runs perfect.  I then change it to string s = "2008 05 09 10:09"; and it still runs perfect.  When I add in the line to read the xml file - string s = @dRow["time"].ToString(); - and it throws the same error.  Again, it also seems that when the conversion lines are added, the Console.WriteLine(s); returns nothing (which presumably should not be the case - since that line comes right after the xml file read, it should at least write the first value before it hits the conversion line).  If I run the code to import the xml data and I don't include the conversion lines, it returns the correct data perfectly.

    Friday, May 9, 2008 2:12 PM
  • Sorry - I run the code offered by Jakesee.

     

    Friday, May 9, 2008 2:12 PM
  • Also, the data is stored in the xml file as a string.

     

    Friday, May 9, 2008 2:14 PM
  • Maybe it is too obvious, but could it be that when the XML file is read into a dataset, the framework will create DateTime objects instead of strings.

    What would really help is:

    1. A small piece of the XML file
    2. Any schema file for the XML file?
    3. The output of the following code:

    foreach (DataRow dRow in ds.Tables["task"].Rows)

    {

       object data = dRow["time"];


       if (data == null)

          Console.WriteLine("NULL");

       else

          Console.WriteLine("[" + data.GetType() + "] : " + data);

    }

     


    Friday, May 9, 2008 2:25 PM
  •  

    i think you really need to provide the XML data ....

     

    maybe .toString() is not working as expected

    Friday, May 9, 2008 2:32 PM
  • I found the problem......it turned out to be a problem with my xml schema.  Philippe's post helped me find the error.  Thanks for everyone help with this issue.

     

    Friday, May 9, 2008 2:34 PM
  •  carl zuroski wrote:
    I found the problem......it turned out to be a problem with my xml schema.  Philippe's post helped me find the error.  Thanks for everyone help with this issue.

     

    I think you marked the wrong post as the answer...

     

    Can you explain what the problem was? It could help others with similar issues.

    Friday, May 9, 2008 2:55 PM
  • no, it was actually an error on my part, not a technical error with either c# or xml.  I was interpreting part of the file incorrectly. 

     

    Friday, May 9, 2008 5:19 PM