0

Been through my textbook, but I'm still having problems with catching an inner exception as I try to add a record to the database. What is the proper syntax for catching an InnerException in my class?

The inner expection is being thrown on the db.SaveChanges. Here is the code in my 'addReviews.cs' class file to add the record:

  public void addReview(int restId, int timeID, string dateValue, string describe, int cuisineId, int serviceId, int ambianceId, string memberId)
    {
        REVIEW addReview = new REVIEW();
        addReview.REVIEW_DATE = Convert.ToDateTime(dateValue);
        addReview.REVIEW_DATE_SUBMITTED = DateTime.Today;
        addReview.REVIEW_DESC = describe;
        addReview.TIME_ID = timeID;
        addReview.REVIEWER_ID = 1;
        addReview.FOOD_ID = cuisineId;
        addReview.SERVCE_ID = serviceId;
        addReview.AMBIENCE_ID = ambianceId;
        addReview.REST_ID = restId;
        addReview.PRICE_ID = 1;

        //Add record to database
        db.AddToREVIEWs(addReview);
        db.SaveChanges();
    }

Thanks in advance!

sll
  • 61,540
  • 22
  • 104
  • 156
Susan
  • 1,822
  • 8
  • 47
  • 69

1 Answers1

1

Try this out:

try
{
  db.SaveChanges();
}
catch(ApplicationException exception)
{
   // just log and throw out
   System.Diagnostics.Debug.WriteLine(
              String.Concat(
                   "EXCEPTION: ", 
                   exception.ToString));

   if (exception.InnerException != null)
   {
      System.Diagnostics.Debug.WriteLine(
               String.Concat(
                  "INNER EXCEPTION: ", 
                  exception.InnerException.ToString));
   }

   throw;
}

PS: try to catch more specific exception, in your case it worth to catch SqlException as well.

sll
  • 61,540
  • 22
  • 104
  • 156
  • OMG ... I had made a change to my db and forgot to update my entity model. Not too swift. Thanks so much for the help as it will come in handy and I continue on this project. Regards!!! – Susan Aug 15 '11 at 15:07