ASP.net InnerException:向DB添加记录

时间:2011-08-14 22:29:25

标签: .net asp.net database savechanges inner-exception

通过我的教科书,但是当我尝试将记录添加到数据库时,我仍然遇到捕获内部异常的问题。在我的类中捕获InnerException的正确语法是什么?

db.SaveChanges 正在引发内部考验。这是我的'addReviews.cs'类文件中添加记录的代码:

  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();
    }

提前致谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

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:尝试捕获更具体的异常,在您的情况下也值得抓住SqlException