[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type)

Posted on 2009-02-14
3
Medium Priority
?
2,184 Views
Last Modified: 2012-05-06
public static System.Int32 Age
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Age") as System.Int32; }
            set { HttpContext.Current.Profile.SetPropertyValue("Age", value); }
        }

Error      1      The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type)      C:\inetpub\wwwroot\OmegaLove\OL_Web\App_Code\UserProfile.cs      49      26      C:\...\OL_Web\

using System;
using System.Web;
using System.IO;
using System.Xml.Serialization;
using System.Web.Caching;
 
public static class UserProfile
{
    public class Personal
    {
        public static System.String UserName
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("UserName") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("UserName", value); }
        }
 
        public static System.String Password
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Password") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Password", value); }
        }
 
        public static System.String Email
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Email") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Email", value); }
        }
 
        public static System.String FirstName
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("FirstName") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("FirstName", value); }
        }
 
        public static System.String LastName
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("LastName") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("LastName", value); }
        }
 
        public static System.DateTime DOB
        {
            get { return (System.DateTime)HttpContext.Current.Profile.GetPropertyValue("DOB"); }
            set { HttpContext.Current.Profile.SetPropertyValue("DOB", value); }
        }
 
        public static System.Int32 Age
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Age") as System.Int32; }
            set { HttpContext.Current.Profile.SetPropertyValue("Age", value); }
        }
 
        public static System.String Gender
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Gender") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Gender", value); }
        }
 
        public static System.String Seeking
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Seeking") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Seeking", value); }
        }
 
        public static System.String ConfirmationGUID
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("ConfirmationGUID") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("ConfirmationGUID", value); }
        }
 
        public static System.String UserIP
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("UserIP") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("UserIP", value); }
        }
 
        public static System.String SessionID
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("SessionID") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("SessionID", value); }
        }
 
    }
    public class Address
    {
        public static System.String Country
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Country") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Country", value); }
        }
 
        public static System.String Region
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Region") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Region", value); }
        }
 
        public static System.String City
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("City") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("City", value); }
        }
 
        public static System.String ZipCode
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("ZipCode") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("ZipCode", value); }
        }
 
    }
    public class Preferences
    {
        public static String Theme
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Theme") as String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Theme", value); }
        }
 
        public static String Culture
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Culture") as String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Culture", value); }
        }
 
    }
    public static void Save()
    {
        HttpContext.Current.Profile.Save();
    }
}

Open in new window

0
Comment
Question by:mathieu_cupryk
  • 2
3 Comments
 
LVL 16

Expert Comment

by:sunithnair
ID: 23641379
Try this
    public static class UserProfile
    {
        public class Personal
        {
            public static System.String UserName
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("UserName") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("UserName", value); }
            }
 
            public static System.String Password
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Password") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Password", value); }
            }
 
            public static System.String Email
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Email") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Email", value); }
            }
 
            public static System.String FirstName
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("FirstName") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("FirstName", value); }
            }
 
            public static System.String LastName
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("LastName") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("LastName", value); }
            }
 
            public static System.DateTime DOB
            {
                get { return (System.DateTime)HttpContext.Current.Profile.GetPropertyValue("DOB"); }
                set { HttpContext.Current.Profile.SetPropertyValue("DOB", value); }
            }
 
            public static System.Int32? Age
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Age") as System.Int32?; }
                set { HttpContext.Current.Profile.SetPropertyValue("Age", value); }
            }
 
            public static System.String Gender
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Gender") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Gender", value); }
            }
 
            public static System.String Seeking
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Seeking") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Seeking", value); }
            }
 
            public static System.String ConfirmationGUID
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("ConfirmationGUID") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("ConfirmationGUID", value); }
            }
 
            public static System.String UserIP
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("UserIP") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("UserIP", value); }
            }
 
            public static System.String SessionID
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("SessionID") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("SessionID", value); }
            }
 
        }
        public class Address
        {
            public static System.String Country
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Country") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Country", value); }
            }
 
            public static System.String Region
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Region") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Region", value); }
            }
 
            public static System.String City
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("City") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("City", value); }
            }
 
            public static System.String ZipCode
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("ZipCode") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("ZipCode", value); }
            }
 
        }
        public class Preferences
        {
            public static String Theme
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Theme") as String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Theme", value); }
            }
 
            public static String Culture
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Culture") as String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Culture", value); }
            }
 
        }
        public static void Save()
        {
            HttpContext.Current.Profile.Save();
        }
    }

Open in new window

0
 
LVL 16

Accepted Solution

by:
sunithnair earned 2000 total points
ID: 23641394
Make it as a nullable type because HttpContext.Current.Profile.GetPropertyValue("Age") can be a null value and System.Int32 cannot be a null value because it is a non nullable value type.
0
 

Author Closing Comment

by:mathieu_cupryk
ID: 31546935
excellent.
I will be posting two questions one with on how I can show all the information about a user. with that class and secondly one with serialization of that object,
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

830 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