0

i have multiple conditions in my IF statement but only the second one works in other words the null values is not detected. i know the fields are blank or null but it is not detecting it. The email is fired only if keep the 2nd condition and remove the first one but i want to check both conditions before i sent the email out. thanks here is my code

string k = gr.Cells[9].Text;
DateTime strExpectedSubDate = DateTime.Parse(gr.Cells[3].Text);
DateTime strDate = DateTime.Now.Date;

if (k == null && strExpectedSubDate < strDate)
{
    send email();
5
  • I would suspect Text does not return null here.. gravity exists. Check the initial assumptions - preferably with a debugger. Commented Nov 12, 2012 at 22:45
  • 1
    Have you considered that k might be an empty string? =) Commented Nov 12, 2012 at 22:45
  • i think you are right i don't see null when i put it in debug mode but the value i see like this and i don't know what it means: "&nbsp" Commented Nov 13, 2012 at 1:34
  • 1
    &nbsp; is HTML code for a space character (specifically, a "non-breaking space"). The lesson here is never to rely on what you see - use a debugger or some other in-code mechanism to find out what's really there. Commented Nov 13, 2012 at 14:15
  • Also, if you feel you've answered your own question, accept your answer as "the" answer. Commented Nov 13, 2012 at 14:16

5 Answers 5

6

Try if (string.IsNullOrEmpty(k) && strExpectedSubDate < strDate).

Sign up to request clarification or add additional context in comments.

Comments

2

You might want to change to:

if (string.IsNullOrWhiteSpace(k) && strExpectedSubDate < strDate)

Or:

if (string.IsNullOrEmpty(k) && strExpectedSubDate < strDate)

Which equals to:

if ((k == null || k == string.Empty) && strExpectedSubDate < strDate)

Comments

1

Try

if (String.IsNullOrEmpty(k) && strExpectedSubDate < strDate)
{
 // send email

}

Comments

1

What you've got is fine for checking if k is null. If you also want to check if it's empty try:

if (string.IsNullOrEmpty(k) && strExpectedSubDate < strDate)
{
    SendEmail();
}

Comments

1

i think this is how i solved my problem

if (K == ("&nbsp;") && strExpectedSubDate < strDate)
and it worked fine.  thanks

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.