Articles → ASP .NET MVC → Internal server error (500) in MVC

Internal server error (500) in MVC






Create a MVC project and database 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();
        }





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





Click to Enlarge




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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250