I need to Initialize a value in a Javascript by using a c# literal that makes reference to a Session Variable. I am using the following code
<script type="text/javascript" language="javascript" >
var myIndex = <%= !((Session["myIndex"]).Equals(null)||(Session["myIndex"]).Equals("")) ? Session["backgroundIndex"] : "1" %>;
However the code above is giving me a classic Object reference not set to an instance of an object. error. Why? Shouldn't (Session["myIndex"]).Equals(null) capture this particular error?
-
Are you sure
Session["myIndex"]isn't null?You should add another short circuit OR check for
(Session["myIndex"] == null)and get rid of(Session["myIndex"]).Equals(null). -
object reference error may be because (Session["myIndex"]) is null,
(Session["myIndex"]).Equals is used to compare value so you can use it you want to compare like (Session["myIndex"]).Equals("yourIndex")
-
in the code behind create a protected variable and initialize it there. The main advantage is that you can debug it there. And in plus you can use try catch.
code-behind
protected string sessionValue; private void Page_Load(...) { try { sessionValue = Session["key"].ToString(); } catch { sessionValue = [defaultValue]; } }javascript:
<script> var sessionValue = "<%= sessionValue %>"; </script>This way you can avoid the crash and do something else if the sessionValue is null or has a defaultValue.
slugster : -1 I would not be using try/catch as a substitute for a simple *if (Session["key"] == null)* check.... i hope you don't use that method too often?GxG : the idea behind try catch is that you can avoid a crash, with if each time the session expires you'll get a crash. Even if i'm not that experienced i believe that this is the safest way to do this. If you could avoid a crash by doing a simple if check then it's okay, but if there is the slightest possibility that the application could crash i suggest the safer try catch versionslugster : Nope, the page will not crash if the Session expires - you simply get a fresh Session with nothing in it. Stick with the *if null* check, and never make the assumption that your item has been correctly placed into Session.GxG : thanks for the advice... If it is not a valuable information that you need to put on the session you can try using request parameters or using a hidden field for storing that string(if it can be converted to a string). -
The problem is that
nullisn't an object, and theEquals()method can only be used on objects. If you want to check if your Session object is null, you should use(Session["myIndex"] == null). You can also usestring.IsNullOrEmpty()for an additional check on empty strings. In that case, your code should be:var myIndex = <%= !string.IsNullOrEmpty((string)Session["myIndex"]) ? Session["backgroundIndex"] : "1" %>;Note: Shouldn't
Session["backgroundIndex"]beSession["myIndex"]in this case? Otherwise the null or empty string check is a bit useless in my opinion.slugster : -1 for you also... you cannot just do a *string.IsNullOrEmpty(Session["myIndex"])* - it won't even compile as *Session[key]* returns an object, not a string. Your explanation was good though.Prutswonder : @slugster: From the context of the sample, it's clear that `Session["myIndex"]` is a string. If it is a different object, then you might as well let the exception be thrown, because you are not expecting it. Besides, the provided sample doesn't check for strings either.slugster : I agree the sample doesn't check for (or cast to) strings either. It doesn't matter what is held at that particular index, it is returned as type *object*. At the time i downvoted there were 4 answers, yours was the best but with syntactic errors. If you edit the post i will reverse my downvote.Prutswonder : Adding a cast to string is a good idea in this case, so I've updated my sample.slugster : Sweet as, downvote reversed :) -
This will work (I have tested it!):
var myIndex = <%=!string.IsNullOrEmpty( (string)Session["myIndex"] ) ? Session["myIndex"] : "1" %> ;
0 comments:
Post a Comment