The alert is not being displayed. The textbox displays the json string after a existing entry in the database is entered therefore the jQuery is functioning. The "$.ajax is going directly to the error code so no display of the alert.
How do you fix a : "null entry for parameter 'id' of non-nullable type 'System.Int32'" Error?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script type="text/javascript">$(function () {$("#btn1").click(function () {
var id1 = $("#text1").val();
var url1 = "Home/fn1";
var data1 = { "id": id1 };
var json1 = JSON.stringify(data1);$("#jsondata").val(json1);$.ajax({
type: "POST",
dataType: "json",
url: 'Home/fn1',
data: json1,
success: function (data) {
alert("success")
},
error: function (e) {
alert(e.responseText);
}
});
});
});</script><center><table><tr><td><br /><br /><br /><br /></td></tr><tr><td>Input Id:</td><td>@Html.TextBox("text1")</td></tr><tr><td colspan="2"><input id="btn1" type="submit" value="Post Ajax" /></td></tr><tr><td>Json String:</td><td>@Html.TextBox("jsondata")</td></tr></table></center>[HttpPost]
public JsonResult fn1(int id)
{
List<Table1> the = new List<Table1>();
string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT [col1],[col2],[col3] FROM Table1 WHERE [id] =" + id + ";", conn))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
the.Add(new Table1 { col1 = dr["col1"].ToString(), col2 = dr["col2"].ToString(), col3 = dr["col3"].ToString() });
}
}
}
}
return Json(the, JsonRequestBehavior.AllowGet);
} Error code:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult.fn1(Int32)' in 'AJAXMethod.Controller.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.