Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to display a set of database records on my listview, but for some reason I get this error Object reference not set to an instance of an object.
Basically on my database table I have serveral records and thay are categorised for instance:

My database table is called Food and the attributes of this table are FoodName & FoodType.
The records on the FoodType field are categorised such as:

-Starter - here are 10 records
-Main - here are 12 records
-Dessert - here are 10 records

Now what I want is that when I click the Starter Button I want to display on my listview the FoodName of 10 records of the FoodType 'Starter'.
So far I have managed to do this under my Starter click event button (see the code below) but for some reason i get an error.

private void cmdStarters_Click(object sender, EventArgs e)
{
   OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
   connBuilder.DataSource = @"C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposMenu.accdb";
   connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
   connBuilder.Add("Jet OLEDB:Engine Type", "5");
  
   string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
   using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
   {
      try
      {
         conn.Open();
        
         OleDbCommand foodsCommand = new OleDbCommand(foodTypeSql, conn);
         OleDbParameter foodType = foodsCommand.Parameters.Add("@foodType", OleDbType.VarChar, 15);
         OleDbDataAdapter foodsDa = new OleDbDataAdapter(foodsCommand);
                   
         foodType.Value = "Starter";
         foodsDa.Fill(DtposMenuDS, "Starter");

         starterTable = DtposMenuDS.Tables["Food"];

         ListViewItem foodItem = new ListViewItem(((DataRowView)DtposMenuBS.Current)["FoodName"].ToString());
         this.listViewItemsInStock.Items.Add(foodItem);
      }
      catch (Exception ex)
      {
         MessageBox.Show("Error: " + ex);
      }
   }
}


could anyone tell how to fix this problem please


thanks in advance


roni
Posted
Comments
Manfred Rudolf Bihy 29-Dec-10 21:14pm    
Roni, how come you're reposting your question? Why don't you stay with your previous post and let us work it out together. Reposting is not considered good manners here on CP.
It would also help us of course to let us know the details of what error occurred.

1 solution

// Load Data from the DataSet into the ListView
private void LoadList()
{
// Get the table from the data set
DataTable dtable = _DataSet.Tables["Titles"];
// Clear the ListView control
listView1.Items.Clear();
// Display items in the ListView control
for (int i = 0; i < dtable.Rows.Count; i++)
{
DataRow drow = dtable.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
ListViewItem lvi = new ListViewItem(drow["title"].ToString());
lvi.SubItems.Add (drow["title_id"].ToString());
lvi.SubItems.Add (drow["price"].ToString());
lvi.SubItems.Add (drow["pubdate"].ToString());
// Add the list items to the ListView
listView1.Items.Add(lvi);
}
}
}


Please vote me if it's working...plz :)

use this type of code ....loop through the datatable of dataset and add each items into list..

give feedback if working..
 
Share this answer
 
v2

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