简体   繁体   中英

get multiple values form the database and show in combo box and different text boxes when the index change c#

   try
        {
           // var model =Execute.q"SELECT product.id, product.productName, product.unitId, product.productCode, units.name FROM product INNER JOIN units ON product.unitId = units.id";

            string query1 = "SELECT product.id, product.productName, product.unitId, product.productCode, units.name FROM product INNER JOIN units ON product.unitId = units.id";
            command = DBConnectivity.getCommandForQuery(query1, connection);
            dataset = new DataSet();
            adapter = new SqlDataAdapter(command);
            adapter.Fill(dataset);
            int reslut = command.ExecuteNonQuery();
            this.cBproductName.DisplayMember = "productName";
            this.cBproductName.ValueMember = "id";
            datarow = dataset.Tables[0].NewRow();
            datarow["productName"] = "Select Product";
            dataset.Tables[0].Rows.InsertAt(datarow, 0);
            this.cBproductName.DataSource = dataset.Tables[0];
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);

        }
 private void cBproductName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.cBproductName.SelectedValue != null)
        {
            txtProCode.Text = this.cBproductName.SelectedValue.ToString();

        }
    }

i want to show product code and the unit name in the text boxes against the id in the product table and on the change on selected index in the combo box cBproductName. my query give me all the values but unable to show in the form.

You can change the SQL statement to include the informations you need.

Like:

SELECT product.productName + ' ' + product.productCode + ' ' + units.name  AS ShowText, product.id, product.productName, product.unitId, product.productCode, units.name FROM product INNER JOIN units ON product.unitId = units.id

Change cBproductName_SelectedIndexChanged method to:

DataRow row = this.cBproductName.SelectedItem as DataRow;
if (row != null)
{
  txtProCode.Text = row["ShowText"].ToString();
}

The ComboBox i bound to a DataSet that contains DataRow elements.

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