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");
Each variable only exists for the lifetime of the block in which it is created. You have them vanishing instantly as each
ifblock 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/catchblocks. Anything that might be useful later needs to be declared before thetry. If a constructor might throw an exception then it should be inside thetryblock.