开发者

How to convert Session Variable to Integer Type in C#

I am using C#

I am trying to check whether my login attempt is not more than 3, I mean with the below condition:

if (((int)Session["LoginAttempt"]) != 3)
{
}

In Login failed condition I am doing increment like below:

Session["LoginAttempt"] = ((int) Session["LoginAttempt"]) + 1;

But it is giving me this error: "Object reference not set to an instance of an object."

Suggest开发者_开发百科ions please!


Sorry Guys,

I just changed the integer converting code from

((int) Session["LoginAttempt"])

to

Convert.ToInt32(Session["LoginAttempt"]) + 1;

and now it is working fine for me, please suggest incase of any issues in it.

Thanks!


Try the magic code:

Session["LoginAttempt"] = ((int?)Session["LoginAttempt"] ?? 0) + 1;

This will convert the session variable Session["LoginAttempt"] to a nullable int (an int that can be null) the ?? 0 provides a value 0 if it is null, so the calculation succeeds.

The Session["LoginAttempt"] can be null if it is not initialized before.


You need to test to see if the Session variable exists before you can use it and assign to it.

Here you are doing an increment:

Session["LoginAttempt"] = ((int) Session["LoginAttempt"]) + 1;

But, if the Session["LoginAttempt"] does not exist, this will explain your error. A quick null test before the increment should sort it out.

if (Session["LoginAttempt"] != null)
    Session["LoginAttempt"] = ((int)Session["LoginAttempt"]) + 1;


Why not encapsulate the LoginAttempt value as a property and auto-assign a value:

protected int LoginAttempt
{
    get
    {
        if (Session["LoginAttempt"] == null)
        {
            Session["LoginAttempt"] = 1;
        }
        return Convert.ToInt32(Session["LoginAttempt"].ToString());
    }
    set
    {
        Session["LoginAttempt"] = value;
    }
}

so that the main body of the function is more readable:

if (LoginAttempt < 3)
{
}


It will do that the first time you try to set it if you haven't previously initialised it. Try this instead:

if (Session["LoginAttempt"] == null)
    Session["LoginAttempt"] = 1;
else
    ((int)Session["LoginAttempt"]) += 1;


Divide your nontrivial code in parts:

int sessionLogicAttempt = (int)Session["LoginAttempt"];
int someValue = sessionLogicAttempt + 1;
Session["LoginAttempt"] = someValue;

Also, add assertions to check for the values you are assuming.


//read
object attemptObj = Session["LoginAttempt"]
int attempt = 0;
if (attemptObj != null) attempt = (int)attemptObj ;

////write
Session["LoginAttempt"] = attempt++;


try make sure not to cast something that is possibly a null value.

int i = Session["val"] == null ? 0 : (int)Session["val"];

though this could mess you up as well if some other programmer uses your "val" session and put a non int value there so.

        int y = 0;
        if (int.TryParse(Session["val"] == null ? string.Empty : Session["val"].ToString(), out y))
        {
            // got your int value
        }
        else
        {
            // no int value in session val
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜