Home > Back-end >  Why my methods interfering with each others and with if statements in C#?
Why my methods interfering with each others and with if statements in C#?

Time:04-27

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 .

CodePudding user response:

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

CodePudding user response:

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 .

  • Related