Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public class ProductModel
{
   public string InsertProduct(Product product)
   {
      try
      {
         GarageEntities db = new GarageEntities();
         db.Products.Add(product);
         db.SaveChanges();

         return product.Name + "was succesfully inserted";
      }
      catch (Exception e)
      {
         return "Error:" + e;
      }
   }
Posted
Updated 15-Dec-15 22:08pm
v2
Comments
Andy Lanng 16-Dec-15 4:09am    
I just tried your code and it's fine. Maybe VS has bugged out. Try restarting VS
Leo Chapiro 16-Dec-15 4:21am    
Marked as "Unclear/Incomplete"
Jochen Arndt 16-Dec-15 4:50am    
"not all code paths return a value" is a compiler warning message.
So badly formulated but not unclear.

1 solution

The compiler complains about a missing return statement at the end of the function (he is not smart enough to detect that the end is never reached). You have three options:

  • Ignore or suppress the warning (nonprofessional).
  • Return the result at the end (move the return from the try block to the end).
  • Return something at the end.

For the third case with comments for the second case:
C#
public string InsertProduct(Product product)
{
   try
   {
      GarageEntities db = new GarageEntities();
      db.Products.Add(product);
      db.SaveChanges();
      // May be moved to end of function
      return product.Name + "was succesfully inserted";
   }
   catch (Exception e)
   {
      return "Error:" + e;
   }
   // Make the compiler happy or return success here
   return "";
}


[EDIT]
A tip for asking about such problems:
Always include the compiler code (CS0161 here) because this makes it clear what happened. However, searching the web for the message and/or code would have answered this too.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900