开发者

Problem with condition statement in c#

开发者 https://www.devze.com 2023-04-04 08:53 出处:网络
if (txt_SendAt.Text != null) { //string SendAt_date = txt_Respondby.Text; DateTime Send_date = Convert.ToDateTime(txt_SendAt.Text);
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);   开发者_JS百科  

}
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.


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

if (string.IsNullOrEmpty(txt_SendAt.Text) == false)


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).


Try:

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


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


You should try change the if to

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

精彩评论

暂无评论...
验证码 换一张
取 消