locked
CholestrolProfile RRS feed

  • Question

  • I am trying to display the Cholestrol profile data in a table.
    Here is the code to show how I am doing this. On line 4, a quick watch in the debugger shows cholestrolItem.When as {11/4/2009}, but it does not get translated ToString() to go in String date1. Why? ToString() is an allowed method for cholestrolItem.When

     

    1.List<CholesterolProfile> cholestrolList = GetValues<CholesterolProfile>(CholesterolProfile.TypeId);

     

    2.foreach (CholesterolProfile cholestrolItem in cholestrolList)

    {
    .
    .
    .

    3.TableCell

     

    dateCell = new TableCell();

     

    4.String date1 = cholestrolItem.When.ToString();// quick watch in the debugger shows cholestrolItem.When as {11/4/2009}

     

    5.DateTime cholestrolDate = DateTime.ParseExact(date1, "MM/dd/yyyy", null);

    .
    .
    .
    }

    Error on line 5:
    System.FormatException: String was not recognized as a valid DateTime.

    Same problem coming with Exercise.

     

    List<Exercise> ExerciseList = GetValues<Exercise>(Exercise.TypeId);

     

    foreach (Exercise ExerciseItem in ExerciseList)

    {

    .
    .

     

    String date1 = ExerciseItem.When.ToString().Substring(0,10);

     

    DateTime exerciseDate = new DateTime();

    exerciseDate =

    DateTime.ParseExact(date1, "MM-dd-yyyy", null);

    .
    .
    }
    Tuesday, November 17, 2009 5:22 PM

Answers


  • You are right, cholestrolItem.When is of type HealthServiceDate only.

    Please take a look at the following link which may be useful: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

    I tried with the below code and I did not get any error.

            List<CholesterolProfile> cholestrolList = GetValues<CholesterolProfile>(CholesterolProfile.TypeId);
            foreach (CholesterolProfile cholestrolItem in cholestrolList)
            {
                String date1 = cholestrolItem.When.ToString();// It can be of any format {11/4/2009 or 11/04/2009}
                DateTime cholestrolDate = DateTime.ParseExact(date1, "M/d/yyyy", null);
            }

    If still you are getting the error, what is the new error?
    -Mahesh
    Wednesday, November 18, 2009 8:20 PM

All replies

  • Hello,

    As you are converting the string to datetime with the value "11/4/2009", you are getting an error. If your value is "11/04/2009", you will possibly not get an error.

    The format string is expecting date in "dd" format, it would expect a "04" in place of "4". 

    The work around can be
    In the DateTime.ParseExact you can use "M/d/yyyy" instead of "MM/dd/yyyy"

    Hope this helps...

    -Mahesh
    Tuesday, November 17, 2009 7:42 PM
  • Mahesh, I deleted all records that had single digit month or day to test what you said ("04" in place of "4". ), but it still gives the same error:System.FormatException: String was not recognized as a valid DateTime.

    I guess it is beacuse it is not in the DateTime format, hr, sec, min are missing. When we create a CholestrolProfile record it only takes HealthServiceDate, not HealthServiceDatetime.

    CholesterolProfile

     

    cholestrol = new CholesterolProfile(new HealthServiceDate).

    Pls let me know how I can overcome this.

    Thankyou.

    Wednesday, November 18, 2009 4:50 PM

  • You are right, cholestrolItem.When is of type HealthServiceDate only.

    Please take a look at the following link which may be useful: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

    I tried with the below code and I did not get any error.

            List<CholesterolProfile> cholestrolList = GetValues<CholesterolProfile>(CholesterolProfile.TypeId);
            foreach (CholesterolProfile cholestrolItem in cholestrolList)
            {
                String date1 = cholestrolItem.When.ToString();// It can be of any format {11/4/2009 or 11/04/2009}
                DateTime cholestrolDate = DateTime.ParseExact(date1, "M/d/yyyy", null);
            }

    If still you are getting the error, what is the new error?
    -Mahesh
    Wednesday, November 18, 2009 8:20 PM