C# If/else语句未执行某些节点

C# If/else语句未执行某些节点,c#,if-statement,C#,If Statement,我有一个代码如下 if(txtEditName.Text.Trim() == "" || txtEditAddress.Text.Trim() == "") { lblBError.Enabled = true; lblBError.Visible = true; lblBError.Text = "Please provide the required field."; return; } else { if(txtControl.Text.Trim()

我有一个代码如下

if(txtEditName.Text.Trim() == "" || txtEditAddress.Text.Trim() == "")
{
    lblBError.Enabled = true;
    lblBError.Visible = true;
    lblBError.Text = "Please provide the required field.";
    return;
}
else
{
    if(txtControl.Text.Trim() == "")
    {
        if(DropDownClient.Enabled)
        {
            if(DropDownClient.SelectedItem.Value == "select")
            {
                lblBError.Enabled = true;
                lblBError.Visible = true;
                lblBError.Text = "Please select Client.";
                return;
            }
        }
        else
        {
            if(lblClientName.Text.Trim() != "")
            {
                sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
                    VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + lblClientName.Text + "'))";
            }
            else
            {
                sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
                    VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")";
            }
        }
    }
    else
    {
        sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
            VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + Convert.ToInt32(txtControl.Text.Trim()) + " )";
        //  SqlCommand cmd = new SqlCommand(sql, connection);
    }
}
我遇到的问题是,代码的某些部分没有执行。当我跑步时,它忽略了其他部分

if(lblClientName.Text.Trim() != "")
{
}

else
{
    sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
        VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")";
}

它跳转else部分的sql=并传递sql ass空字符串。我不知道为什么会这样?我检查了所有的东西,看起来都很好。请告诉我代码有什么问题

首先,如评论中所述,使用string.IsNullOrWhitespace或string.IsNullOrEmpty来帮助您:

if (string.IsNullOrWhitespace(txtEditName.Text) || string.IsNullOrWhitespace(txtEditAddress.Text))
{
    lblBError.Enabled = true;
    lblBError.Visible = true;
    lblBError.Text = "Please provide the required field.";
    return;
}
else
{
    if (!string.IsNullOrWhitespace(txtControl.Text))
    {
        if (DropDownClient.Enabled && DropDownClient.SelectedItem.Value == "select")
        {
            lblBError.Enabled = true;
            lblBError.Visible = true;
            lblBError.Text = "Please select Client.";
            return;
        }
        else
        {
            if (!string.IsNullOrWhitespace(lblClientName.Text))
            {
                sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
                        VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + lblClientName.Text + "'))";
            }
            else
            {
                sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
                VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")";
            }
        }
        else
        {
            sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
            VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + Convert.ToInt32(txtControl.Text.Trim()) + " )";
                //  SqlCommand cmd = new SqlCommand(sql, connection);
        }
    }
}
现在,如果sql=。。。如果正在执行行,则必须启用dropdownclient并且其所选项目=select。我看不出还有什么别的可能

编辑:

我试图重构你的代码。请原谅我的错误,我没有太多时间这么做:

private bool EditFieldsAreValid()
{
    if (string.IsNullOrWhiteSpace(txtEditName.Text) || string.IsNullOrWhiteSpace(txtEditAddress.Text))
        return false;

    return true;
}

private string CreateSql(string value)
{
    return @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
         VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + value + "'))";
}

    if (!EditFieldsAreValid())
    {
        lblBError.Enabled = true;
        lblBError.Visible = true;
        lblBError.Text = "Please provide the required field.";
        return;
    }
    if (string.IsNullOrWhiteSpace(txtControl.Text))
    {
        if (DropDownClient.Enabled && DropDownClient.SelectedItem.Value == "select")
        {
            lblBError.Enabled = true;
            lblBError.Visible = true;
            lblBError.Text = "Please select Client.";
        }
        else
        {
            if (string.IsNullOrWhiteSpace(lblClientName.Text))
            {
                sql = CreateSql(lblClientName.Text);
            }
            else
            {
                sql = CreateSql(DropDownClient.SelectedItem.Value);
            }
        }
    }
    else
    {
        sql = CreateSql(txtControl.Text.Trim());
    }

谢谢你们的帮助。蒂格兰先生所指出的非常有帮助。我发现程序忽略的else语句不应该放在我放的地方。它应该是这样的

  if (txtControl.Text.Trim() == "")
        {
            if (DropDownClient.Enabled)
            {
                if (DropDownClient.SelectedItem.Value == "select")
                {
                    lblBError.Enabled = true;
                    lblBError.Visible = true;
                    lblBError.Text = "Please select Client.";

                    return;

                }
                else
                {
                    sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
                    VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")";

                }

            }
            else
            {
                if (lblClientName.Text.Trim() != "")
                {
                    sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
                       VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + lblClientName.Text + "'))";

                }
                else
                {
                    sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
                       VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")";
                }
            }

不过我花了几个小时。谢谢大家。

可能是因为DropDownClient.Enabled始终为真。。当您尝试使用调试器时,他们会告诉您关于lblClientName的什么?@NuruSalihu您可以将调试打印语句添加到if块中。这将告诉我们条件lblClientName.Text.Trim!=是真是假。而且,那里有一些严重的代码重复。我已对代码进行了格式化,使其更易于阅读。使用String.isnullorempltyllblclientname.Text.Trim代替teadhi,只有txtControl.Text的else部分未执行。按顺序,它不会检查lblClientName。。。。