为什么我的方法互相干扰以及C#中的if语句?

发布于 2025-01-24 17:59:15 字数 3482 浏览 3 评论 0原文

我有一个表格可以在某些情况下插入SQL数据库中的某些数据,首先我需要检查nulls并提醒用户:

void CheckNulls() {

    try {
        if (txtcode.Text.Length == 0 || txtItem.Text.Length == 0 || txtWh.Text.Length == 0) {

            MessageBox.Show("Fill Required Fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        else {
            checkExistAndDo();

        }

    }

    catch(Exception Err) {
        MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

}

然后,我需要检查用户是否检查任何consectbox

void checkExistAndDo() {

    if (chkProduct.Checked || chkMaterial.Checked)

    {

        if (chkProduct.Checked == true) {
            if (!chkMaterial.Checked) {
                da = new SqlDataAdapter("SELECT [ProductCode] FROM Products Where [ProductCode] = @prcode ", Cn);
                da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
                dt.Clear();
                da.Fill(dt);
                if (dt.Rows.Count > 0) {

                    MessageBox.Show("Existing Code", "Error");

                }
                else {

                    TakeAction();
                }
            }
        }

        else {
            da = new SqlDataAdapter("SELECT Code FROM Items Where Code = @prcode ", Cn);
            da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
            dt.Clear();

            da.Fill(dt);
            if (dt.Rows.Count > 0) {
                MessageBox.Show("Existing Code", "Error");

            }
            else {

                TakeAction();
            }

        }

    }

    else {
        MessageBox.Show("Check even one", "check", MessageBoxButtons.OK, MessageBoxIcon.Warning);

    }

}

因此,我有总共4种不同的组合:

chkProduct.Checked : chkMaterial.Checked : Action
--------------------------------------------------------------
              true :                true : InsertSubProduct()
              true :               false : InsertProduct()
             false :                true : InsertMaterial()
             false :               false : Ask User to Check   

然后,我需要根据组合采取行动:

private void TakeAction()
{
    try
    {

        if (chkProduct.Checked == true && chkMaterial.Checked == false)
        {


            InsertProduct();
            MessageBox.Show("Product Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        else if (chkProduct.Checked == false && chkMaterial.Checked == true)
        {

            InsertMaterial();
            MessageBox.Show("Material Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        else
        {
            InsertSubProduct();
            MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }

    catch (Exception Err)
    {
        MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    finally
    {
        this.Close();
    }


}

问题是,该部分没有做任何不显示我的消息的事情,甚至不是错误消息,就好像它在我的代码中不存在:

       else
        {
            InsertSubProduct();
            MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

对不起。对于长期的帖子,但我尽了最大的努力并多次调试了代码,但我无法通过此事,谢谢您,您的帮助非常感谢。

I have a form to insert some data in sql database with some conditions , First I need to check for nulls and alert the user :

void CheckNulls() {

    try {
        if (txtcode.Text.Length == 0 || txtItem.Text.Length == 0 || txtWh.Text.Length == 0) {

            MessageBox.Show("Fill Required Fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        else {
            checkExistAndDo();

        }

    }

    catch(Exception Err) {
        MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

}

Then I need to check if the user checked any checkbox :

void checkExistAndDo() {

    if (chkProduct.Checked || chkMaterial.Checked)

    {

        if (chkProduct.Checked == true) {
            if (!chkMaterial.Checked) {
                da = new SqlDataAdapter("SELECT [ProductCode] FROM Products Where [ProductCode] = @prcode ", Cn);
                da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
                dt.Clear();
                da.Fill(dt);
                if (dt.Rows.Count > 0) {

                    MessageBox.Show("Existing Code", "Error");

                }
                else {

                    TakeAction();
                }
            }
        }

        else {
            da = new SqlDataAdapter("SELECT Code FROM Items Where Code = @prcode ", Cn);
            da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
            dt.Clear();

            da.Fill(dt);
            if (dt.Rows.Count > 0) {
                MessageBox.Show("Existing Code", "Error");

            }
            else {

                TakeAction();
            }

        }

    }

    else {
        MessageBox.Show("Check even one", "check", MessageBoxButtons.OK, MessageBoxIcon.Warning);

    }

}

So , I have 4 different combinations in total :

chkProduct.Checked : chkMaterial.Checked : Action
--------------------------------------------------------------
              true :                true : InsertSubProduct()
              true :               false : InsertProduct()
             false :                true : InsertMaterial()
             false :               false : Ask User to Check   

Then I need to take action based on the combination :

private void TakeAction()
{
    try
    {

        if (chkProduct.Checked == true && chkMaterial.Checked == false)
        {


            InsertProduct();
            MessageBox.Show("Product Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        else if (chkProduct.Checked == false && chkMaterial.Checked == true)
        {

            InsertMaterial();
            MessageBox.Show("Material Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        else
        {
            InsertSubProduct();
            MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }

    catch (Exception Err)
    {
        MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    finally
    {
        this.Close();
    }


}

The problem is that this part doesn't do anything not showing my message , Not even an error message as if it doesn't exist in my code :

       else
        {
            InsertSubProduct();
            MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

Sorry for long post but i tried my best and debugged the code many times and i can't get pass this , Thanks in advance and your help is deeply appreciated .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2025-01-31 17:59:15

如果threestate属性设置为true,则检查的属性将返回true,用于检查不确定的 checkstate 。

检查threestate属性chkmaterial复选框

If the ThreeState property is set to true, the Checked property will return true for either a Checked or Indeterminate CheckState.

Check the ThreeState property for chkMaterial checkbox

七七 2025-01-31 17:59:15

根据@joel Coehoorn评论,我尝试了不同的方法
我引用“更好的关注点。CheckNulls()只能返回bool,并且不向用户显示任何消息或调用任何其他方法。Checkexistanddo()根本不应该存在(该逻辑应完全在数据库中作为数据库插入产品/材料的一部分())

Based on @Joel Coehoorn comments I tried different approach
I quote " Do separation of concerns better. CheckNulls() should only return a bool, and not show any messages to the user or call any other methods. checkExistAndDo() should not exist at all (that logic should be entirely in the database as part of InsertProduct/MaterialSubproduct()). "

It worked great , Thanks all for replying , I appreciate your guiding .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文