int counter=1;
Session["counter"]=counter;
When you want to update that, read the value and convert it to int and then increase, save back
if(Session["counter"]!=null)
{
counter=Convert.ToInt32(Session["counter"]);
}
counter++;
Session["counter"]=counter;
EDIT : As per the comment, This is how you can check the counter value. I wrapped the checking inside 2 methods to set and get, you can even use Properties as others mentioned.
private int GetLoginAttempts()
{
int counter=0;
if(Session["counter"]!=null)
{
counter=Convert.ToInt32(Session["counter"]);
}
return counter;
}
private void IncreaseLoginAttempts()
{
if(Session["counter"]!=null)
{
counter=Convert.ToInt32(Session["counter"]);
}
counter++;
Session["counter"]=counter;
}
and when user tries to login( in your button click / action method ), check the Current value
if(GetLoginAttempts()==3)
{
//This means user already tried 3 times, show him a message !
}
else
{
//Do the login process, If login fails, increase the counter
IncreaseLoginAttempts()
}