Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to fill listBox1 with product name from Access database, i face an error, this is my code on the form load...
C#
private void WStoreSystem_Load(object sender, EventArgs e)
        {
          //////////////////////////////////////////////////////////////////////

            string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\WStoreSystem\\WStoreSystem\\WStoreSystem\\bin\\Debug\\StoreSys.mdb";
            OleDbConnection con = new OleDbConnection(str);
            var dss = new DataSet();
            var adapter = new OleDbDataAdapter("SELECT ProName FROM Products", con);
            con.Open();
            adapter.Fill(dss);
            DataTable dt;
            dt = dss.Tables[0];

            Dictionary<string, string> ProName = new Dictionary<string, string>();



the error is :
Column 'ProductID' does not belong to table Table.
            foreach (DataRow row in dss.Tables[0].Rows)
            {
                ProName.Add(row["ProductID"].ToString(), row["ProName"].ToString()); //Product name is the    key.
            }

            listBox1.DataSource = ProName;
            listBox1.DisplayMember = "Key";
            listBox1.ValueMember = "Key";
            
            con.Close();

         /////////////////////////////////////////////////////////////////////
           
            ProductsClass prod = new ProductsClass();
    
           
            DataSet ds = prod.get();
            
            DGV1.DataSource = ds.Tables[0];
            DGV1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            DGV1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            
           
            this.ControlBox = false;
}
Posted
Updated 30-Apr-15 16:25pm
v2

The only column you retrieved from the database is ProName. There is no ProductID listed in your SELECT statement.
 
Share this answer
 
try
{

string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\WStoreSystem\\WStoreSystem\\WStoreSystem\\bin\\Debug\\StoreSys.mdb";
OleDbConnection con = new OleDbConnection(str);
con.Open();
OleDbCommand comm= new OleDbCommand();
comm.Connection = con;
string qu = "SELECT ProName FROM Products";
comm.CommandText = qu;
OleDbDataReader reder = comm.ExecuteReader();
while (reder.Read())
{
listBox1.Items.Add(reder["ProName"].ToString());
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error "+ ex);
}
 
Share this answer
 

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