user4456130 user4456130 - 2 months ago 6
C# Question

Not all code paths return a value. I was just wondering what was the fix to this?

When I hover over the Index it has a red line underneath and says


'UnauthEnquiryController.index(UnauthenticatedEnquiryViewModel)': not all code paths return a value.


I was just wondering what was the fix to this?

[HttpPost]
public ActionResult Index(UnauthenticatedEnquiryViewModel unauthenticatedEnquiryViewModel)
{
//unauthenticatedEnquiryViewModel.EnquiryType;
//NEED TO ADD A METHOD THAT SENDS FILE TO CRM HERE
if (ModelState.IsValid)
{
if (1 == 0) //take off
{
string fromAddress = WebConfigurationManager.AppSettings["FromEmail"]; //gets email from web.config file
//var toAddress = new MailAddress(); //need to get this from crm

var enquiry = DataAccessEnquiry.GetEnquiryCategoryEmail(unauthenticatedEnquiryViewModel.EnquiryType); //gets the to address based on CRm

string UnauthEmailSubject = WebConfigurationManager.AppSettings["UnauthEmailSubject"]; //gets subject from web.config file

MailMessage mailMessage = new MailMessage(fromAddress, enquiry.Email); //put to address frrom variable declared above

mailMessage.Subject = UnauthEmailSubject;

StringBuilder mailbuilder = new StringBuilder();
mailbuilder.AppendLine("First name: " + unauthenticatedEnquiryViewModel.FirstName);
mailbuilder.AppendLine("Last name: " + unauthenticatedEnquiryViewModel.LastName);
mailbuilder.AppendLine("Communication: " + unauthenticatedEnquiryViewModel.CCommmunication);
mailbuilder.AppendLine("Email: " + unauthenticatedEnquiryViewModel.Email);
mailbuilder.AppendLine("Confiirmation of email: " + unauthenticatedEnquiryViewModel.ConfirmEmailAddress);
mailbuilder.AppendLine("Mobile telephone No: " + unauthenticatedEnquiryViewModel.MobileTelephoneNo);
mailbuilder.AppendLine("Confiirmation of mobile telephone no: " + unauthenticatedEnquiryViewModel.ConfirmMobileTelephoneNo);
mailbuilder.AppendLine("Alternative telephone no: " + unauthenticatedEnquiryViewModel.AlternativeTelephoneNo);
mailbuilder.AppendLine("Confiirmation of alternative telephone no: " + unauthenticatedEnquiryViewModel.ConfirmAlternativeTelephoneNo);
mailbuilder.AppendLine("I am a: " + unauthenticatedEnquiryViewModel.Profession);
mailbuilder.AppendLine("Enquiry Type: " + unauthenticatedEnquiryViewModel.EnquiryType);
mailbuilder.AppendLine("Your message: " + unauthenticatedEnquiryViewModel.YourMessage);


if (unauthenticatedEnquiryViewModel.File != null) // this finds overall null
{
foreach (var file in unauthenticatedEnquiryViewModel.File) // loop through every File
{
if (file != null) //Finds induvidual null
{
var extension = new FileInfo(file.FileName).Extension.ToUpper();
mailMessage.Attachments.Add(new Attachment(file.InputStream, file.FileName));
}
}
}

mailMessage.Body = mailbuilder.ToString();

SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
}

return View("Unauthsuccess", unauthenticatedEnquiryViewModel);
}

unauthenticatedEnquiryViewModel.Professions = DataAccessEnquiry.GetProfessionUnauthenticated();
unauthenticatedEnquiryViewModel.EnquiryTypes = new List<EnquiryType>();
}

BWA BWA
Answer
[HttpPost]
public ActionResult Index(UnauthenticatedEnquiryViewModel unauthenticatedEnquiryViewModel)
{
    //unauthenticatedEnquiryViewModel.EnquiryType;
    //NEED TO ADD A METHOD THAT SENDS FILE TO CRM HERE
    if (ModelState.IsValid)
    {
        ...

        return View("Unauthsuccess", unauthenticatedEnquiryViewModel);
    }

    unauthenticatedEnquiryViewModel.Professions = DataAccessEnquiry.GetProfessionUnauthenticated();
    unauthenticatedEnquiryViewModel.EnquiryTypes = new List<EnquiryType>();

    //THIS path doesn't return value.
    //HERE return somethig
}

You return value only if if (ModelState.IsValid) is true but what want you return if this is false?

Comments