Solved

ASP.NET Razor Newbie Question

Posted on 2013-12-30
6
278 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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