这是我遇到的错误,这是令人困惑的,因为它只是前一段时间。我不小心点击了一些按钮,这发生了。我正在创建一个"删除"我正在做的一个简单系统的功能......
输出另一个错误说:
"参数字典包含参数的空条目 '用户id'非可空类型&Systems.Int32'
这是我的代码:
UserController.cs
public ActionResult DeleteUser(int id)
{
UserBusinessLayer userService = new UserBusinessLayer("sqlconn");
UserModel userModel = new UserModel();
try
{
UserEntity userEntity = userService.GetUserId(id);
userModel.usr_Id = userEntity.usr_Id;
userModel.email = userEntity.email;
userModel.username = userEntity.username;
userModel.password = userEntity.password;
}
catch (Exception ex)
{
throw ex;
}
return View(userModel);
}
[HttpPost, ActionName("DeleteUser")]
public ActionResult DeleteUserConfirmed(int id)
{
UserBusinessLayer userService = new UserBusinessLayer("sqlconn");
userService.DeleteUser(new UserEntity(){
usr_Id = id
});
return RedirectToAction("List");
}
UserBusinessLayer.cs
public UserEntity DeleteUser(int userId)
{
UsersDataAccess userService = new UsersDataAccess(_key);
try
{
return userService.DeleteUser(userId);
}
catch (Exception exx)
{
throw exx;
}
}
UserDataAccess.cs
public UserEntity DeleteUser(int userId) {
SqlConnection oCon = null;
SqlCommand oCmd = null;
UserEntity user = null;
try
{
oCon = _connection.openConn();
string sqlDelete = string.Format("DELETE FROM Users WHERE usr_id = @userId");
oCmd = new SqlCommand(sqlDelete, oCon);
oCmd.Parameters.Add(new SqlParameter("@userId", user.usr_Id));
oCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
return user;
}