• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

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

0
introlux
Asked:
introlux
  • 7
  • 4
  • 2
  • +1
2 Solutions
 
OBonioCommented:
Have you stepped through the code in debug mode?  This should shed some light on where the logic is falling over.
0
 
Kevin CrossChief Technology OfficerCommented:
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

0
 
Kevin CrossChief Technology OfficerCommented:
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.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
introluxAuthor Commented:
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
0
 
Kevin CrossChief Technology OfficerCommented:
Sorry, I am more of a Java programmer and probably crossed my syntax's there.
0
 
Kevin CrossChief Technology OfficerCommented:
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");
0
 
introluxAuthor Commented:
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??
0
 
Kevin CrossChief Technology OfficerCommented:
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.
0
 
prairiedogCommented:
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.
0
 
introluxAuthor Commented:
Im still confused, in what I should do. Can you please provide an example of this.

Regards,

introlux
0
 
Kevin CrossChief Technology OfficerCommented:
We were pointing out that you have multiple IF statements above not depending on each other, so no matter what happens you will end up running this piece of code at least once (posibly multiple times as coded):

updateData();
            hardCopy();
            excelFile();
            DelLockSession();
            Response.Redirect("SessPrint.aspx");

If your intent is to display messages if data is completely filled in and stop executing AND only execute if none of the IF criteria is met, then consider doing like this:

protected void btnPrint_Click(object sender, EventArgs e)
{
    if ((opProStatusDrop.SelectedValue.equals("Closed") || opProStatusDrop.SelectedValue.equals("Open - complete")) && CheckIFNull() && (chkFQC.Checked || !chkIR.Checked))
    {
        lblStatus.Text = "You have not confirmed check boxes";
    }
    else if (opProStatusDrop.SelectedValue.equals("Open - field") && !CheckIFTBC())
    {
        ModalPopupExtender1.Show();
    }
    else 
    {
		updateData();
		if (opProStatusDrop.SelectedValue.equals("Closed"))
		{
			sendMail();
		}
		else if (opProStatusDrop.SelectedValue.equals("Open - complete"))
		{
			sendMail2();
		}
		hardCopy();
		excelFile();
		DelLockSession();
		Response.Redirect("SessPrint.aspx");
    }
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
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.
0
 
prairiedogCommented:
>>>Im still confused, in what I should do. Can you please provide an example of this.
Ok, make your code a little bit cleaner so you can get the logic straight:
1. create a method, say "FollowUp" like this:
protected void FollowUp()
{
 updateData();
 hardCopy();
 excelFile();
 DelLockSession();
 Response.Redirect("SessPrint.aspx");
}
2. Change your code as attached code snippet with "switch case" statement instead of "if-elseif-else" statement.
3. Read my comments in the code provided to make sure you handle the situation when "CheckIFNull == false"
Let me know if helps.

protected void btnPrint_Click(object sender, EventArgs e)
{
	switch (opProStatusDrop.SelectedValue)
	{
		case "Closed":
			if (CheckIFNull())
			{
				if (!chkFQC.Checked || !chkIR.Checked) 
				{
				  lblStatus.Text = "You have not confirmed check boxes";
				}
				
				else
				{
					FollowUp();
				}
			}
			/* what if CheckIFNull == false ?
			 * You need to do something here, right?
			 */
			break;
 
		case "Open - complete": /* Make sure the it matches the EXACT string value */
			if (CheckIFNull())
			{
				if (!chkFQC.Checked || !chkIR.Checked)
				{
					lblStatus.Text = "You have not confirmed check boxes";
				}
				else
				{
					FollowUp();
				}
			}
		/* 
		 * else
		 * {
		 * what if CheckIFNull == false ?
		 * You need to do something here, right?
		 * }
		 * */
			break;
 
		case "Open - field":
			if (CheckIFTBC() == false)
			{
				ModalPopupExtender1.Show();
			}
			else
			{
				FollowUp();
			}
			break;
 
		default:
			FollowUp();
	}          
	
}

Open in new window

0
 
introluxAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now