简体   繁体   中英

How to change label's text depending on item selected on a combo box which is populated using SQLite?

I have a button that creates 6 controls for the columns of a TableLayoutPanel, the 6 columns are: Order Number, Product Name ( Combo Box ), Current Quantity ( Label ), Quantity Bought, New Quantity, and Delete Row.

The combo box is populated using SQLite. For the Variants table, I have 7 columns: [Product Name], Name, [Serial Number], [Stock On Hand], [Sell Before].

How can I make it so that, the labels under "Current Quantity" changes depending on the [Stock On Hand] of the selected item on the combo box?

On my current code an error of "Index was outside the bounds of the array" shows every time I choose in the combo box.

Code for creating dynamic combo box:

ComboBox cboItemName = new ComboBox
            {
                Name = "cboItemName" + rowIndex,
                Dock = DockStyle.Fill,
                DropDownStyle = ComboBoxStyle.DropDownList
            };
            cboItemName.SelectedIndexChanged += new EventHandler(cboItemName_SelectedIndexChanged);
            tblOrders.Controls.Add(FillComboBox(cboItemName));

Code for the event handler of the combo box:

 void cboItemName_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cbo = (ComboBox)sender;
            string name = cbo.Name;
            string splittedString = new String(name.Where(Char.IsDigit).ToArray());;

            string labelName = "lblCurrentQuantity" + splittedString;
            string selectedIndex = cbo.SelectedIndex.ToString();
            string selectedItem = cbo.SelectedItem.ToString();

            Label tbx = tblOrders.Controls.Find(labelName, true).FirstOrDefault() as Label;
            EditLabelCurrentQuantity(tbx, selectedItem);
        }

And, lastly, code for the changing of label's text:

private void EditLabelCurrentQuantity(Label label, string itemSelected)
        {
            auth = new Authentication();
            auth.getConnection();

            try
            {
                using (SQLiteConnection con = new SQLiteConnection(auth.connectionString))
                {

                    con.Open();

                    SQLiteCommand cmd = new SQLiteCommand();

                    cmd.CommandText = @"SELECT [Stock On Hand] FROM Variants WHERE Name='" + itemSelected + "'";
                    cmd.Connection = con;


                    SQLiteDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        string name = Convert.ToString(reader["[Stock On Hand]"]);
                    }

                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

I have finally figured it out, if anyone ever did come across something to my bad design, I have fixed this by adding this:

string name = Convert.ToString(reader[0]);
labelCategory.Text = name;

in:

SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
    string name = Convert.ToString(reader["[Stock On Hand]"]);
}

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