0
Try below code after while loop,
var query = (from x in listBox1.Items.Cast<string>()
where x.Contains(comboBox1.Text)
select x).ToList<string>();
int sum=0;
foreach (var item in query)
{
var value = Convert.ToInt32(item.Split('=')[1]);
sum += value;
}
0
you added item in listbox is working fine?
0
hey ketan r u at there it gives the same error can you or any one help me
0
HI,
Provided previous code contain listbox3 thats whay I have provide that solution.
You are doing all things in while loop then you have to not required listbox1 for calculate sum. just Put below code
decimal sum = 0;
while (dr.Read())
{
sum += Convert.ToDecimal(int.Parse(int.Parse(dr[0].ToString())));
listBox1.Items.Add(string.Format("{0} = {1}", Combo_Services_Customer.SelectedItem, Convert.ToString(int.Parse(dr[0].ToString()))));
var sum = listBox1.Items.Cast<string>().Sum(x => int.Parse(x)).ToString();
}
Txt_Amt_Services_Customer.Text = Convert.ToString(sum);
0
from the below code i got the in listbox service_name and service_price (i.e shaving = 50) after selecting the service_name (i.e shaving) from the combobox now i want only addition of service_price (i.e 50.....) in textbox.
0
after writting that code it gives these type of error Input string was not in a correct format which is in bold letter and my code is look like after that code inserting is as follows:
private void Combo_Services_Customer_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=ABC\\MYSQL;initial catalog=saloon;user id=abc\\Anurag;integrated security=sspi");
con.Open();
SqlCommand cmd = new SqlCommand("select Service_Price from Customer_Services where Service_Name='" + Combo_Services_Customer.SelectedItem.ToString() + "'", con);
cmd.Parameters.Add(new SqlParameter("@Customer_Services", Combo_Services_Customer.SelectedItem.ToString()));
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
listBox1.Items.Add(string.Format("{0} = {1}", Combo_Services_Customer.SelectedItem, listBox1.Text = Convert.ToString(int.Parse(dr[0].ToString()))));
var sum = listBox1.Items.Cast<string>().Sum(x => int.Parse(x)).ToString();
Txt_Amt_Services_Customer.Text = Convert.ToString(sum);
}
con.Close();
}
my problem is that i want to show only addition of that integer values into textbox

0
private void Combo_Services_Customer_SelectedIndexChanged(object sender, EventArgs e)
{
//listBox1.Items.Add(Combo_Services_Customer.SelectedItem);
//Combo_Services_Customer.SelectedItem = "";
SqlConnection con = new SqlConnection("data source=ABC\\MYSQL;initial catalog=saloon;user id=abc\\Anurag;integrated security=sspi");
con.Open();
SqlCommand cmd = new SqlCommand("select Service_Price from Customer_Services where Service_Name='" + Combo_Services_Customer.SelectedItem.ToString() + "'", con);
cmd.Parameters.Add(new SqlParameter("@Customer_Services", Combo_Services_Customer.SelectedItem.ToString()));
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Txt_Amt_Services_Customer.Text = Convert.ToString(int.Parse(dr[0].ToString()));
listBox3.Items.Add(Convert.ToString(int.Parse(dr[0].ToString())));
listBox1.Items.Add(Combo_Services_Customer.SelectedItem);
//listBox1.Items.Add(string.Format("{0} = {1}", Combo_Services_Customer.SelectedItem, listBox1.Text = Convert.ToString(int.Parse(dr[0].ToString()))));
decimal sum = 0;
for (int i = 0; i < listBox3.Items.Count - 0; i++)
{
//sum += Convert.ToDecimal(listBox1.Items[i].ToString());
sum += Convert.ToDecimal(int.Parse(listBox3.Items[i].ToString()));
}
Txt_Amt_Services_Customer.Text = Convert.ToString(sum);
}
con.Close();
}

0
Hi,
Can you provide code nippet to select item from combobox and add item in listbox.
So may I help you