Problem in calculating days in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssmeshack
    New Member
    • Jul 2008
    • 38

    Problem in calculating days in C#

    Im having problem with substract the date retriave from database in gridview with today's date.... And calculate it...Is anyone can help me. Im just learning Visual C#.

    here is my code...

    DateTime datenow = DateTime.Now;
    DateTime datethen = DateTime.Parse( e.Row.Cells[3].Text);
    TimeSpan diff = datenow - datethen;
    int days = diff.Days;

    if (days < 1)
    {
    e.Row.Cells[3].BackColor = System.Drawing. Color.White;
    }

    pls...
  • nmsreddi
    Contributor
    • Jul 2006
    • 366

    #2
    Hello

    what is the problem you are getting ,did you get any error?

    are you getting any incorrect out put ?

    Comment

    • ssmeshack
      New Member
      • Jul 2008
      • 38

      #3
      The problem is at

      DateTime datethen = DateTime.Parse( e.Row.Cells[3].Text);

      the error saying that FormatException was unhandled by user.

      Is it i wrote it wrongly?

      Comment

      • cloud255
        Recognized Expert Contributor
        • Jun 2008
        • 427

        #4
        Originally posted by ssmeshack
        DateTime datethen = DateTime.Parse( e.Row.Cells[3].Text);

        the error saying that FormatException was unhandled by user.
        The compiler is telling you that the format of the content in the cell is wrong.
        Make sure that the data in that column conforms to the format of date time.

        Your code looks ok, the problem is with the data that is in that cell.

        good luck

        Comment

        • ssmeshack
          New Member
          • Jul 2008
          • 38

          #5
          Thank you very much.

          I find that the cell, I defined was wrong....

          it actually like this...

          Code:
          DateTime datethen = DateTime.Parse(e.Row.Cells[1].Text);
          thank you.

          Comment

          Working...