Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中的条件语句问题_C#_Asp.net - Fatal编程技术网

C# c语言中的条件语句问题

C# c语言中的条件语句问题,c#,asp.net,C#,Asp.net,我有一个文本框,从日历中选择日期和时间。当我将文本框留空时,它应该在else条件下执行语句,但在if条件下执行语句。我错在哪里 如果我将其保留为空,则会出现此错误。字符串未被识别为有效的日期时间。请尝试: if (txt_SendAt.Text != null) { //string SendAt_date = txt_Respondby.Text; DateTime Send_date = Convert.ToDateTime(txt_SendAt.Text); pa

我有一个文本框,从日历中选择日期和时间。当我将文本框留空时,它应该在else条件下执行语句,但在if条件下执行语句。我错在哪里

如果我将其保留为空,则会出现此错误。字符串未被识别为有效的日期时间。

请尝试:

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

}
使用String.IsNullOrWhitespacetxt_SendAt.Text而不是检查null


您正在检查控件是否为空。它不会是空的;您需要检查文本框的Text属性是否有值。因此,如果您使用IsNullOrEmpty或IsNullOrWhitespace,您将进行双重检查-如果控件的属性不太可能为null,或者它只是一个或多个空格。

您正在测试文本框本身是否为null,而不是文本:

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

txt_SendAt指的是文本框控件。如果需要txt_SendAt.Text

您应该尝试将if更改为

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

然后,您必须删除以下条件:txt_发送到文本框还是文本框中的文本?我猜now是一个DateTime变量,可能设置为DateTime.now。这是正确的还是字符串?如果是字符串,它的值是多少?这应该是!string.isnulloremptytext_SendAt.Text(如果在OP代码的if条件中使用)。
if ((txt_SendAt.Text != null)||(txt_SendAt.Text.Trim() != ""))