-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 に答える