[英]c# if else statement
I have a problem here because my coding is not working(error) and I don't know how to correct it.Can you guys check if this statement right or wrong? 我在这里遇到问题,因为我的编码不起作用(错误)并且我不知道如何纠正它。你们能检查一下这句话是对还是错? My conditions is 1)if textbox productname is null or empty and dropdownlist1 not selected, text will null. 我的条件是1)如果文本框productname为null或为空,并且未选择dropdownlist1,则文本为null。 2)if textbox productname is filled(string) then text will filled in 3)if if textbox productname is null or empty and dropdownlist1 selected, text will select value. 2)如果文本框产品名称已填充(字符串),则文本将被填充3)如果文本框产品名称为null或为空,并且选择dropdownlist1,则文本将选择值。 Refer bold text.THANKS!! 请参阅粗体字。谢谢!
if (String.IsNullOrEmpty(txtSearchProductname.Text) == true)
{
if (**DropDownList1.SelectedValue.ToString == null**)
{
txtSearchProductname.Text = " ";
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue.ToString();
}
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text.ToString();
}
Two issues: 两个问题:
ToString
, not ToString()
. 您有ToString
,没有ToString()
。 ToString
refers to the function itself; ToString
指向函数本身; you need the parentheses to invoke the method 您需要括号来调用该方法 ToString()
at all, since the value may be null; 您根本不应该调用ToString()
,因为该值可能为null。 this will generate a NullReferenceException
. 这将生成NullReferenceException
。 Just check if DropDownList1.SelectedValue == null
. 只需检查DropDownList1.SelectedValue == null
。 This should be all you need: 这应该是您所需要的:
if (String.IsNullOrEmpty(txtSearchProductname.Text))
{
if (DropDownList1.SelectedValue == null)
{
txtSearchProductname.Text = " ";
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue;
}
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text;
}
The first thing I see is that you have a ToString method without the parenthesis. 我看到的第一件事是您有一个没有括号的ToString方法。 It should look like this: 它看起来应该像这样:
if (DropDownList1.SelectedValue.ToString() == null)
As others have pointed out, the second issue is the comparison to null after converting the item to a string. 正如其他人指出的那样,第二个问题是将项目转换为字符串后,是否为null的比较。 Converting a null to a string will cause an error (the string representation of a null doesn't exist). 将null转换为字符串会导致错误(不存在null的字符串表示形式)。 Instead, as they indicated, you should remove the ToString() entirely and compare the SelectedValue to null like so: 相反,正如他们指出的那样,您应该完全删除ToString(),并将SelectedValue与null进行比较,如下所示:
if (DropDownList1.SelectedValue == null)
.ToString
is a method. .ToString
是一个方法。 You want to check the result of calling that method, so you need to call it (hence, .ToString()
). 您要检查调用该方法的结果,因此需要对其进行调用(因此, .ToString()
)。
You don't need that many ToString
. 您不需要那么多ToString
。 If DropDownList1.SelectedValue
is null, then DropDownList1.SelectedValue.ToString()
will throw an exception. 如果DropDownList1.SelectedValue
为null,则DropDownList1.SelectedValue.ToString()
将引发异常。
if (string.IsNullOrEmpty(txtSearchProductname.Text) == true)
{
if (DropDownList1.SelectedValue == null)
{
txtSearchProductname.Text = " ";
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue;
}
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text;
}
You are using the SelectedValue
of the DropDownList with a ToString()
which is not needed. 您正在使用DropDownList的SelectedValue
和不需要的ToString()
。 See below. 见下文。
if (String.IsNullOrEmpty(txtSearchProductname.Text) == true)
{
if (string.IsNullOrEmpty(DropDownList1.SelectedValue))
{
txtSearchProductname.Text = " ";
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue;
}
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text.ToString();
}
HTH 高温超导
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.