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

时间:2022-12-06 23:11:06

i'm trying to populate textbox value based on another textbox but i cannot populate the the other textbox. i'm sharing my code please guide me with best 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 Throwing the error like:Conversion failed when converting the nvarchar value '' to data type int.

当将nvarchar值“转换为数据类型int”时,它会抛出如下错误:转换失败。

2 个解决方案

#1


2  

First of all your error is in this line:-

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

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

Since you are trying to pass an integer value to NVARCHAR column, Please change your code like this:-

由于您正在尝试将一个整数值传递给NVARCHAR列,请更改您的代码如下:-

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

Please read this:- Can we stop using AddWithValue

请阅读以下内容:-我们可以停止使用AddWithValue吗

Now, once this is fixed, change your jQuery code from sucess to success and it should work!

现在,一旦解决了这个问题,将jQuery代码从成功更改为成功,它应该可以工作了!

Apart from this, use using statement to automatically dispose your valuables resources something like this:-

除此之外,使用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);
}

#2


2  

You're passing a string to Parameters.AddWithValue method, but int is expected. Convert id variable to int.

将字符串传递给参数。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 );

EDIT

编辑

Here is complete code you may copy/paste

这里是完整的代码,您可以复制/粘贴

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

But there is a better solution if you expect integer id in AgreementNo(string id) method.

但是如果您希望在AgreementNo(string id)方法中使用整型id,则有更好的解决方案。

Just change parameter type to int:

只需将参数类型更改为int:

public JsonResult AgreementNo(int id)

#1


2  

First of all your error is in this line:-

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

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

Since you are trying to pass an integer value to NVARCHAR column, Please change your code like this:-

由于您正在尝试将一个整数值传递给NVARCHAR列,请更改您的代码如下:-

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

Please read this:- Can we stop using AddWithValue

请阅读以下内容:-我们可以停止使用AddWithValue吗

Now, once this is fixed, change your jQuery code from sucess to success and it should work!

现在,一旦解决了这个问题,将jQuery代码从成功更改为成功,它应该可以工作了!

Apart from this, use using statement to automatically dispose your valuables resources something like this:-

除此之外,使用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);
}

#2


2  

You're passing a string to Parameters.AddWithValue method, but int is expected. Convert id variable to int.

将字符串传递给参数。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 );

EDIT

编辑

Here is complete code you may copy/paste

这里是完整的代码,您可以复制/粘贴

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

But there is a better solution if you expect integer id in AgreementNo(string id) method.

但是如果您希望在AgreementNo(string id)方法中使用整型id,则有更好的解决方案。

Just change parameter type to int:

只需将参数类型更改为int:

public JsonResult AgreementNo(int id)