{“参数化查询”(@Usr_Name nvarchar(4000).... nv'需要参数'@Usr_Name',这是未提供的。“}

时间:2017-07-13 08:35:20

标签: c# asp.net-mvc

我在这里获得此异常是我的用于在db中创建用户详细信息的插入代码。我想尽力解决但不能这样做

renderProductsList(){
    return this.state.productsList.map((product)=>{
        return (
            <div key={product._id}>
                <h4>{product.itemName}</h4>
                <p>{product.itemDesc}</p>
                <img src={product.itemImgUrl}/>
                <p>${product.price}</p>
                <button onClick={this.addToCart.bind(this)}>Add to Cart</button>
            </div>
        );
    });
}

1 个答案:

答案 0 :(得分:-1)

Comment :  // Problem in your query. Please write Actual DB Field Name of 
your SQL in below line.

public ActionResult Create(UserModel usr)
{
    con.Open();
    Comment :  // Problem in your query. Please write Actual DB Field Name 
                  of your SQL in below line.

 string query = "Insert into Tbl_User 
(Field1,Field2,Field3,Field4,Field5,Field6,Field7) 
Values(@Usr_Name,@FirstName,@LastName,@Gender,@Phone,@Address,@Email)";
     SqlCommand cmd = new SqlCommand(query, con);
     SqlParameter usrname = cmd.Parameters.AddWithValue("@Usr_Name", 
     usr.Usr_Name);
if (usr.Usr_Name == null)
{
    usrname.Value = DBNull.Value;
}
SqlParameter fname = cmd.Parameters.AddWithValue("@FirstName", usr.First_Name);
if (usr.First_Name == null)
{
    fname.Value = DBNull.Value;
}
cmd.Parameters.AddWithValue("@LastName", usr.Last_Name);
cmd.Parameters.AddWithValue("@Gender", usr.Gender);
cmd.Parameters.AddWithValue("@Phone", usr.Phone);
cmd.Parameters.AddWithValue("@Address", usr.Address);
cmd.Parameters.AddWithValue("@Email", usr.Email);
cmd.ExecuteNonQuery();

return RedirectToAction("Index");
 }