0
if (txt_SendAt.Text != null)
{
    //string SendAt_date = txt_Respondby.Text;
    DateTime Send_date = Convert.ToDateTime(txt_SendAt.Text);

    param[15] = new MySqlParameter("@SendDate", MySqlDbType.DateTime);
    param[15].Value = Send_date;
    command.Parameters.AddWithValue("@SendDate", Send_date);     

}
else
{
    param[15] = new MySqlParameter("@SendDate", MySqlDbType.DateTime);
    param[15].Value = now;
    command.Parameters.AddWithValue("@SendDate", now);

}

I have a text box where i select date and time from a calender. When I leave the text box empty it should execute the statements in else condition but it executes the statement in if. Where am I going wrong

I am getting this error if i leave it blank String was not recognized as a valid DateTime.

2
  • is txt_SendAt a text box or the text in the text box? Commented Sep 13, 2011 at 14:44
  • My guess is that now is a DateTime variable (probably set to DateTime.Now). Is that correct or is it a string? If a string, what's it's value? Commented Sep 13, 2011 at 14:59

5 Answers 5

5

You're testing if the textbox itself is null, not it's text:

if (string.IsNullOrEmpty(txt_SendAt.Text) == false)
Sign up to request clarification or add additional context in comments.

1 Comment

This should be "!string.IsNullOrEmpty(txt_SendAt.Text)" if it used in the "if" condition in the OP's code.
4

Use String.IsNullOrWhitespace(txt_SendAt.Text) instead of checking for null.

You're checking if the control is null. It won't be null; you need to check if the Text property of the text box has a value. So if you use IsNullOrEmpty or IsNullOrWhitespace, you'll do a dual check -- if the control's property is null (unlikely) or if it's just blank space (or spaces).

Comments

2

Try:

if (!string.IsNullOrEmpty(txt_SendAt.Text))

1 Comment

You have to remove the condition then :)
1

txt_SendAt refers to the textbox control. You want txt_SendAt.Text

Comments

0

You should try change the if to

if ((txt_SendAt.Text != null)||(txt_SendAt.Text.Trim() != ""))

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.