0

I'm using SQLHELPER classes for getting the id from the procedure. I had written the following code :

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

I'm pssing the username and password but I'm facing "Null reference exception was unhandled by the user" run time exception. When I debug I got the both values which I sent exactly.

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 Answers1

0

You have to return @userExist which you have set from SP please find below updated SP

UPDATED

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
Curiousdev
  • 5,668
  • 3
  • 24
  • 38
  • returned the output parameter, still facing the same issue. I'm getting null reference exception not sqlexception. Please think in that way also. – Srikanth Reddy Mar 29 '17 at 06:16
  • PLease put a debug point and than quick watch on `SqlHelper.ExecuteScalar(constr, CommandType.StoredProcedure, "CheckUser", spc)` this line what you'r getting check if debug reached at this line or not if not which line you'r getting exception – Curiousdev Mar 29 '17 at 06:20
  • I'm getting Null in quickwatch. Please check the image I had attached below the Quest. – Srikanth Reddy Mar 29 '17 at 06:27
  • Ohh that's weird can you show the `SqlHelper.ExecuteScalar` method? – Curiousdev Mar 29 '17 at 06:55