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