-1

so in the following code I should be able to connect to a database, and then output that database to a datagrid for the users to view. However what is actually happening is that once the code hits the Load method, it throws a null error. I'm not sure why, and I would be grateful if you guys could explain why I'm not pulling data correctly... The line in question is this:

List<Users> _users = UserRepository.GetUsers();

My code goes as such:

DBDisplay.xaml.cs

public partial class DBDisplay : Window
{
    public DBDisplay()
    {
        InitializeComponent();
        var viewModel = new DbDisplayViewModel();
        viewModel.UserRepository = new UserRepository();
        this.DataContext = viewModel;
    }
}

DBDisplayViewModel.cs

public class DbDisplayViewModel
{
    private ObservableCollection<Users> _users;

    public IUserRepository UserRepository{ get; set; }

    public DbDisplayViewModel()
    {
        Load();
    }



    public ObservableCollection<Users> Users
    {
        get
        {
            if (_users == null)
            {
                _users = new ObservableCollection<Users>();
            }

            return _users;
        }
        set
        {
            if (value != null)
            {
                _users = value;
            }
        }
    }

    private void Load()
    {
        List<Users> _users = UserRepository.GetUsers(); // THIS IS THE BROKEN LINE THAT THROWS A NULL ERROR
        Users = new ObservableCollection<Users>(_users);
    }
}

UserRepository.cs

    public class UserRepository : IUserRepository
{
    private string connectionStr;

    public System.Data.SqlClient.SqlConnection connectionToDB;

    private System.Data.SqlClient.SqlDataAdapter dataAdapter;

    public void openConnection()
    {
        // create the connection to the database as an instance of System.Data.SqlClient.SqlConnection
        connectionToDB = new System.Data.SqlClient.SqlConnection(connectionStr);

        //open the connection
        connectionToDB.Open();
    }


    // Get the data set generated by the sqlStatement
    public System.Data.DataSet getDataSet(string sqlStatement)
    {
       dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlStatement, connectionToDB);

        // create the dataset
        var dataSet = new System.Data.DataSet();

        dataAdapter.Fill(dataSet);

        //return the dataSet
        return dataSet;
    }
    public List<Users> GetUsers()
    {
        connectionToDB = new System.Data.SqlClient.SqlConnection(connectionStr);
        connectionToDB.Open();
        List<Users> userlist = new List<Users>();
        DataSet ds = getDataSet("Select UserID, FirstName, LastName from UserData");
        Users users;

        foreach (DataRow row in ds.Tables[0].Rows)
        {
            users = new Users
            {
                UserID = row["UserID"].ToString(),
                FirstName = row["FirstName"].ToString(),
                LastName = row["LastName"].ToString()
            };


            userlist.Add(users);
        }

        return userlist;

    }


}

Users.cs just has some properties IUserRepository has a get set method for User Repository

Badja
  • 840
  • 1
  • 6
  • 29

1 Answers1

0

You are calling Load from the constructor. At that time, the UserRepository is still null (you set it in the call after the constructor).

Provide the repository to the constructor and set it from there.

public DbDisplayViewModel(IUserRepository repository)
{
    this.UserRepository = repository;

    this.Load();
}

Required reading: What is a NullReferenceException, and how do I fix it?

Community
  • 1
  • 1
Patrick Hofman
  • 143,714
  • 19
  • 222
  • 294