Articles → ASP .NET MVC → Internal Server Error (500) In MVC

Internal Server Error (500) In MVC






Create A MVC Project And Database Table




Picture showing the design view of the sample table
Click to Enlarge


Create A Button On Index




@{
    ViewBag.Title = "Home Page";
}

<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script><script type="text/javascript">
    $(document).ready(
               function () {
                   $("a").click(
                       function () {
                           $.ajax({
                               type: "POST",
                               url: "@Url.Action("Insert", "Home")",
                               success: function (data) {
                                   alert("sucess");
                               },
                               error: function (err) {
                                   alert("error");
                               }
                           });
                       });
               });
</script><a href="#" id="lnkClick">Click</a>




public ActionResult Insert() {
  using(SqlConnection connection = new SqlConnection(“conn string”)) {
    using(SqlCommand command = new SqlCommand("Insert into Employee(Employee_name) values('Employee 1')", connection)) {
      connection.Open();
      command.ExecuteNonQuery();
    }
  }
  return View();
}




Picture showing the error message when the application is deployed on the server
Click to Enlarge




Global.Asax Comes For Rescue




protected void Application_EndRequest() {
  Exception[] ex = this.Context.AllErrors;
  if (ex != null)
    System.IO.File.WriteAllText(@ "c:\test\exception.txt", ex[0].Message);
}




Picture showing the actual error in the text file for the internal server error 500
Click to Enlarge




Posted By  -  Karan Gupta
 
Posted On  -  Sunday, December 6, 2015

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250