Hi,

I am having some problem using nested if else in C# project. I have no idea why it is always executing the Save() function. I don't want to execute the save() function if the (dr.Read()) is true.

Here is the code:

protected void BtnSave_Click1(object sender, EventArgs e)
    {

        string vDrpListProblemStatusID;
        vDrpListProblemStatusID = DrpListProblemStatusID.SelectedValue.ToString();
        if (vDrpListProblemStatusID == "5" || vDrpListProblemStatusID == "6")
        {
            string sql;
            sql = "select * from Recommendations2 where Status not in (3,4)and Problem_ID = " + TID;
            SqlDataReader dr = gv.GetDRsql(sql);
            if (dr.Read())
            {
                lblMsg.Text = "You have to close all Recommendations.";
                lblMsg.Visible = true;
            }
            else
            {
                save();
                Response.Redirect("ProblemsSaveMsg.aspx");
            }
        }
        else
        {
            save();
            Response.Redirect("ProblemsSaveMsg.aspx");
        }
}

Edited 6 Years Ago by skamranj: modification

Have you tried debugging and setting a breakpoint?
dr.Read() is probably false.

I am not sure if dr.Read() is false because i get the following displayed on the screen

"You have to close all Recommendations."

this means that dr.Read() is true so it executes the following two lines.

lblMsg.Text = "You have to close all Recommendations.";
lblMsg.Visible = true;

In addition it also executes the Else conditions.

I just noticed you have response.redirect so this is must be an ASP.net project. Be aware that the ButtonClick events are handled after the page has loaded. Is it possible something in your page load is firing an event?
Put a breakpoint at the start of your event code and step through it to find out which lines of code definitely fire rather than guessing which ones may have fired.

Edited 6 Years Ago by Geekitygeek: n/a

Comments
Good thinking.

I just noticed you have response.redirect so this is must be an ASP.net project. Be aware that the ButtonClick events are handled after the page has loaded. Is it possible something in your page load is firing an event?
Put a breakpoint at the start of your event code and step through it to find out which lines of code definitely fire rather than guessing which ones may have fired.

Yes its is an Asp.net project. I will see what is firing in the page load event. Just one more thing i want to make sure, did you notice anything wrong with the IF else IF statements.. am i using them correctly.

You can only be sure what is really happening, if you debug your code.

your advice helped, I just did the debug and figured out that the another instance of the click event was triggered running the save() function. Removed it and it works as required.

thanks.

This question has already been answered. Start a new discussion instead.