简体   繁体   中英

Combobox only fills with last record from database and not all records

I got a problem where I want to fill a combobox with items from a list. The list gets items from my database.

The problem is that the combobox only fills with the last record in database.

Example : If I got 5 records in my database with 5 different Id's like this: 1, 2, 3, 4, 5. Then the combobox only show me the Id 5, five times. Like this: 5-5-5-5-5. What I want is: 1-2-3-4-5.

It might be a small error and I've tried to research and test different things but nothing have worked. Would appreciate any help. Thanks in advance.

The code below is from my database class where I think the problem is, which fills my list with items from database:

    public static List<Product> getProducts()
    {
        List<Product> listProducts= new List<Product>();

        try
        {
            connection = new SqlConnection("Data Source=localhost\\SQLEXPRESS;Initial Catalog=DB_Products;Integrated Security=SSPI;");
            connection.Open();

            SqlCommand selectProductNumber = new SqlCommand(@"Select ProductNumber FROM Product", connection);

            SqlDataReader dr = selectProductNumber.ExecuteReader();

            while (dr.Read())
            {
                Product product = new Product();
                product.ProductNumber = dr["ProductNumber"].ToString();
                listProducts.Add(product);
            }
            dr.Close();
            dr.Dispose();
            return listProducts;
        }

        finally
        {
            if (connection != null)
            {
                connection.Close();
            }
        }
    }

Here is the method to fill the combobox and where I use the methods:

    private ProductList productList = new ProductList();

    private void FillComboBox()
    {
        comboBox.Items.Clear();

        for (int i = 0; i < productList.Count; i++)
        {
            comboBox.Items.Add(productList[i].GetProductNumber(" \t "));
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        productList.Fill();
        FillComboBox();

    }

From my ProductList class:

    public void Fill()
    {
        productList = DB_class.getProducts();
    }

    public Product this[int index]
    {
        get
        {
            return productList[index];
        }
    }

    public void Add(Product product)
    {
        productList.Add(product);
    }

    public int Count
    {
        get { return productList.Count; }
    }

From my Product class:

    public string GetProductNumber(string showProductNumber)
    {
        return ProductNumber;
    }

Try this.Change your FillComboBox method as follows

private void FillComboBox()
{
    comboBox.Items.Clear();
    List<Product> proList = getProducts();
    for (int i = 0; i < proList.Count; i++)
    {
        comboBox.Items.Add(proList[i]);
    }
}

This is not a direct answer to your question, but it soles your problem. If you add an "Linq to Sql"(.dbml) file to your project. And simple drag the tables you need from Server Explorer - then you can solve it like this.

    private void button1_Click(object sender, EventArgs e)
    {
        var DBcontext = new DBDataContext();

        comboBox1.ValueMember = "ID";
        comboBox1.DisplayMember = "Description";

        comboBox1.DataSource = DBcontext.Items.ToList();

        comboBox1.SelectedIndex = 0;

        DBcontext.Dispose();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //Get displaytext
        MessageBox.Show(comboBox1.Text);

        //Get id value
        MessageBox.Show(comboBox1.SelectedValue.ToString()); 
    }

//If you want your method you should do it like this

    class Product
    {


        public string ProductNumber { get; set; }

        public override string ToString()
        {
            return ProductNumber;
        }

    }
    private List<Product> productList = new List<Product>();
    productList  = GetProducts(); //your sql function.

    private void FillComboBox()
    {
        comboBox1.Items.Clear();

        foreach (var p in productList)
        {
            comboBox1.Items.Add(p); 
        }
    }

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