I want to return a view if an object(country) is not null. But I get the error "Not All code paths return a value"
My code looks like this
public ActionResult Show(int id)
{
if (id != null)
{
var CountryId = new SqlParameter("@CountryId", id);
Country country = MyRepository.Get<Country>("Select * from country where CountryId=@CountryId", CountryId);
if (country != null)
{
return View(country);
}
}
}
This happens when you are returning something from within the "if" statement. The compiler thinks, what if the "if" condition is false? That way you are not returning anything even when you have the return type of "ActionResult" defined in the function. So add some default returns in the else statement:
public ActionResult Show(int id)
{
if (id != null)
{
var CountryId = new SqlParameter("@CountryId", id);
Country country = MyRepository.Get<Country>("Select * from country where CountryId=@CountryId", CountryId);
if (country != null)
{
return View(country);
}
else
{
return View(something);
}
}
else
{
return View(something);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With