Click here to Skip to main content
16,019,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
get input string was not in correct for when converting label text to integer and multiplying...

What I have tried:

C#
private void GrandTotal()
    {
        int GTotal = 0;
        for (int i = 0; i < BillingGridView.Rows.Count; i++)
        {
            String total = (BillingGridView.Rows[i].Cells[7].Text);
            GTotal += Convert.ToInt32(total.ToString());
        }
        BillTotalLabel.Text = GTotal.ToString();
        if (BillTotalLabel.Text != "")
        {
            int billtotal = Convert.ToInt32(BillTotalLabel.Text);
            int vat = Convert.ToInt32(VATPerLabel.Text.ToString());
            int totalaftervat = billtotal * vat/100;
            VATValueLabel.Text = totalaftervat.ToString();
        }
    }
Posted
Updated 9-Apr-17 6:01am
v2
Comments
[no name] 9-Apr-17 8:32am    
"get input string was not in correct for", here let me finish that for you. "get input string was not in correct format". And, it means exactly what it says. Whatever text is in that label can't be converted to an int.
Ramesh Kumar Barik 9-Apr-17 8:58am    
Check on which line the issue occurred.
It seems that the value which you want to convert to integer, is not in integer format.

@NotPoliticallyCorrect has pointed you in the right direction - the content of one of the textboxes is not an integer.

The problem with using Convert.ToInt32 is that it will, as you have discovered, throw an exception if the string is not an integer. For example "1" will be ok, "12345" will be ok but both "$35" and "14.6" will cause the failure you have observed.

A better solution is to use one of the integer parsing methods - see How to: Convert a String to a Number (C# Programming Guide)[^]

There are other issues with your code ...
C#
string total = (BillingGridView.Rows[i].Cells[7].Text);
is throwing a compile error for me, it should probably be
C#
String total = (BillingGridView.Rows[i].Cells[7].Value.ToString());

The line
int vat = Convert.ToInt32(VATPerLabel.Text.ToString());
has an unnecessary .ToString() call in it ... VATPerLabel.Text is already a string! Similarly
C#
GTotal += Convert.ToInt32(total.ToString());
doesn't need the .ToString() as you have already declared total as a String.

The line
C#
int billtotal = Convert.ToInt32(BillTotalLabel.Text);
has an unnecessary conversion in it ... you have just set the BillTotalLabel.Text to be GTotal.ToString();so that line should be
C#
int billtotal = GTotal;

So you should have something like this (note I have not tested this)
C#
private void GrandTotal()
{
    int GTotal = 0;
    int res;
    for (int i = 0; i < BillingGridView.Rows.Count; i++)
    {
        String total = (BillingGridView.Rows[i].Cells[7].Value.ToString());
        if(int.TryParse(total, out res))
            GTotal += Convert.ToInt32(total);
    }
    BillTotalLabel.Text = GTotal.ToString();
    VATValueLabel.Text = "";
    if (String.IsNullOrWhiteSpace(BillTotalLabel.Text))
        return;

    int billtotal = GTotal;
    int vat;
    if (int.TryParse(VATPerLabel.Text, out vat))
    {
        int totalaftervat = billtotal*vat/100;
        VATValueLabel.Text = totalaftervat.ToString();
    }
}
I will leave you with one final comment - I don't think you want to be using int for some of these variables - decimal or double might be more appropriate
 
Share this answer
 
Comments
Karthik_Mahalingam 9-Apr-17 23:22pm    
5
Place proper trim if space or some character is coming.
 
Share this answer
 

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