0

I want to use multiple operators like =, >, && in my if statement. I only want to send the email when this condition is true if (strAuthReqDate == null && strDate < strExpectedSubDate). So i think my "less than operator" is wrong pls let me know what i am doing wrong here. thanks

Here is my code:

foreach (GridViewRow gr in GridView1.Rows)
{
    CheckBox cb = (CheckBox)gr.FindControl("chkItem");
    if (cb.Checked)
    {
        // string strID = gr.Cells[0].Text;
        string strExpectedSubDate = gr.Cells[3].Text;
        string strAuthReqDate = gr.Cells[8].Text;
        string strDate = Convert.ToString(System.DateTime.Now);
        if (strAuthReqDate == null && strDate < strExpectedSubDate)
        {
            send email();
        }
    }
 }
शेखर
  • 17,412
  • 13
  • 61
  • 117
moe
  • 5,149
  • 38
  • 130
  • 197

4 Answers4

2

Try to use this code to compare dates

DateTime strExpectedSubDate = DateTime.ParseExact(gr.Cells[3].Text, dateformat);
DateTime strAuthReqDate = DateTime.ParseExact(gr.Cells[8].Text, dateformat);
DateTime strDate = System.DateTime.Now;
if (strAuthReqDate == null && strDate < strExpectedSubDate)
{
}
gabba
  • 2,815
  • 2
  • 27
  • 48
  • i am getting an error and i think need to get rid off time portion in my date. It is showing both the date and the time in the field so how can i only show the Date because the strExpectedSubDate shows only the Date. thanks – moe Nov 12 '12 at 03:03
  • To help You show only short date in grid I need to see the code where you add records to grid. In general you need set dateformat somewhere. – gabba Nov 12 '12 at 11:07
  • Also take a look at answer in http://stackoverflow.com/questions/7580809/parse-c-sharp-string-to-datetime as emartel advised you. – gabba Nov 12 '12 at 11:08
2

First, Cells[i].Text is never null, so you should use String.IsNullOrWhiteSpce or even better, try to cast it to a DateTime instead.

You cannot compare strings and expect that they are treated like DateTimes, C# is not VB6. So convert them to Datetimes first:

DateTime ExpectedSubDate;
string strExpectedSubDate = gr.Cells[3].Text;
if(DateTime.TryParse(strExpectedSubDate, out ExpectedSubDate))
{
    DateTime AuthReqDate;
    string strAuthReqDate = gr.Cells[8].Text;
    if(!DateTime.TryParse(strAuthReqDate, out AuthReqDate))
    {
        if(DateTime.Now < ExpectedSubDate)
        {
            SendMail();
        }
    }
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Your code compares textual dates, not actual dates. You need to compare DateTime objects for it to work like you expect!

emartel
  • 7,712
  • 1
  • 30
  • 58
  • so do i need to change it like this: DateTime strExpectedSubDate = gr.Cells[3].Text; DateTime strAuthReqDate = gr.Cells[8].Text; – moe Nov 11 '12 at 22:42
  • You will need to convert your text, see this solution: http://stackoverflow.com/questions/7580809/parse-c-sharp-string-to-datetime – emartel Nov 11 '12 at 22:44
1

It's because strDate and strExpectedSubDate are strings. So you can't compare them using less than operator

WannaCSharp
  • 1,898
  • 2
  • 13
  • 19