0

Im trying to fill a listview with data from my database but get a null reference exception when i load the listview. Im using MVC pattern.

Code in my data acces layer:

public List<Menu_Item> DB_Get_All_MenuItems()
{
    string query = "SELECT * FROM [Menu_Items]";
    SqlParameter[] sqlParameters = new SqlParameter[0];
    return ReadTables(ExecuteSelectQuery(query, sqlParameters));
}

private List<Menu_Item> ReadTables(DataTable dataTable)
{
    List<Menu_Item> menu_items = new List<Menu_Item>();
    foreach (DataRow dr in dataTable.Rows)
    {
        Menu_Item menu_Item = new Menu_Item()
        {
            Menu_ID = (int)dr["Menu_ID"],
            Naam = (string)dr["Naam"],
            Prijs = (float)dr["Prijs"],
            Voorraad = (int)dr["Voorraad"],
            Categorie_ID = (int)dr["Catogorie_ID"]

        };
        menu_items.Add(menu_Item);
    }
    return menu_items;
}

I have checked and my SQL parameters are all spelled correctly

The exact error is: datatable.rows = 'datatable.rows' threw an exception of type 'system.nullreferenceexception'

My DateTable method looks like this

protected DataTable ExecuteSelectQuery(string query, params SqlParameter[] sqlParameters)
        {
            SqlCommand command = new SqlCommand();
            DataTable dataTable;
            DataSet dataSet = new DataSet();

            try
            {
                command.Connection = OpenConnection();
                command.CommandText = query;
                command.Parameters.AddRange(sqlParameters);
                command.ExecuteNonQuery();
                adapter.SelectCommand = command;
                adapter.Fill(dataSet);
                dataTable = dataSet.Tables[0];
            }
            catch (SqlException e)
            {
                return null;
                throw new Exception(e.ToString());
            }
            finally
            {
                CloseConnection();
            }
            return dataTable;
        }
  • 2
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Crowcoder Dec 11 '19 at 12:33
  • What exactly do you expect from ExecuteNonQuery? It does not return rows to be read by your adapter. Your dataset is empty and your attempt to access the first table results in the error. – SMor Dec 11 '19 at 15:36

3 Answers3

1

Check whether dataTable null or not

if(dataTable!=null && dataTable.Rows.Count > 0)
{
foreach (DataRow dr in dataTable.Rows)
    {
        Menu_Item menu_Item = new Menu_Item()
        {
            Menu_ID = (int)dr["Menu_ID"],
            Naam = (string)dr["Naam"],
            Prijs = (float)dr["Prijs"],
            Voorraad = (int)dr["Voorraad"],
            Categorie_ID = (int)dr["Catogorie_ID"]

        };
        menu_items.Add(menu_Item);
    }
}
bimal george
  • 313
  • 2
  • 16
1

check if the datarows.count is null or not ...

Also in DB_Get_All_MenuItems() mention dbconnction string, sql command type, and datareader and all...first make sure the datas is returning in this function o/p....

gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41
JJ Raj
  • 11
  • 1
0

You are trying to use something that is null. This means you either set it to null, or you never set it to anything at all. Now, You can then place a breakpoint at every found location and run your program with the debugger attached. Every time the debugger breaks on such a breakpoint, you need to determine whether you expect the reference to be non-null.

Developer
  • 89
  • 1
  • 14