简体   繁体   中英

DataTable Rows Count Null Exception

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

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

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....

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM