Link to home
Start Free TrialLog in
Avatar of fwsteal
fwsteal

asked on

HttpContext.Current.Profile null or not present

Need to work with Nulls or if the value is not found.

protected void Page_Load(object sender, EventArgs e)
{
 try
  {
   System.Web.Profile.ProfileBase p = HttpContext.Current.Profile;

   //firstname
   if (p.GetPropertyValue("FirstName").ToString == null)
    {
     lblFullName.Text = "firstname";
    }
     else
    {
     lblFullName.Text = p.GetPropertyValue("FirstName").ToString();
    }

How do I write this to check for a null value or if the value
does not exist?
Avatar of Answer_Me
Answer_Me
Flag of India image

Add a check for profilebase object.

 try
  {
   System.Web.Profile.ProfileBase p = HttpContext.Current.Profile;
// New check introduced for profile check
 if (p!=null)
{
   //firstname
   if (p.GetPropertyValue("FirstName").ToString == null)
    {
     lblFullName.Text = "firstname";
    }
     else
    {
     lblFullName.Text = p.GetPropertyValue("FirstName").ToString();
    }
}
Avatar of fwsteal
fwsteal

ASKER

what if I want to include lastname, email, phone and such?
Avatar of fwsteal

ASKER

Also, I need to render 'not found' if the value is not located. I tried this but had no luck:


          System.Web.Profile.ProfileBase p = HttpContext.Current.Profile;


                if (p != null)
                {
                    //firstname
                    if (p.GetPropertyValue("FirstName") == null)
                    {
                        lblFirstName.Text = "firstname not found";
                    }
                    else
                    {
                        lblFirstName.Text = p.GetPropertyValue("FirstName").ToString();
                    }

                    //lastname
                    if (p.GetPropertyValue("LastName") == null)
                    {
                        lblLastName.Text = "lastname not found";
                    }
                    else
                    {
                        lblLastName.Text = p.GetPropertyValue("LastName").ToString();
                    }

                    //email
                    if (p.GetPropertyValue("Email").ToString() == null)
                    {
                        lblEmail.Text = "email not found";
                    }
                    else
                    {
                        lblEmail.Text = p.GetPropertyValue("Email").ToString();
                    }

                    //job title
                    if (p.GetPropertyValue("JobTitle").ToString() == null)
                    {
                        lblJobTitle.Text = "jobtitle not found";
                    }
                    else
                    {
                        lblJobTitle.Text = p.GetPropertyValue("JobTitle").ToString();
                    }

                    //customer id
                    if (p.GetPropertyValue("CustomerID").ToString() == null)
                    {
                        lblCustomerID.Text = "CustomerID not found";
                    }
                    else
                    {
                        lblCustomerID.Text = p.GetPropertyValue("CustomerID").ToString();
                    }

                    //account name
                    if (p.GetPropertyValue("AccountName").ToString() == null)
                    {
                        lblAccountName.Text = "AccountName not found";
                    }
                    else
                    {
                        lblAccountName.Text = p.GetPropertyValue("AccountName").ToString();
                    }

                }
Avatar of Swapnil
Hi fwsteal,
You can check it by following way, this will work

                   if (p.Properties["FirstName"] == null)
                    {
                        lblFirstName.Text = "firstname not found";
                    }
                    else
                    {
                        lblFirstName.Text = p.GetPropertyValue("FirstName").ToString();
                    }

Regards,
NetSwap
Avatar of fwsteal

ASKER

NetSwap,

Here is what I've done thus far and with your suggestion.

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                System.Web.Profile.ProfileBase p = HttpContext.Current.Profile;

                if (p != null)
                {
                    //firstname
                    if (p.GetPropertyValue("FirstName") == null)
                    //if (p.Properties["FirstName"] == null)    // nothing is rendered out
                    {
                        lblFirstName.Text = "firstname is null";
                    }
                    else
                    {
                        lblFirstName.Text = p.GetPropertyValue("FirstName").ToString();
                    }

                    //lastname
                    if (p.GetPropertyValue("LastName") == null)
                    {
                        lblLastName.Text = "lastname is null";
                    }
                    else
                    {
                        lblLastName.Text = p.GetPropertyValue("LastName").ToString();
                    }

                    //email
                    if (p.GetPropertyValue("Email") == null)
                    {
                        lblEmail.Text = "email is null";
                    }
                    else
                    {
                        lblEmail.Text = p.GetPropertyValue("Email").ToString();
                    }

                    //job title
                    if (p.GetPropertyValue("JobTitle") == null)
                    {
                        lblJobTitle.Text = "jobtitle is null";
                    }
                    else
                    {
                        lblJobTitle.Text = p.GetPropertyValue("JobTitle").ToString();
                    }

                    //customer id
                    if (p.GetPropertyValue("CustomerID") == null)
                    {
                        lblCustomerID.Text = "CustomerID is null";
                    }
                    else
                    {
                        lblCustomerID.Text = p.GetPropertyValue("CustomerID").ToString();
                    }

                    //account name
                    if (p.GetPropertyValue("AccountName") == null)
                    {
                        lblAccountName.Text = " - AccountName is null";
                    }
                    else
                    {
                        if (p.GetPropertyValue("AccountName") == "")
                        {
                            lblAccountName.Text = " - AccountName is empty";
                        }
                        else
                        {
                            lblAccountName.Text = " - " + p.GetPropertyValue("AccountName").ToString();
                        }  
                    }

                    //account type
                    if (p.GetPropertyValue("AccountType") == null)
                    {
                        lblAccountType.Text = "(AccountType is null)";
                    }
                    else
                    {
                        if (p.GetPropertyValue("AccountType") == "Parent")
                        {
                            lblAccountType.Text = "(Parent Account)";
                            lkbtnChangeAccount.Text = "Change Account";
                            lkbtnChangeAccount.PostBackUrl = "s/sp/changeaccount.aspx";
                        }
                        else
                        {
                            lblAccountType.Text = "";
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                lblError.Text="Message:" + ex.Message.ToString();
            }
        }

By check the property collection, I get nothing rendered. Any suggestions?
you can handle it by otherways as following

                    if (p.Properties["FirstName"] != null && p.GetPropertyValue("FirstName").ToString() != "")    
                    {
                        lblFirstName.Text = p.GetPropertyValue("FirstName").ToString() ;

                    }
                    else
                    {
                        lblFirstName.Text = "firstname is null or empty";

                    }
Avatar of fwsteal

ASKER

I'm running the aspx file with the persistent data on a control file under moss2007 and it renders out nothing when I use: p.Properties["FirstName"]

Wonder why?
Avatar of fwsteal

ASKER

Got it:

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                System.Web.Profile.ProfileBase p = HttpContext.Current.Profile;

                if (p != null)
                {
                    //firstname                    
                    if (p.GetPropertyValue("FirstName") != null)
                    {
                        if (p.GetPropertyValue("FirstName").ToString() == null || p.GetPropertyValue("FirstName").ToString() == "")
                        {
                            lblFirstName.Text = "*FirstName is null or is empty*";
                        }
                        else
                        {
                            lblFirstName.Text = p.GetPropertyValue("FirstName").ToString();
                        }
                    }

                    //lastname
                    if (p.GetPropertyValue("LastName") != null)
                    {
                        if (p.GetPropertyValue("LastName").ToString() == null || p.GetPropertyValue("LastName").ToString() == "")
                        {
                            lblLastName.Text = " *LastName is null or is empty* ";
                        }
                        else
                        {
                            lblLastName.Text = p.GetPropertyValue("LastName").ToString();
                        }
                    }

                    //email
                    if (p.GetPropertyValue("Email") != null)
                    {
                        if (p.GetPropertyValue("Email").ToString() == null || p.GetPropertyValue("Email").ToString() == "")
                        {
                            lblEmail.Text = " *Email is null or is empty* ";
                        }
                        else
                        {
                            lblEmail.Text = p.GetPropertyValue("Email").ToString();
                        }
                    }

                    //job title
                    if (p.GetPropertyValue("JobTitle") != null)
                    {
                        if (p.GetPropertyValue("JobTitle").ToString() == null || p.GetPropertyValue("JobTitle").ToString() == "")
                        {
                            lblJobTitle.Text = " *JobTitle is null or is empty* ";
                        }
                        else
                        {
                            lblJobTitle.Text = p.GetPropertyValue("JobTitle").ToString();
                        }
                    }

                    //customer id
                    if (p.GetPropertyValue("CustomerID") != null)
                    {
                        if (p.GetPropertyValue("CustomerID").ToString() == null || p.GetPropertyValue("CustomerID").ToString() == "")
                        {
                            lblCustomerID.Text = " *CustomerID is null or is empty* ";
                        }
                        else
                        {
                            lblCustomerID.Text = p.GetPropertyValue("CustomerID").ToString();
                        }
                    }

                    //account name
                    if (p.GetPropertyValue("AccountName") != null)
                    {
                        if (p.GetPropertyValue("AccountName").ToString() == null || p.GetPropertyValue("AccountName").ToString() == "")
                        {
                            lblAccountName.Text = " - *AccountName is null or is empty* ";
                        }
                        else
                        {
                            lblAccountName.Text = p.GetPropertyValue("CustomerID").ToString();
                        }
                    }

                    //account type
                    if (p.GetPropertyValue("AccountType") != null)
                    {
                        if (p.GetPropertyValue("AccountType").ToString() == null || p.GetPropertyValue("AccountType").ToString() == "")
                        {
                            lblAccountType.Text = "(*AccountType is null or is empty*)";
                        }
                        else
                            if (p.GetPropertyValue("AccountType") == "Parent")
                            {
                                lblAccountType.Text = "(Parent Account)";
                                lkbtnChangeAccount.Text = "Change Account";
                                lkbtnChangeAccount.PostBackUrl = "s/sp/changeaccount.aspx";
                            }
                            else
                            {
                                lblAccountType.Visible = false;
                                lkbtnChangeAccount.Visible = false;
                            }
                    }
                }
            }
            catch(Exception ex)
            {
                lblError.Text="Message:" + ex.Message.ToString();
            }
        }

ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial