C#编码根据使用数据库的组合框向文本框添加值

时间:2016-05-15 04:42:13

标签: c#

我根据cmbParent组合框中的更改值编写了此代码以更改cmbChild组合框中的值。现在我有另一个名为“price”的文本框来显示cmbChild组合框中显示的产品价格。我有一个名为“table”的数据库表,它有两列名为brand& amp;价钱。我可以在品牌栏中找到cmbChild组合框项目。当客户在cmbChild组合框中选择不同的名称时,我需要更改文本框中的值。请帮我。我的代码在这里。 BT它不起作用。它适用于文本框点,但它会提供有关SQL编码的错误消息。

`public Form1()         {             的InitializeComponent();

        listParent.Add("Chocolate");
        listParent.Add("Biscuit");
        listParent.Add("Milk");
        listParent.Add("Sugar");
        listParent.Add("Flour");
        listParent.Add("Ice Cream");

        listChild.Add(new Tuple<string, string>("Chocolate", "Kandos"));
        listChild.Add(new Tuple<string, string>("Chocolate", "Edna"));
        listChild.Add(new Tuple<string, string>("Chocolate", "Mars"));
        listChild.Add(new Tuple<string, string>("Chocolate", "Rovello"));

        listChild.Add(new Tuple<string, string>("Biscuit", "Maliban"));
        listChild.Add(new Tuple<string, string>("Biscuit", "Munchee"));
        listChild.Add(new Tuple<string, string>("Biscuit", "Maam"));

        listChild.Add(new Tuple<string, string>("Milk", "Anchor"));
        listChild.Add(new Tuple<string, string>("Milk", "Nespray"));
        listChild.Add(new Tuple<string, string>("Milk", "Raththi"));
        listChild.Add(new Tuple<string, string>("Milk", "Lakspray"));

        listChild.Add(new Tuple<string, string>("Sugar", "Arpico"));
        listChild.Add(new Tuple<string, string>("Sugar", "Cargils"));

        listChild.Add(new Tuple<string, string>("Flour", "Prima"));
        listChild.Add(new Tuple<string, string>("Flour", "MDK"));

        listChild.Add(new Tuple<string, string>("Ice Cream", "Elephant House"));
        listChild.Add(new Tuple<string, string>("Ice Cream", "Keels"));
        listChild.Add(new Tuple<string, string>("Ice Cream", "Highland"));



        foreach (var item in listParent)
        {
            cmbParent.Items.Add(item);
        }

        cmbParent.SelectedIndexChanged += CmbParent_SelectedIndexChanged;
        cmbChild.SelectedIndexChanged += CmbChild_SelectedIndexChanged;
    }

    List<String> listParent = new List<String>();
    List<Tuple<String, String>> listChild = new List<Tuple<String, String>>();


    private void CmbChild_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedBrand = (string)cmbChild.SelectedItem;

        //get data from database
        SqlConnection conn = new SqlConnection("[your connection string]");
        SqlCommand command = new SqlCommand();
        command.Connection = conn;

        command.CommandText = "Select Price from [your table] where productBrand = @productBrand";
        command.Parameters.AddWithValue("@productBrand", selectedBrand);

        conn.Open();
        decimal price = (decimal)command.ExecuteScalar();

        txtPrice.Text = price.ToString();

        conn.Close();
    }

    private void CmbParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<Tuple<String, String>> productsList = new List<Tuple<String, String>>();
        productsList = listChild.Where(x => x.Item1 == (string)cmbParent.SelectedItem).ToList();

        cmbChild.Items.Clear();
        foreach (var item in productsList)
        {
            cmbChild.Items.Add(item.Item2);
        };
    }`

0 个答案:

没有答案