Solved

IF Nested Problem - HELP c#

Posted on 2008-10-02
14
330 Views
Last Modified: 2013-12-17
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
Comment
Question by:introlux
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 8

Expert Comment

by:OBonio
ID: 22625650
Have you stepped through the code in debug mode?  This should shed some light on where the logic is falling over.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22625654
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
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22625666
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
 

Author Comment

by:introlux
ID: 22625847
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
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22625895
Sorry, I am more of a Java programmer and probably crossed my syntax's there.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22625963
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
 

Author Comment

by:introlux
ID: 22625989
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22626041
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
 
LVL 22

Expert Comment

by:prairiedog
ID: 22627353
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
 

Author Comment

by:introlux
ID: 22632088
Im still confused, in what I should do. Can you please provide an example of this.

Regards,

introlux
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 22632538
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
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22632541
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
 
LVL 22

Assisted Solution

by:prairiedog
prairiedog earned 250 total points
ID: 22634329
>>>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
 

Author Closing Comment

by:introlux
ID: 31502426
Thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now