Solved

Help with Nested IF Statement c#

Posted on 2008-09-29
10
963 Views
Last Modified: 2012-05-05
Hi,

I am having problems with my IF statement and wanted to check if I have nest it correctly. as regardless if CheckIFNull is true, it still goes ahead with the rest of the code!

Regards,

introlux
protected void btnPrint_Click(object sender, EventArgs e)

    {

        if (opProStatusDrop.SelectedValue == "Closed")

        {

            CheckIFNull();
 

            if (chkFQC.Checked == false || chkIR.Checked == false)

            {

                lblStatus.Text = "You have not confirmed final quota counts or final response rate";

            }
 

            else

            {

                updateData();

                sendMail();

                hardCopy();

                excelFile();

                DelLockSession();

                Response.Redirect("SessPrint.aspx");

            }

        }

        if (opProStatusDrop.SelectedValue == "Open - complete")

        {

            CheckIFNull();
 

            if (chkFQC.Checked == false || chkIR.Checked == false)

            {

                lblStatus.Text = "You have not confirmed final quota counts or final response rate";

            }

            else

            {

                updateData();

                sendMail2();

                hardCopy();

                excelFile();

                DelLockSession();

                Response.Redirect("SessPrint.aspx");

            }

        }

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

        {

            CheckIFTBC();

        }

        else

        {

            updateData();

            sendMail2();

            hardCopy();

            excelFile();

            DelLockSession();

            Response.Redirect("SessPrint.aspx");

        }

    }

Open in new window

0
Comment
Question by:introlux
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 18

Assisted Solution

by:Priest04
Priest04 earned 50 total points
ID: 22595163
CheckIFNull();

what does this code do? It just calls some procedure, in which, I presume, you have some validation code, but NO value is returned. It should look something like

bool CheckIfNull()
{
    if (some condition)
    {
        return true;
    }
    else
    {
        return false;
    }
}

and then in your code it would look something like

if (CheckIFNull())
{
    // code to execute
}

or

if (!CheckIFNull())
{
    // code to execute
}

Goran

This way you have it at the moment, it only calls the procedure, and continues with execution.

Goran
0
 

Author Comment

by:introlux
ID: 22595173
I have supplied the code for CheckIFNull and CheckIFTBC code.


    void CheckIFNull()

    {

        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";

                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)

            {

                lblStatus.Text = "You have not completed all the fields or TBC fields are outstanding";

            }

            else

            {

                lblStatus.Text = "OK NULL";

            }

        }

    }
 

    void CheckIFTBC()

    {

        DataTable dt = new DataTable();

        string selectSQL;
 

        selectSQL = "SELECT * 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() == "TBC")

            {

                lblStatus.Text = "You still have TBC values";

                break;

            }

            if (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 still have TBC values";

            }

            else

            {

                lblStatus.Text = "OK TBC";

            }

        }

    }

Open in new window

0
 
LVL 15

Assisted Solution

by:angus_young_acdc
angus_young_acdc earned 100 total points
ID: 22595190
It is running through all of the code because there is nothing telling it not to.

If the rest of the code relies on CheckIfNulls result then you need to make sure that there is a return which can be used as a breaking point.  EG:

bool myResult = CheckIfNull()  // where CheckIfNull has a return type of bool

if (myResult == true)
{
      // Whatever you have checked is null
      // Do whatever you want to handle that
}
else
{
      // Its not null
      // Do whatever you want to handle the result
}

That way it will only continue through the code when you get the result that you actually want.
0
 

Author Comment

by:introlux
ID: 22595211
Is there any chance you can provide me an example code using one of my methods?

As I am looking at the guidlines you set but I do not understand.

Regards,

introlux
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 300 total points
ID: 22595299
change CheckIFNull like this:

(you would make similar changes for


    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 = fals;

                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)

            {

                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;

    }
 
 

then chagle the main procedure, like this:
 

            if (CheckIFNull() == true)

            {

 

             if (chkFQC.Checked == false || chkIR.Checked == false)

             {

                 lblStatus.Text = "You have not confirmed final quota counts or final response rate";

             }

 

             else

             {

                 updateData();

                 sendMail();

                 hardCopy();

                 excelFile();

                 DelLockSession();

                 Response.Redirect("SessPrint.aspx");

             }

            }

Open in new window

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

 

Author Comment

by:introlux
ID: 22595377
For some strange reason its not picking the word TBC or tbc! It is still allowing the user to go through even though TBC or tbc fields/data exists!

any idea??
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 22595583
What do you mean?  Do you mean it does this line but doesn't display your error message?
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")
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 50 total points
ID: 22595608
A side comment, this kind of lines:

if (chkFQC.Checked == false || chkIR.Checked == false)

could be simplified as:

if (!chkFQC.Checked || !chkIR.Checked)
0
 

Author Closing Comment

by:introlux
ID: 31501138
Thank you for your support in helping me sort this problem out!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 22597827
Glad to be of assistance.

AW
0

Featured Post

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

Join & Write a Comment

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

19 Experts available now in Live!

Get 1:1 Help Now