从方法获取空引用异常

时间:2017-03-29 05:40:54

标签: asp.net asp.net-mvc ado.net

我正在使用SQLHELPER类从过程中获取id。 我写了以下代码:

string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
public int CheckUser(string userName,string password)
    {
        SqlParameter[] spc = new SqlParameter[5];
        spc = SqlHelperParameterCache.GetSpParameterSet(constr, "CheckUser");
        spc[0].Value = userName;
        spc[1].Value =password;
        spc[2] = new SqlParameter("@userExist", SqlDbType.Int, 32);
        spc[2].Direction = ParameterDirection.Output;
        int res = (int)SqlHelper.ExecuteScalar(constr, CommandType.StoredProcedure, "CheckUser", spc);

        return res;
    }

我正在使用用户名和密码,但我正面临“用户未处理Null引用异常”运行时异常。当我调试时,我得到了我发送的两个值。

Create proc CheckUser
@userName varchar(50),
@password varchar(50),
@userExist int output
as
  begin
   set @userExist= (select COUNT(*) 
                from tbluserInfo 
                where  UserName=@userName and [Password]=@password)

    select [uid] from tbluserInfo 
            where  UserName=@userName and [Password]=@password

 end

Null Ref QuickWatch. Click here for image

1 个答案:

答案 0 :(得分:0)

您必须返回从SP设置的@userExist,请在下方更新SP

<强>已更新

Create proc CheckUser
@userName varchar(50),
@password varchar(50),
@userExist int output
as
  begin
   set @userExist= (select isnull([uid],0) from tbluserInfo 
            where  UserName=@userName and [Password]=@password)

   return @userExist;

 end