I am trying to fill in the value of a text field based on another text field, but I cannot fill in another text field. I am sharing my code, please help me with a better solution
Action method:
public JsonResult AgreementNo(string id) { string no; string _str = id; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()); SqlCommand cmd = new SqlCommand("SELECT top(1) num from loan where id=@str ", con); cmd.Parameters.AddWithValue("@str",id); cmd.CommandType = CommandType.Text; DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); no = ds.Tables[0].Rows[0]["num"].ToString(); return Json(new { no = no }, JsonRequestBehavior.AllowGet); }
Script:
$("#BarrowerName").blur(function () { $.ajax({ url: '@Url.Action("AgreementNo", "Home")', // url: '@Url.Action("AgreementNo", "Home")', dataType: "json", data: JSON.stringify({ id: $("#BarrowerName").val() }), type:"POST", async: false, contentType: 'application/json,charset=utf-8', sucess: function (data) { $("#AgreementNo").val(data.no) response(data); } }); });
It throws an error, for example: Conversion error when converting nvarchar '' value to int data type.
source share