?
Solved

ASP.NET Razor Newbie Question

Posted on 2013-12-30
6
Medium Priority
?
295 Views
Last Modified: 2013-12-30
How do I check if a session variable is true?  I keep getting an error saying it cannot convert object to bool.

I've tried something like this:

@if (Session["myVariable"] == true) {
  Do something
}
0
Comment
Question by:greddin
  • 3
  • 3
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39747292
This isn't so much a Razor issue as it is a C# one. You have to ensure that you are comparing the same types when using ==. The things you store in Session are stored as type Object. That means when you want to get them out, and treat them like what they actually are, then you need to cast. Simply add a cast to your logic:

@if ((bool)Session["myVariable"] == true) {
  Do something
}

Open in new window

0
 

Author Comment

by:greddin
ID: 39747305
Thanks kaufmed:

The syntax you give above compiles without error but gives me this error on the page:

"Object reference not set to an instance of an object"

Any ideas?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39747334
Something is null. You'll have to figure out what. If you can post the code, then I can take a gander.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:greddin
ID: 39747339
It looks like this:

@if ((bool)Session["isSuperAdmin"] == true) {
    <li><a href="#">Admin Link</a></li>
}

isSuperAdmin is a bit/boolean field from SQL.  0 or 1
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39747361
If that's the code that is throwing the error, then are you sure you have something assigned to the session key "isSuperAdmin"? You might try instead:

@if (Session["isSuperAdmin"] != null && (bool)Session["isSuperAdmin"] == true) {

Open in new window

0
 

Author Closing Comment

by:greddin
ID: 39747373
That worked. I really appreciate your help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question