Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I hope you're well.
I want to read the data from my Access database into my checkboxes. So, I read the database into an arrayList but I do not know how I can assign those values to my checkboxes. So, this is for creating a prototype of an online shop using Windows Forms Applicaton in c#. I want the names of the products to come from the database and be displayed in the checkboxes that are on the form beneath the pictures of the products. Please assist me.

What I have tried:

sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace FinalAssignment
{
public partial class Customer : Form
{
private OleDbConnection connection = new OleDbConnection();
public Customer()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=FILE PATH;Persist Security Info=False;";
}



private void Customer_Load(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT * Cakes";
List<string> Cakes = new List<string>();
using (OleDbDataReader reader = command.ExecuteReader())
{
while(reader.Read())
{
Cakes.Add(reader["Product Name"].ToString());
}
}
checkBox1.Text = Cakes(0);

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


}
}
}
Posted
Updated 11-Oct-20 0:13am
v2
Comments
BillWoodruff 11-Oct-20 5:26am    
Where do the CheckBoxes ... besides checkBox1 ... come from ? Are they on the Form ? Or, do you need to create them at run-time ? Is the number of 'Cakes variable ?
Batflash 11-Oct-20 5:33am    
The checkboxes are on the form.
'Cakes' is the name of my Arraylist.

Firstly, your command is wrong, it will fail as you don't specify the table you want to get the data from:
SQL
SELECT * FROM Cakes
In addition, it's a good practice to always specify the columns you want rather than use SELECT *:
SQL
SELECT MyColumn1, MyColumn2 FROM Cakes
That way, you don't waste bandwidth fetching columns you won't use, and your code is more "future proof" against later DB changes. In your case you probably want:
SQL
SELECT `Product Name` FROM Cakes
As your column contains spaces. (It's a better idea to use non-spaced column names instead)

Instead of using checkboxes to hold your products, I'd suggest using a DataGridView - if you create a Cake class instead of using strings, you can both display more information and use the selection facilities available to the control for you user to pick the item(s) he wants. Individual checkboxes are a PITA, as you don't know at design time how many you need, and a new cake added to the DB means changing your app!
Give it a try: it's a pretty simple control to use and it makes your life a whole load eaiser!
 
Share this answer
 
Comments
Batflash 11-Oct-20 5:25am    
Okay, thank you. But, let's say, in my case, I know the number of checkboxes, how do I do it? Because using datagridview will not display the products as would be in a shop.
BillWoodruff 11-Oct-20 5:31am    
"display the products as would be in a shop" Please edit your original post, answer the questions I asked you in a comment, and explain this in more detail.
OriginalGriff 11-Oct-20 5:59am    
Seriously?
You would need to construct an array to hold the checkboxes and then use an index into that inside the loop.
Which is messay, and - to be honest - a pretty bad design for a shop!
If this is your "Final assignment" then you want it to look "realistic" not a half baked "this is easy for me the programmer" solution - which your checkboxes will do!
Batflash 11-Oct-20 6:08am    
Okay then, advise me exactly how I would go about it.
OriginalGriff 11-Oct-20 6:22am    
It's your assignment, not mine: I don't have access to exactly what is required of you. But if this is the final assignment, it's supposed to showcase what you have learned so far - so read the question really carefully, and start looking for how "real world" shops do something similar. That'll give you a basis for what you need to design - and it won't involve a dozen checkboxes! :laugh:
Note: a List<string> is not an ArrayList: it is a generic List of Type 'String

Assume:

1) you created a List<CheckBox> of all the CheckBoxes

2) you have a valid List<string> containing the names of cakes

3) you never have more 'Cakes than the number of CheckBoxes

4) you followed OriginalGriff's suggestions in using the database
public void SetCakeCheckBoxes(List<CheckBox> cakeCheckBoxes, List<string> cakes)
{
    int ncakes = cakes.Count;

    for (int i = 0; i < cakeCheckBoxes.Count; i++)
    {
        CheckBox cbx = cakeCheckBoxes[i];

        if (i < ncakes)
        {
            cbx.Text = cakes[i];
            cbx.Visible = true;
        }
        else
        {
            // handle unused CheckBoxes ?
            cbx.Text = "";
            cbx.Visible = false;
        }
    }
}
 
Share this answer
 
v3
Comments
Batflash 11-Oct-20 6:33am    
Wait, so where exactly does this code go?
BillWoodruff 11-Oct-20 7:13am    
It goes where you put it after you study it, experiment, and understand and/or modify it. And, that is your task.
Batflash 11-Oct-20 7:57am    
Right, sorry. Thank you so much.

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