In my windows application, I have price column in the database which stores the value of the product.
In my winform I have one combo box and list box. Combo box displays all the items in the database. When user selects product from the combo box and click add, these product will be added to the list box.
In mean time when user clicks add item, I retrieve the price of the item and pass it to method which add this value.
But how can I add the multiple values, for example I add product 1, product 2 from combo box to list box, I want to add the value those items as 550+200 = 750 and display it to the text box.
Currently I am doing this.
To retrieve price:
public DataSet searchforPrice(string price)
{
DataSet dataSet = new DataSet();
// Create connection object
OleDbConnection oleConn = new OleDbConnection(connString);
try
{
oleConn.Open();
string sql = "SELECT [Price] FROM [Product] WHERE [Product Name] ='" + price + "'";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn);
dataAdapter.Fill(dataSet, "Product");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
oleConn.Close();
}
return dataSet;
}
Adding item to listbox and display total price:
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Add(comboBox2.Text);
maskedTextBox10.Text = addPrice().ToString();
}
Adding price of the product which is being selected in combobox
public int addPrice()
{
DataSet ds = searchforPrice(comboBox2.Text);
int price = Convert.ToInt32(ds.Tables[0].Rows[0]["Price"]);
return price;
}