13
Answers

how i can add no from listbox if it containing name & value

Photo of anurag guhe

anurag guhe

8y
845
1
hi guys can any one help me ..i am developing a project in c# windows form & in that form there is 1 combobox,1 listbox and 1 textbox. my combobox should be containing the data(i.e. service_name) which is fetching from sql database and these service_name is added into the listbox with there service_price (i.e multiple item selected from combobox is added in listbox with there srvice_price) (for ex.shaving=50 in one row) now i want my textbox should be show the addition of all service_price

Answers (13)

0
Photo of ketan borsdiya
NA 1.1k 12k 8y
What error comming...
0
Photo of anurag guhe
NA 43 7.4k 8y
its not working
0
Photo of anurag guhe
NA 43 7.4k 8y
its not working
0
Photo of ketan borsdiya
NA 1.1k 12k 8y
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
Photo of ketan borsdiya
NA 1.1k 12k 8y
you added item in listbox is working fine?
0
Photo of anurag guhe
NA 43 7.4k 8y
hey ketan r u at there it gives the same error can you or any one help me
0
Photo of anurag guhe
NA 43 7.4k 8y
it gives the same error
0
Photo of ketan borsdiya
NA 1.1k 12k 8y
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
Photo of anurag guhe
NA 43 7.4k 8y
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
Photo of anurag guhe
NA 43 7.4k 8y
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
Photo of ketan borsdiya
NA 1.1k 12k 8y
Hi Anurag,
There is not need to use for loop you just paste below line after while (dr.Read()) { }
var sum = listBox1.Items.Cast<string>().Sum(x => int.Parse(x)).ToString();
and assigned sum value to your textbox.
0
Photo of anurag guhe
NA 43 7.4k 8y
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
Photo of ketan borsdiya
NA 1.1k 12k 8y
Hi,
Can you provide code nippet to select item from combobox and add item in listbox.
So may I help you