Mahesh Reddy

Mahesh Reddy

  • 1.7k
  • 337
  • 9.7k

Display(Return) validation message in catch block

Jan 22 2019 10:52 PM
Hi,
In controller codei have used try and catch blocks 
coming to my requirement 
I have one model in that i have 2 fields named as Name,Code
In formi have entered  only Name 
after clicking of submitbutton it was throwing error 
everything is ok 
problem is that instead of displaying a cutsom error message in catch i need to display what type of exception raised in database
 
Could please kindly any one help me
Note:ifi can fill all the fields at that time it was saving to database properly 
In catch block instead of displaying something went wrong display what type of exception we raised.
Code
............ 
try
{
State state = new State();
state.Name = Name;
state.Code = st.Code;
db.State.Add(state);
db.SaveChanges();
dbContextTransaction.Commit();
}
catch (Exception)
{
dbContextTransaction.Rollback();
return "Something went wrong";
}
 

Answers (5)