Solved

ASP.NET Razor Newbie Question

Posted on 2013-12-30
6
274 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Something is null. You'll have to figure out what. If you can post the code, then I can take a gander.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:greddin
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
That worked. I really appreciate your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now