Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear people

How to populate a ListView with database records on click event button. Basically I have a form, and in this form I have a button (called Starters), and a listview. On my database I have a table called Food.
The attributes of this table are:

=============================================
FoodID    FoodName     FoodType     FoodPrice
=============================================
000001     Soup        Starter      £4.50
-------    ------      --------     -------

=============================================


What I basically want is, on click event button to display on my listview only the the FoodName...
Here is the code on Starter click event button, but for some reason it gives me an error.

public partial class StockIn : Form
{
     private DataSet DtposMenuDS = new DataSet("DtposMenu");
     private DataTable starterTable = null;

     public StockIn()
     {
        InitializeComponent();
     }

     private void cmdExit_Click(object sender, EventArgs e)
     {
        this.Close();
     }

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

        // Food SQL Query
        string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";

        using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
        {
            try
            {
                conn.Open();
                // TODO: Get Food here
               OleDbCommand foodsCommand = new OleDbCommand(foodTypeSql, conn);
               OleDbParameter foodType = foodsCommand.Parameters.Add("@foodType", OleDbType.VarChar, 15);
               OleDbDataAdapter foodsDa = new OleDbDataAdapter(foodsCommand);
               foodType.Value = "Starters";
               foodsDa.Fill(DtposMenuDS, "Starters");

               starterTable = DtposMenuDS.Tables["Starter"];
                    
                    
               ListViewItem foodItem = new ListViewItem();
               foodItem.SubItems.Add(((DataRowView)DtposMenuBS.Current)["FoodName"].ToString());
               this.listViewItemsInStock.Items.Add(foodItem);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex);
            }
        }
    }

}



Any help it would be very very greatfull....

Thanks in advance


Roni
Posted
Comments
Al-Farooque Shubho 28-Dec-10 21:09pm    
Exactly what error message it is showing?
LAPEC 28-Dec-10 21:36pm    
Hi Al-Farooque Shubho
The error is:

Object reference not set to an instance of an object

Well, given the error message, it seems there is at least one object having null value which you are trying to use. I would suggest you to put a break point and try to debug in Visual Studio to find out exactly where the exception is occurring.
 
Share this answer
 
Comments
LAPEC 28-Dec-10 21:49pm    
at this line of code

ListViewItem foodItem = new ListViewItem(((DataRowView)DtposMenuBS.Current)["FoodName"].ToString());
Al-Farooque Shubho 28-Dec-10 22:14pm    
Well, if ((DataRowView)DtposMenuBS.Current)["FoodName"] is null, then the "Object reference not set to an instance of an object" error will occur.

I would suggest you to execute the SQL command on the database to see what result set appears and also to see whether any row of the result set does NOT have the "FoodName" value. Most likely one or more row does not have the "FoodName" value and that's why such error is occurring.
Manfred Rudolf Bihy 29-Dec-10 15:20pm    
Good answer!
I copied your code and added comments:

C#
public partial class StockIn : Form
{
     private DataSet DtposMenuDS = new DataSet("DtposMenu");
     private DataTable starterTable = null;
     public StockIn()
     {
        InitializeComponent();
     }
     private void cmdExit_Click(object sender, EventArgs e)
     {
        this.Close();
     }
     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");
        // Food SQL Query
        string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
        using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
        {
            try
            {
                conn.Open();
                // TODO: Get Food here
               OleDbCommand foodsCommand = new OleDbCommand(foodTypeSql, conn);
               OleDbParameter foodType = foodsCommand.Parameters.Add("@foodType", OleDbType.VarChar, 15);
               OleDbDataAdapter foodsDa = new OleDbDataAdapter(foodsCommand);
               //The entry in the columns foodTpe in your table reads Starter and not Starters
               // WRONG: foodType.Value = "Starters";
               foodType.Value = "Starter";
               // Your table is called Food
               foodsDa.Fill(DtposMenuDS, "Food");
               // Your table is called Food
               starterTable = DtposMenuDS.Tables["Food"];
                    
                    
               ListViewItem foodItem = new ListViewItem();
               foodItem.SubItems.Add(((DataRowView)DtposMenuBS.Current)["FoodName"].ToString());
               this.listViewItemsInStock.Items.Add(foodItem);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex);
            }
        }
    }
}


Hope that helps!

Cheers,

Manfred
 
Share this answer
 
Comments
LAPEC 28-Dec-10 21:43pm    
Hi Manfred

I'm confused about the comments can you be more specific please


thanks

Roni
Manfred Rudolf Bihy 29-Dec-10 6:03am    
Hi Roni, your parameter for the query is "Starters", but in the table you've shown us the only entry in column FoodType is "Starter". Thus your query returns no rows and that is why DtposMenuBS.Current contains no records. You are trying to access a field in an empty record set which will most likely cause a null reference exception.

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