Click here to Skip to main content
16,017,325 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello, i wanted to do some calculation using lables,
in my page i am using values in the label which are being bought by checkboxes,
the values in label should be added and accrodingly directed to nect page,
its a personality test, so acc to the values, the page should be directed.

the values in the label are ranging from 0 nd 10 and 20.
according to the calculation, the grand total will be ranging from 0- 50, 50-100, 150-200, according to the codes.

its showing an error of
System.FormatException: 'Input string was not in a correct format.'


What I have tried:

private void total()
 {
     //double Balance;
     //double Amount1 = double.parse(this.Amount.text);
     //double Amount2 = double.parse(this.AmountPaid.text);
     //Balance = Amount1 - Amount2;

     //double value1, value2, value3, value4, value5, value6, value7, value8, value9, value10,total;


     //value1 = Convert.ToDouble(Label7.Text);
     //value2 = Convert.ToDouble(Label8.Text);
     //value3 = Convert.ToDouble(Label9.Text);
     //value4 = Convert.ToDouble(Label10.Text);
     //value5 = Convert.ToDouble(Label11.Text);
     //value6 = Convert.ToDouble(Label12.Text);
     //value7 = Convert.ToDouble(Label13.Text);
     //value8 = Convert.ToDouble(Label14.Text);
     //value9 = Convert.ToDouble(Label15.Text);
     //value10 = Convert.ToDouble(Label16.Text);

     int total = Convert.ToInt32(Label7.Text) + Convert.ToInt32(Label8.Text) + Convert.ToInt32(Label9.Text) + Convert.ToInt32(Label10.Text) + Convert.ToInt32(Label11.Text) + Convert.ToInt32(Label12.Text) + Convert.ToInt32(Label13.Text) + Convert.ToInt32(Label14.Text) + Convert.ToInt32(Label15.Text) + Convert.ToInt32(Label16.Text);


     if (total>50)
     {
         Response.Redirect("quizresult.aspx");
     }
     else if ((total >50) & (total>= 100))
     {
         Response.Redirect("quizresult1.aspx");
     }
     else if ((total > 100) & (total >= 150))
     {
         Response.Redirect("quizresult2.aspx");
     }
     else if ((total > 150) & (total >= 200))
     {
         Response.Redirect("quizresult3.aspx");
     }
 }


new this is from where labels values are shown
<td colspan="3" style="border-style: solid; border-width: thin">
               <asp:CheckBoxList ID="CheckBoxList1" runat="server" Height="41px" Width="480px" RepeatDirection="Horizontal">
                   <asp:ListItem>A-0</asp:ListItem>
                   <asp:ListItem>B-10</asp:ListItem>
                   <asp:ListItem>C-20</asp:ListItem>
               </asp:CheckBoxList>
               <br />
               <asp:Label ID="Label7" runat="server"></asp:Label>


protected void Button2_Click(object sender, EventArgs e)
        {

            Label7.Text = CheckBoxList1.SelectedItem.Text;
            Label8.Text = CheckBoxList2.SelectedItem.Text;
            Label9.Text = CheckBoxList3.SelectedItem.Text;
            Label10.Text = CheckBoxList4.SelectedItem.Text;
            Label11.Text = CheckBoxList5.SelectedItem.Text;
            Label12.Text = CheckBoxList6.SelectedItem.Text;
            Label13.Text = CheckBoxList7.SelectedItem.Text;
            Label14.Text = CheckBoxList8.SelectedItem.Text;
            Label15.Text = CheckBoxList9.SelectedItem.Text;
            Label16.Text = CheckBoxList10.SelectedItem.Text;

        }
Posted
Updated 18-May-22 0:04am
v2
Comments
Richard Deeming 18-May-22 5:35am    
The values in your labels are not valid integers according to the parsing rules of the current culture.

Since we can't see your labels, and we have no access to your application, we can't tell you why.

You need to debug your code to find out what the label values are, and why they're not what you expect.

NB: Do yourself a favour and start giving your controls meaningful names, rather than accepting the default names suggested by Visual Studio. You might remember what value Label42 represents today, but when you come back to your code in six weeks, you'll have forgotten.
Member 15198389 18-May-22 5:37am    
the values in label are just 0 10 and 20 and hey are bought in by checkboxes, can i show you my system?please can you help
Richard Deeming 18-May-22 5:59am    
The error message you posted is:
Input string was not in a correct format

That means at least one of the values you are trying to parse is not a valid integer.

Once again, we cannot see your screen, access your computer, or read your mind. We have no way of knowing what the values in those labels are. All we know is that at least one of the values is not a valid integer.

YOU need to debug your code.
Member 15198389 18-May-22 5:40am    
the values in the label are not inputed, they are sent from the checkbox, as i wnated the checkbox lists values to be straight away added and then sen tto the next page accrodingly, but didnt know how so i used label in between, i had used text box also before but it didnt come in the textbox the values from checkbox, so i used labels to get from checkbox

1 solution

Your if statements are all incorrect. In the first case if the total is equal to 120, then it is definitely greater than 50, so it will never go to any of the other tests. So it needs to be reordered to something like:
C#
if (total >= 200))
{
    Response.Redirect("quizresult3.aspx");
}
else if (total >= 150))
{
    Response.Redirect("quizresult2.aspx");
}
else if (total>= 100))
{
    Response.Redirect("quizresult1.aspx");
}
else if (total>50)
{
    Response.Redirect("quizresult.aspx");
}

But what happens if the total is 50 or less?

Also note: a single ampersand in your if statements is not correct, it should be
C#
if (expression1 && expression2)

Also using Convert.ToInt32 on all those text fields will crash your code if any of them contains a non-numeric character. And are you sure you want the values from labels rather than input fields?
 
Share this answer
 
Comments
Member 15198389 18-May-22 5:44am    
can i please give you my anydesk code and can you check?please
Richard MacCutchan 18-May-22 5:51am    
If you have further information then use the Improve question link above, and add the details to your question.
Member 15198389 18-May-22 5:55am    
i have no much details, i dont know how to express more, so like i chnaged the if statements now how do i chnage the int double , or how do i add the values? like i am using label and those are int values only, how do i brig the label values and add them?
Richard MacCutchan 18-May-22 5:59am    
You should use Int.TryParse on each value, so you can capture any item that is not a valid number. And since we cannot see exactly what you have in these labels we cannot guess what may be wrong.
Member 15198389 18-May-22 6:05am    
i have improved question and shwoed, given just one checkbox lits but others alos the same only

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900