C# 为什么我的程序没有';如果其他操作正确,则无法执行?

C# 为什么我的程序没有';如果其他操作正确,则无法执行?,c#,asp.net,C#,Asp.net,我在三种不同的条件下进行测试; 第一个是下拉列表的selectedIndex 1为2,文本框为空 第二个是下拉列表的selectedIndex 1是2,文本框不是空的 最后一个是选择的下拉列表索引是3 执行时,它直接进入最后一个条件,甚至不测试第一个条件 protected void btnValider_Click(object sender, EventArgs e) { if (DropDown1.SelectedIndex == 2) { if (txt

我在三种不同的条件下进行测试; 第一个是下拉列表的selectedIndex 1为2,文本框为空 第二个是下拉列表的selectedIndex 1是2,文本框不是空的 最后一个是选择的下拉列表索引是3 执行时,它直接进入最后一个条件,甚至不测试第一个条件

protected void btnValider_Click(object sender, EventArgs e)
{
    if (DropDown1.SelectedIndex == 2)
    {

        if (txtNvSt.Text != null)
        {

            con.charger("update Reparation set dateReception='" + txtDateReception.Text + "', nNouvST='" + txtNvSt.Text + "', suivieMateriel='" + txtSuivi.Text + "', statut='" + cmbStatut.SelectedValue + "' where serviceTag ='" + txtServiceTag.Text + "'", false);
            con.charger("update Materiel set reparation= NULL where serviceTag='" + txtServiceTag.Text + "'", false);
            Session["ST"] = txtNvSt.Text;
            Response.Redirect("NouveauMAt.aspx");

        }
        else
            if (txtNvSt.Text == null)
            {
                MessageBox.Show("txtNv null");
                con.charger("update Reparation set dateReception='" + txtDateReception.Text + "',suivieMateriel='" + txtSuivi.Text + "',statut='" + DropDown1.SelectedValue + "'  where serviceTag ='" + txtServiceTag.Text + "'", false);
                con.charger("update Materiel set reparation = NULL where serviceTag='" + txtServiceTag.Text + "'", false);
                con.charger("insert into Stocker values('1', '" + txtServiceTag.Text + "')", false);

            }
    }
    else
    {

        con.charger("update Materiel set reparation = NULL, idEmplacement = NULL where serviceTag='" + txtServiceTag.Text + "'", false);
        con.charger("insert into Stocker values('4', '" + txtServiceTag.Text + "')", false);
        con.charger("update Reparation set dateReception='" + txtDateReception.Text + "',suivieMateriel='" + txtSuivi.Text + "',statut='" + cmbStatut.SelectedValue + "' where serviceTag='" + txtServiceTag.Text + "'", false);
        Response.Redirect("StockHS.aspx");
    }


}

ASP文本框的
Text
属性从不为空

默认值为空字符串,因此如果要检查文本框是否为空,请将条件更改为以下内容:

if (txtNvSt.Text != string.Empty)
或者,您可以使用which帐户来表示空格,根据您的情况,空格可能仍然被认为是“空的”

你的意思是这直接指向
其他
(我想)。你说条件如下:

最后一个是选择的下拉列表索引是3

但是,您的
else
实际上并没有检查任何内容。如果第一种情况失败,即检查
DropDown1.SelectedIndex==2
,则无论
SelectedIndex
的值是多少,您都将始终输入此
else


请记住,
SelectedIndex
零基的,因此列表中的第二项是索引
1
,依此类推。

调试代码并查看在条件中检查的每个属性的值。如果您希望回答,请确保提供不需要外部信息(如
cmbStatut
)的示例,或为代码中使用的每个属性提供值(作为内联注释)。
cmbStatut.SelectedIndex
不等于
2
。如果您调试,您可以自己查看。在第一个
If
语句上设置一个
断点,然后自己调试。最好使用
string.IsNullOrWhiteSpace
@Jodrell添加,谢谢