Click here to Skip to main content
15,947,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am a little bit stuck for some reason something I have changed/deleted in my code has caused my login to crash and keep throwing the NullReferenceException. Normally I can find the cause of this kind of exception but for some reason this one is just escaping me!

This is my code behind on my log in:
protected void Login_Click(object sender, EventArgs e)
    {

        List<User> user1 = new List<User>();

        UserBLL UserDb = new UserBLL();
        User user = new User();
        user.UserName = UserNametxt.Text;
        user.Password = Passwordtxt.Text;
        user1 = UserDb.GetSalt(user);

        string Checkhash = UserDb.CreatePasswordHash(user.Password, user1[0].Salt.ToString());
        string Originalhash = user1[0].Password.ToString();

        if (Checkhash == Originalhash)
        {
            UserBLL UserBLL = new UserBLL();
            List<User> user3 = new List<User>();
            user3 = UserBLL.GetUserID(user);
            user.UserID = Convert.ToInt16(user3[0].UserID.ToString());

            Session["User"] = user.UserName;
            Session["UserID"] = user.UserID;
            Response.Redirect("~/MyProfile.aspx", false);
        }
        else
        {
            Errormsg.Text = "Your UserName or Password is incorrect";
        }

    }

This is where the error is occurring:
public List<User> GetSalt(User user)
       {
           return UserDb.GetSalt(user);
       }


This is the user class just for extra info:
namespace Entities
{
    public class User
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public string Role { get; set; }
        public string Salt { get; set; }
    }
}


If anyone could point out my error I would appreciate it.

Thanks

Dan
Posted

Amazingly(maybe not), the problem was in the BLL:

public List<user> GetSalt(User user)
       {
           return UserDb.GetSalt(user);
       }</user>


at the top of the class I had defined UserDb as:

UserDAL UserDb;


and it wanted:

UserDAL UserDb = new UserDAL();


If anyone could tell me what the difference is it would be nice to know.

Thanks
 
Share this answer
 
Check how the UserDb instance inside the method GetSalt is assigned or created.

The reason for Null Reference exception is no instance created for the variable UserDb and you are trying to access that variable inside the GetSalt method.

It's worth to note that variable UserDb you have defined must be different from UserDb inside the method.

First of all GetSalt is a method of UserBLL which again calls another method GetSalt of UserDb and not UserBLL.

check these two classes closely. or Add your code here for better answer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900