1

Here is the code:

public void calculations()
{
    int value;
    try
    {
        if ((string.IsNullOrEmpty(txtrate.Text)) || (string.IsNullOrEmpty(txttotalkm.Text)))
        {
            MessageBox.Show("Fill both the Values", "Try Again", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else if ((int.TryParse(txtrate.Text, out value)) && (int.TryParse(txttotalkm.Text, out value)))
        {
            int rate = int.Parse(txtrate.Text.Trim());
            int km = int.Parse(txttotalkm.Text.Trim());

            decimal gross = rate * km;
            decimal NBT = (gross * 8 / 100);
            decimal vat = ((gross + NBT) * 11 / 100);
            decimal total = vat + NBT + gross;

            string snbt = String.Format("{0:Rs 0.00}", NBT);
            string svat = String.Format("{0:Rs 0.00}", vat);
            string stotal = String.Format("{0:Rs 0.00}", total);

            lblnbt.Visible = true;
            lblnbt.Text = snbt;
            lblvat.Visible = true;
            lblvat.Text = svat;

            lbltotal.Visible = true;
            lbltotal.Text = stotal;
            string ltnbt = lblnbt.Text.ToString();
            string ltvat = lblvat.Text.ToString();
            string lttotal = lbltotal.Text.ToString();

            int inbt = Convert.ToInt32(lblnbt.Text);
            int ivat = Convert.ToInt32(lblvat.Text);
            int itotal = Convert.ToInt32(lbltotal.Text);

            //CreateWordDocument(@"C:\temp\test.docx",
            //           @"C:\temp\new.docx");
            clear();
        }
        else
        {
            MessageBox.Show("Wrong Values Please Check again", "Try Again", MessageBoxButtons.OK, MessageBoxIcon.Information);
            clear();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

When running the program, it says:

input string was not in a correct format error

I must say I'm just trying to code and learn, not a pro, I just can't find out the issue.

Tried removing the try...catch box and see the exception it says this:

The Exception That Jumped out

If there is an easier way to do this, just let me know.

4
  • On which line do you get the exception? Commented Aug 25, 2016 at 8:15
  • The int.Parse causing the issue here use int.TryParse instead. Hope that you are the Nth person come up with this question Commented Aug 25, 2016 at 8:15
  • @diiN_ @ int ivat = Convert.ToInt32(lblvat.Text); Commented Aug 25, 2016 at 8:32
  • @un-lucky CAN u please show where i need to edit int rate = int.TryParse(txtrate.Text.Trim()); this gives me code error Commented Aug 25, 2016 at 8:34

3 Answers 3

1

The error is here:

//snbt, svat and stotal are not valid integers, because they
//are text formatted that start with "Rs"...
string snbt = String.Format("{0:Rs 0.00}", NBT);
string svat = String.Format("{0:Rs 0.00}", vat);
string stotal = String.Format("{0:Rs 0.00}", total);

lblnbt.Visible = true;
lblnbt.Text = snbt;
lblvat.Visible = true;
lblvat.Text = svat;

lbltotal.Visible = true;
lbltotal.Text = stotal;
string ltnbt = lblnbt.Text.ToString();
string ltvat = lblvat.Text.ToString();
string lttotal = lbltotal.Text.ToString();

//HERE the exception is triggered, because the "text" in the 
//Labels start with "Rs".
int inbt = Convert.ToInt32(lblnbt.Text);
int ivat = Convert.ToInt32(lblvat.Text);
int itotal = Convert.ToInt32(lbltotal.Text);

To correct this, instead of converting AGAIN from the labels to integer, why not simply use NBT, VAT and TOTAL?

Or better, why do you need to reconvert to integers from the labels at all?

Sign up to request clarification or add additional context in comments.

2 Comments

how shoud i correct it ? ` string snbt = String.Format("{0:Rs 0.00}",NBT.ToString()); string svat = String.Format("{0:Rs 0.00}", vat.ToString()); string stotal = String.Format("{0:Rs 0.00}", total.ToString()); ` like this ?
The problem is that you are formatting values to show, and than is trying to convert them again, from the labels, to integer. Why are you doing this?
0

Parsing should be performed by always keeping the wrong format input as Exception raising cause. For the same in C#, there are clear solutions like.

•If you've got a string, and you expect it to always be an integer (say, if some web service is handing you an integer in string format), you'd use Int32.Parse().

•If you're collecting input from a user, you'd generally use Int32.TryParse(), since it allows you more fine-grained control over the situation when the user enters in invalid input.

Convert.ToInt32() takes an object as its argument, and I believe it invokes Int32.TryParse() when it finds that the object taken as the argument is a string

2 Comments

could you please show me code example. sorry for being so noob at this :)
Look the last code snippet shared by @Jauch . It will solve your issue. By the way can you show me one line which you tried to execute
0

If you only allow numeric values in your TextBox "txtrate" and "txttotalkm", you can use a "NumericUpDown" control. It only allows numeric inputs and so there is no need to use the parse methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.