Link to home
Start Free TrialLog in
Avatar of introlux
introluxFlag for United Kingdom of Great Britain and Northern Ireland

asked on

IF Nested Problem - HELP c#

Hi,

I am having problem with my nested IF statement as IF the statement is true for some strange reason it is not executing the error message!

I have attached the code snippet to see if you can help me.

Regards,

introlux
protected void btnPrint_Click(object sender, EventArgs e)
    {
        if (opProStatusDrop.SelectedValue == "Closed")
        {
            if (CheckIFNull() == true)
            {
                if (!chkFQC.Checked || !chkIR.Checked) //(chkFQC.Checked == false || chkIR.Checked == false)
                {
                    lblStatus.Text = "You have not confirmed check boxes";
                }
                else
                {
                    updateData();
                    sendMail();
                    hardCopy();
                    excelFile();
                    DelLockSession();
                    Response.Redirect("SessPrint.aspx");
                }
            }
        }
        if (opProStatusDrop.SelectedValue == "Open - complete")
        {
            if (CheckIFNull() == true)
            {
                if (!chkFQC.Checked || !chkIR.Checked) //(chkFQC.Checked == false || chkIR.Checked == false)
                {
                    lblStatus.Text = "You have not confirmed check boxes";
                }
                else
                {
                    updateData();
                    sendMail2();
                    hardCopy();
                    excelFile();
                    DelLockSession();
                    Response.Redirect("SessPrint.aspx");
                }
            }
        }
        if (opProStatusDrop.SelectedValue == "Open - field")
        {
            if (CheckIFTBC() == false)
            {
                ModalPopupExtender1.Show();
            }
            else
            {
                updateData();
                hardCopy();
                excelFile();
                DelLockSession();
                Response.Redirect("SessPrint.aspx");
            }
        }
        else
        {
            updateData();
            hardCopy();
            excelFile();
            DelLockSession();
            Response.Redirect("SessPrint.aspx");
        }
    }
 
    bool CheckIFNull()
    {
        bool isValid = true;
        DataTable dt = new DataTable();
        string selectSQL;
 
        selectSQL = "SELECT *FROM tblProCom ";
        selectSQL += "WHERE Pid='" + txtPid.Text + "'";
 
        OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("App_Data/ProCom.mdb"));
        OleDbCommand objCmd = new OleDbCommand(selectSQL, objConn);
 
        OleDbDataAdapter obj = new OleDbDataAdapter();
        obj.SelectCommand = objCmd;
 
        obj.Fill(dt);
 
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            if (dt.Rows[0][i].ToString().Trim() == string.Empty || dt.Rows[0][i].ToString().Trim() == "Please select" || dt.Rows[0][i] == null || dt.Rows[0][i] == "" || dt.Rows[0][i].ToString().Trim() == "TBC" || dt.Rows[0][i].ToString().Trim() == "tbc")
            {
                lblStatus.Text = "You have not completed all the fields or TBC fields are outstanding";
                isValid = false;
                break;
            }
            if (lstPManager.SelectedValue == "Please select" || lstResearch_Agency.SelectedValue == "Please select" || lstScripter.SelectedValue == "Please select" || txtSSup.Text == "" || txtKeyword.Text == "" ||
                lstPManager.SelectedValue == string.Empty || lstResearch_Agency.SelectedValue == string.Empty || lstScripter.SelectedValue == string.Empty || txtSSup.Text == string.Empty || txtKeyword.Text == string.Empty ||
                txtPoints.Text == "" || txtSLine.Text == "" || txtActFSD.Text == "" || txtActFFDD.Text == "" || txtActFFnlDD.Text == "" || txtActLOI.Text == "" || txtFQC.Text == "" || txtNumQFalls.Text == "" ||
                txtPoints.Text == string.Empty || txtSLine.Text == string.Empty || txtActFSD.Text == string.Empty || txtActFFDD.Text == string.Empty || txtActFFnlDD.Text == string.Empty || txtActLOI.Text == string.Empty || txtFQC.Text == string.Empty || txtNumQFalls.Text == string.Empty ||
                txtNumScreen.Text == "" || txtFIR.Text == "" || txtNumIntSent.Text == "" || txtFResRate.Text == "" || txtIssues.Text == "" || opProStatusDrop.SelectedValue == "Please select" || InvoiceableDrop.SelectedValue == "" ||
                txtNumScreen.Text == string.Empty || txtFIR.Text == string.Empty || txtNumIntSent.Text == string.Empty || txtFResRate.Text == string.Empty || txtIssues.Text == string.Empty || opProStatusDrop.SelectedValue == string.Empty || InvoiceableDrop.SelectedValue == string.Empty ||
                delTimeOnSpecDrop.SelectedValue == "" || txtResCloseTime.Text == "" ||
                delTimeOnSpecDrop.SelectedValue == string.Empty || txtResCloseTime.Text == string.Empty ||
                lstPManager.SelectedValue == "TBC" || lstResearch_Agency.SelectedValue == "TBC" || lstScripter.SelectedValue == "TBC" || txtSSup.Text == "TBC" || txtKeyword.Text == "TBC" ||
                lstPManager.SelectedValue == "tbc" || lstResearch_Agency.SelectedValue == "tbc" || lstScripter.SelectedValue == "tbc" || txtSSup.Text == "tbc" || txtKeyword.Text == "tbc" ||
                txtPoints.Text == "TBC" || txtSLine.Text == "TBC" || txtActFSD.Text == "TBC" || txtActFFDD.Text == "TBC" || txtActFFnlDD.Text == "TBC" || txtActLOI.Text == "TBC" || txtFQC.Text == "TBC" || txtNumQFalls.Text == "TBC" ||
                txtPoints.Text == "tbc" || txtSLine.Text == "tbc" || txtActFSD.Text == "tbc" || txtActFFDD.Text == "tbc" || txtActFFnlDD.Text == "tbc" || txtActLOI.Text == "tbc" || txtFQC.Text == "tbc" || txtNumQFalls.Text == "tbc" ||
                txtNumScreen.Text == "TBC" || txtFIR.Text == "TBC" || txtNumIntSent.Text == "TBC" || txtFResRate.Text == "TBC" || txtIssues.Text == "TBC" || opProStatusDrop.SelectedValue == "TBC" || InvoiceableDrop.SelectedValue == "TBC" ||
                txtNumScreen.Text == "tbc" || txtFIR.Text == "tbc" || txtNumIntSent.Text == "tbc" || txtFResRate.Text == "tbc" || txtIssues.Text == "tbc" || opProStatusDrop.SelectedValue == "tbc" || InvoiceableDrop.SelectedValue == "tbc" ||
                delTimeOnSpecDrop.SelectedValue == "TBC" || txtResCloseTime.Text == "TBC" ||
                delTimeOnSpecDrop.SelectedValue == "tbc" || txtResCloseTime.Text == "tbc")
            {
                lblStatus.Text = "You have not completed all the fields or TBC fields are outstanding";
                isValid = false;
            }
            else
            {
                lblStatus.Text = "OK NULL";
                isValid = true;
            }
        }
        return isValid;
    }

Open in new window

Avatar of OBonio
OBonio
Flag of United Kingdom of Great Britain and Northern Ireland image

Have you stepped through the code in debug mode?  This should shed some light on where the logic is falling over.
Avatar of Kevin Cross
Couple of suggestions here.  Maybe will help in eliminate troubleshooting points to get to answer of issue.  Not sure which nested IF isn't working, but here are my thoughts.

It appeared your logic between the two IF statements at the top only differed by sendMail() versus sendMail2().  If other major differences, then I would invert and have CheckIfNull() first then do if on status selected value.  For strings, I would use equals instead of == and bool treat as bool so no == at all like you are doing with the .Checked tests.
protected void btnPrint_Click(object sender, EventArgs e)
        {
            if ((opProStatusDrop.SelectedValue.equals("Closed") || opProStatusDrop.SelectedValue.equals("Open - complete")) && CheckIFNull())
            {
                if (!chkFQC.Checked || !chkIR.Checked) //(chkFQC.Checked == false || chkIR.Checked == false)
                {
                    lblStatus.Text = "You have not confirmed check boxes";
                }
                else
                {
                    updateData();
                    if (opProStatusDrop.SelectedValue.equals("Closed"))
                    {
                        sendMail();
                    }
                    else
                    {
                        sendMail2();
                    }
                    hardCopy();
                    excelFile();
                    DelLockSession();
                    Response.Redirect("SessPrint.aspx");
                }
            }
            if (opProStatusDrop.SelectedValue.equals("Open - field"))
            {
                if (!CheckIFTBC())
                {
                    ModalPopupExtender1.Show();
                }
                else
                {
                    updateData();
                    hardCopy();
                    excelFile();
                    DelLockSession();
                    Response.Redirect("SessPrint.aspx");
                }
            }
            else
            {
                updateData();
                hardCopy();
                excelFile();
                DelLockSession();
                Response.Redirect("SessPrint.aspx");
            }
        }

Open in new window

If you can find in DEBUG mode as suggested which IF is not being read correctly, let us know.  Although, changing logic around as suggested may help as it simplifies code and eliminates point of failure.
Avatar of introlux

ASKER

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0117: 'string' does not contain a definition for 'equals'

Source Error:

 

Line 661:    protected void btnPrint_Click(object sender, EventArgs e)
Line 662:    {
Line 663:        if ((opProStatusDrop.SelectedValue.equals("Closed") || opProStatusDrop.SelectedValue.equals("Open - complete")) && CheckIFNull())
Line 664:        {
Line 665:            if (!chkFQC.Checked || !chkIR.Checked) //(chkFQC.Checked == false || chkIR.Checked == false)
 
The IF statement that was not working is:

            if (opProStatusDrop.SelectedValue.equals("Open - field"))

Regards,

introlux
Sorry, I am more of a Java programmer and probably crossed my syntax's there.
Ok, looked in Visual Studio and looks like I put in lower case which is Java.  Equals is uppercase in C#.

opProStatusDrop.SelectedValue.Equals("Closed"); // should work -- compiles for me.

Can also use:
String.Compare(opProStatusDrop.SelectedValue, "Closed");
I have tried the above and it still allows the process to happen. Do you think I should use the equal function for the null check??
No if process is happening, then that means something you are testing on is returning true but you expect it to return false.

Note that your CheckIFNull assumes TRUE.
bool isValid = true;

Usually you want this set to the rule and not the exception.  What I mean by that is if only a few instances this should be true then set to false here so that only time you get true is if specific condition is satified.  As it stands, even if nothing works below this line you will get true.
There is a log flaw in your code that may caused this problem.
For your outer "if" logic, you should use "if...else if...else if...else" logic, instead of "if...if...if...else" logic as you are using now. Even better, try to use "switch case" logic.
Another thing you should check is to make sure the SelectedValue matches your compared string (case-sensitive, remember). If possible, use SelectedIndex.
Im still confused, in what I should do. Can you please provide an example of this.

Regards,

introlux
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You need to change up the logic is the net of what we are saying.  You can use different approach using a boolean to signify processing should be done or not and set it to false when conditions are true.  That will allow you to split up some of the IF statements as you had them but not fire the update in multiple places or incorrectly.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks