C# - Bug in Code Logic
Posted
by
Matthew
on Stack Overflow
See other posts from Stack Overflow
or by Matthew
Published on 2013-07-02T17:03:00Z
Indexed on
2013/07/02
17:05 UTC
Read the original article
Hit count: 237
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?
© Stack Overflow or respective owner