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();
        }
    }
 }
3
  • It seems to be fine. What is the problem body? Commented Nov 11, 2012 at 22:37
  • You are doing a string comparison, not a date comparison. What format do you expect the strExpectedSubDate to be in? Commented Nov 11, 2012 at 22:38
  • both fields are date fields. can you show me Mohamed some example? Commented Nov 11, 2012 at 22:44

4 Answers 4

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)
{
}
Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
Also take a look at answer in stackoverflow.com/questions/7580809/… as emartel advised you.
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();
        }
    }
}

Comments

1

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

2 Comments

so do i need to change it like this: DateTime strExpectedSubDate = gr.Cells[3].Text; DateTime strAuthReqDate = gr.Cells[8].Text;
You will need to convert your text, see this solution: stackoverflow.com/questions/7580809/…
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.