-2

我有一个代码如下...

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

我遇到的问题是,代码的某些部分没有执行。当我运行时,它忽略了 else 部分

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 空字符串。我不确定为什么会这样?我检查了一切,一切似乎都很好。请有人指出代码有什么问题?

4

2 回答 2

0

首先,如评论中所述,使用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());
    }
于 2013-09-30T07:41:07.670 回答
0

谢谢大家帮助。蒂格兰先生所指出的帮助很大。我发现程序忽略的 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 + ")";
                }
            }

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

于 2013-09-30T08:04:49.470 回答