C# - Bug in Code Logic
- by Matthew
I have some code which keeps track of the number of times a button has been clicked. As a matter of fact, when the page first loads, a counter is set to 0. On every postback, the counter is incremented by 1. I have only one button on the page.
The main idea behind this is to allow the user to enter some details 4 times. If he enters invalid details for 4 times, he is redirected to an error page. Otherwise, he is redirected to a confirmation page.
This is my code:
if (!this.IsPostBack)
{
Session["Count"] = 0;
}
else
{
if (Session["Count"] == null)
{
Session.Abandon();
Response.Redirect("CheckOutErrorPage.htm");
}
else
{
int count = (int)Session["Count"];
if (count == 3)
{
Session.Abandon();
Response.Redirect("CheckOutFailure.aspx");
}
else
{
count++;
Session["Count"] = count;
}
}
}
Everything works as it should except that if the user enter invalid details for 3 times and then he enters VALID details on the 4th time, the user is redirected to the Error Page (because he has tried 4 times) instead of the confirmation page.
How can I solve this please?