Resolved Why my methods interfering with each others and with if statements in C#?

Amr Ashraf

Member
Joined
Apr 5, 2022
Messages
14
Programming Experience
Beginner
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 :
CheckNulls:
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 :

CheckExistAndDo:
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 :

TakeAction:
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 :

Problem:
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 .
 
Solution
You are writing C# code. Your code should look like C# code. Why are you using the Java and JavaScript style indentation? Why the Java style naming convention? Why the Java and JavaScript style check for booleans? Let me fix things up for you and it'll become more obvious why the call to InsertSubProduct() is unreachable.

C#:
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 :" +...
i tried my best and debugged the code many times
Did you really debug by setting breakpoints and step through the code and inspect variable values? Or was your idea of debugging just running the program and hope for the best, or at worst see error messages?
 
You are writing C# code. Your code should look like C# code. Why are you using the Java and JavaScript style indentation? Why the Java style naming convention? Why the Java and JavaScript style check for booleans? Let me fix things up for you and it'll become more obvious why the call to InsertSubProduct() is unreachable.

C#:
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);
    }
}

void CheckExistAndDo()
{
    if (chkProduct.Checked || chkMaterial.Checked)
    {
        if (chkProduct.Checked)
        {
            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);
    }
}

private void TakeAction()
{
    try
    {
        if (chkProduct.Checked && !chkMaterial.Checked)
        {
            InsertProduct();
            MessageBox.Show("Product Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else if (!chkProduct.Checked && chkMaterial.Checked)
        {
            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();
    }
}

Now look at the case for chkProduct.Checked == true on line 25. The condition is true, so it goes to line 27. But chkMaterial.Checked == true so the condition is false. The causes execution to go to line 43 and then line 65.
 
Solution
Now look at the case for chkProduct.Checked == true on line 25. The condition is true, so it goes to line 27. But chkMaterial.Checked == true so the condition is false. The causes execution to go to line 43 and then line 65.
Thanks so much for your help , It was so helpful i will re create the entire idea to be more efficient
You are writing C# code. Your code should look like C# code. Why are you using the Java and JavaScript style indentation? Why the Java style naming convention? Why the Java and JavaScript style check for booleans?
I was trying some online website to beautify the code and fix the indents and that what it have done 😄
Did you really debug by setting breakpoints and step through the code and inspect variable values? Or was your idea of debugging just running the program and hope for the best, or at worst see error messages?
I remove the most of the code , leave some and restore the rest bit by bit to see which part is causing problems or doesn't do it's part , Be easy on me I am very new to this :oops:
 
I know there is a lot to learn. To get the most bang for your buck, take the 1-2 hours to learn how to use the debugger built into Visual Studio. The skills you learn will continue to be useful in your journey and will save you a lot of time in the future.
 

Latest posts

Back
Top Bottom