根据数据库中的另一个文本框填充文本框值

时间:2014-12-25 05:46:00

标签: c# jquery asp.net asp.net-mvc

我正在尝试根据另一个文本框填充文本框值,但我无法填充其他文本框。我正在分享我的代码,请指导我提供最佳解决方案

行动方法:

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

脚本:

 $("#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);
}
});                           
});

它抛出错误:将nvarchar值''转换为数据类型int时转换失败。

2 个答案:

答案 0 :(得分:2)

您正在传递stringParameters.AddWithValue方法,但预计会int。将id变量转换为int

int intID = int.Parse(id);

SqlCommand cmd = new SqlCommand("SELECT top(1) num from loan where id=@str", con);
cmd.Parameters.AddWithValue("@str", intID );

修改

以下是您可以复制/粘贴的完整代码

public JsonResult AgreementNo(string id)
{
    string no;
    int intId = int.Parse(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", intId);
    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);
    }
}

但是如果你期望AgreementNo(string id)方法中的整数id,那么有一个更好的解决方案。

只需将参数类型更改为int

public JsonResult AgreementNo(int id)

答案 1 :(得分:2)

首先,你的错误就在这一行: -

cmd.Parameters.AddWithValue("@str",id);

由于您尝试将整数值传递到NVARCHAR列,请更改您的代码: -

cmd.Parameters.Parameters.Add("@str",SqlDbType.NVarChar).Value = id;

请阅读: - Can we stop using AddWithValue

现在,修复此问题后,将jQuery代码从sucess更改为success,它应该可以正常工作!

除此之外,使用using语句自动处理贵重物品资源,如下所示: -

string CS = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using(SqlConnection con = new SqlConnection(CS))
using(SqlCommand cmd = new SqlCommand("SELECT top(1) num from loan where id=@str", con))
{
    cmd.Parameters.Parameters.Add("@str",SqlDbType.NVarChar).Value = 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);
}