Reputation: 237
I basically want to total the amount.
The amount gets stored in a label. I want to add labels. Basically I want to do an addition of labels but i can't because label is .Text
which is String
so when i add it I get a string of added label while i want the Numbers stored in the labels to get added. this is my code below.
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
temp4 = Int32.Parse(DropDownList3.Text);
temp5 = temp4 * 76;
Label7.Text = temp5.ToString();
}
On the click of a button the amount in Lablel7 should get added with another Label.
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
Label16.Text = Label7.Text+Label6.Text;
}
So that the total amount can be found.
Am kinda new to programming and all itself and this is part of my project am sorry if this questions seems stupid
Upvotes: 0
Views: 14379
Reputation: 4104
Two solutions :
1- You can parse each Text to convert into Int32, that you can add and then convert in text with ToString()
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
Label16.Text = (Int32.Parse(Label17.Text) + Int32.Parse(Label6.Text)).ToString();
}
2- On each chanching, you can save values in private properties of type Int32, and work with them.
Upvotes: 1
Reputation: 12053
Label7.Text is type of string, you can add int, so you have to convert it. Afterr all you have convert back int to string
Label16.Text = (int.Parse(Label7.Text)+int.Parse(Label6.Text)).ToString();
Try to rename your controls and var. For example lblAmount tell you maore than Label6. Please read about Camel, Pascal convertion, It will help you in the future.
Upvotes: 2
Reputation: 9074
Label16.Text = (int.parse(Label7.Text)+int.parse(Label6.Text)).toString();
Use above code.
Convert your addition to string datatype.
Upvotes: 2
Reputation: 223282
Parse both label's Text property to integer and then do the addition.
Label16.Text = (int.Parse(Label7.Text) + int.Parse(Label6.Text)).ToString();
Its better if you can use int.TryParse
which would save you from the exception if the text is not a number.
int number1;
int number2;
if(!int.TryParse(Label7.Text, out number1))
{
// invalid number in Label7
}
if(!int.TryParse(Label6.Text, out number2))
{
// invalid number in Label6
}
Label16.Text = (number1 + number2).ToString();
Upvotes: 2