ASP.net InnerException: add record to DB
-
14-12-2020 - |
Pregunta
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!
Solución
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.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow