Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:53:56+00:00 2026-06-05T15:53:56+00:00

I’m trying to write code in my controller that when run, will create a

  • 0

I’m trying to write code in my controller that when run, will create a bunch of variables and then send an email using a template that has placeholders to include the variables. However it’s assigning information from a user account to the variables and if the user hasn’t filled in that information yet it will crash. As a work around I declared them inside if statements that checked to make sure they weren’t null first. But now when I try to pass them to the email template it tells me the variable doesn’t exist. If I remove the if statement it works again. Any ideas why?

Code is below…

if (UserContext != null && UserContext.Account != null)
        {

            if (UserContext.UserName != null)
            { string UserName = string.Format(UserContext.UserName); }

            if (UserContext.EmailAddress != null)
            { string EmailAddress = string.Format(UserContext.EmailAddress); }

            if (UserContext.Account.PrimaryContactFirstName != null)
            { string FirstName = string.Format(UserContext.Account.PrimaryContactFirstName); }

            if (UserContext.Account.PrimaryContactLastName != null)
            { string LastName = string.Format(UserContext.Account.PrimaryContactLastName); }

            if (UserContext.Account.PrimaryContactPhoneNumber != null)
            { string PhoneNumber = string.Format(UserContext.Account.PrimaryContactPhoneNumber); }

            if (UserContext.Account.HeadquartersAddressLine1 != null)
            {
                string AddressLine1 = string.Format(UserContext.Account.HeadquartersAddressLine1);

                if (UserContext.Account.HeadquartersAddressLine2 != null)
                { string AddressLine2 = string.Format(UserContext.Account.HeadquartersAddressLine2); }
            }

            if (UserContext.Account.HeadquartersCity != null)
            { string City = string.Format(UserContext.Account.HeadquartersCity); }

            if (UserContext.Account.HeadquartersState != null)
            { string State = string.Format(UserContext.Account.HeadquartersState); }

            if (UserContext.Account.HeadquartersZip != null)
            { string ZipCode = string.Format(UserContext.Account.HeadquartersZip); }

            if (UserContext.Account.Name != null)
            { string Name = string.Format(UserContext.Account.Name); }

            string body = string.Format(Resources.ContactUsLoggedInEmailTemplate, model.FirstName, model.LastName, model.PhoneNumber, model.Email, model.ReasonForContact, model.Message, UserName, EmailAddress, FirstName, LastName, PhoneNumber, AddressLine1, AddressLine2, City, State, ZipCode, Name);

            string subject = string.Format("Web Submit: {0}", model.ReasonForContact);

            EmailHelper.SendEmail("support@website.com", subject, body, true);

        }
        if (UserContext == null)
        {
            string body = string.Format(Resources.ContactUsEmailTemplate, model.FirstName, model.LastName, model.PhoneNumber, model.Email, model.ReasonForContact, model.Message);

            string subject = string.Format("Web Submit: {0}", model.ReasonForContact);

            EmailHelper.SendEmail("support@website.com", subject, body, true);
        }

        return RedirectToAction("ContactConfirmation");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:53:57+00:00Added an answer on June 5, 2026 at 3:53 pm

    Each variable only exists for the lifetime of the block in which it is created. You have them vanishing instantly as each if block ends. The solution is to declare all of the variables first, then assign values as needed.

    This is one of the common annoyances of try/catch blocks. Anything that might be useful later needs to be declared before the try. If a constructor might throw an exception then it should be inside the try block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.