0

Error Pic: n1

This has nothing to do with DateTime so I don't know why this keeps popping up, here is the code I'm using. When you press calculate after entering the required text its supposed to display the price then calculate the discount and finally display the final price.

The line that comes with the error - decimal price = Convert.ToDecimal(txtPrice.Text);

txtPrice is empty, or more specifically read only, the variable 'price' should set it to 100 or whatever I want from my if statement, correct?

Btw the price isn't supposed to be 100 for each section, kept changing the code slightly to make the error go away so I just copied and pasted "price = 100;" so I didn't have to type a different number each time.

private void btnCalculate_Click(object sender, EventArgs e)
{
    int tenure = Convert.ToInt16(txtTenure.Text);
    string seatingType = txtLocation.Text;
    decimal discountPercent = .0m;
    decimal price = Convert.ToDecimal(txtPrice.Text);

    if (seatingType == "Main")
    {
        price = 100;

        if (tenure >= 6)
            discountPercent = .5m;
        else if (tenure >= 12)
            discountPercent = .75m;
        else if (tenure >= 24)
            discountPercent = 1.0m;
    }
    else if (seatingType == "Pit")
    {
        price = 100;

        if (tenure >= 12)
            discountPercent = .5m;
        else if (tenure >= 12)
            discountPercent = .75m;
        else if (tenure >= 24)
            discountPercent = 1.0m;
    }
    else if (seatingType == "Balcony")
    {
        price = 100;

        if (tenure >= 12)
            discountPercent = .5m;
        else if (tenure >= 12)
            discountPercent = .75m;
        else if (tenure >= 24)
            discountPercent = 1.0m;
    }

    decimal discountAmount = price * discountPercent;
    decimal finalPrice = price - discountAmount;

    txtFinalPrice.Text = finalPrice.ToString("c");

}

I'm using a textbook example and everything looks correct, not sure why this is happening. n2

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Warhawk
  • 9
  • 3
  • Have you debugged the code? What line does the error occur on? – Steve May 11 '16 at 01:52
  • Wow I feel stupid, this one is giving me the error. decimal price = Convert.ToDecimal(txtPrice.Text); I tried using int instead of decimal and I get the same error. – Warhawk May 11 '16 at 01:54
  • What is the value of `txtPrice.Text`? Not sure why you're getting a DateTime error though. Can you post a screenshot of the exception? – Steve May 11 '16 at 01:55
  • What line is causing the error? What is the **exact** error message you're getting? What does the debugger tell you when you step through the code? The image of the code from your textbook or tutorial site is absolutely irrelevant here; you didn't copy/paste that code, so it has no value to the question here. – Ken White May 11 '16 at 01:55
  • txtPrice is empty, or more specifically read only, the variable 'price' should set it to 100 or whatever I want from my if statement, correct? I'll get a pic of the error – Warhawk May 11 '16 at 01:57
  • Possible duplicate of [int.Parse, Input string was not in a correct format](http://stackoverflow.com/questions/9372210/int-parse-input-string-was-not-in-a-correct-format) – Steve May 11 '16 at 02:00
  • @Warhawk The problem is the application won't run pass `Convert.ToDecimal`, as it will be stopped due to `FormatException` for passing a `string.Empty` to it. – Xiaoy312 May 11 '16 at 02:00
  • If `txtPrice` is supposed to be read-only, why you `Convert.ToDecimal(txtPrice.Text)` it? It is empty, so can't be parse into decimal. – raymai97 May 11 '16 at 02:05
  • That "When converting a string to a DateTime" sentence is just a *general* tip for FormatException errors. It is not meant to apply to your exact code. The actual error is "Input string was in an incorrect format". So, what is the string that you are trying to convert? – Blorgbeard May 11 '16 at 02:11
  • You said your textbox is empty? You cannot convert an empty string to a decimal (or int, or Int16). You should check `string.IsNullOrEmpty(txtPrice.Text)` before you try to convert. – Blorgbeard May 11 '16 at 02:13
  • Ok so I figured I had to do that to make the price appear in the txtPrice text box. How do I make it do that? Obviously decimal price = txtPrice.Text; isn't working. – Warhawk May 11 '16 at 02:16
  • You should check string.IsNullOrEmpty(txtPrice.Text) before you try to convert. – Blorgbeard May 11 '16 at 02:25

2 Answers2

0

The troubleshoot tips are just tips based on an specific type of exception. In your case, it is common that the FormatException error happens when converting a DateTime string. That's why Visual Studio tips you about DateTime.

https://msdn.microsoft.com/en-us/library/2ww37f14.aspx

About your error, you cannot convert an empty string to number. So I suggest you to change your code from:

int price = Convert.ToInt16(txtPrice.Text)

to

int price;
if (decimal.TryParse(txtPrice.Text, out price) == false)
    price = 100;
Paulo Prestes
  • 484
  • 2
  • 8
-1

Wrap

int tenure = Convert.ToInt16(txtTenure.Text);

with:

try
{
   int tenure = Convert.ToInt16(txtTenure.Text);
}
catch (FormatException e)
{
   ...report error...
}

Also, your if statements exit before getting to the proper discount.

You need to reverse them and check for the lower amounts first:

if (seatingType == "Main")
{
    price = 100;

    if (tenure >= 24)
        discountPercent = .5m;
    else if (tenure >= 12)
        discountPercent = .75m;
    else
        discountPercent = 1.0m;
}

etc...

As you have it now, they are exiting early before reaching the appropriate discount factor, and it doesn't handle tenure less than 6.

NoCake
  • 84
  • 7
  • Geesh, thanks for the downvotes :) Please note: poster changed his question significantly *after* answer was posted; edited a bit to align it – NoCake May 11 '16 at 02:08
  • I didn't vote at all yet, shows 0 for me? Anyway thank you for the comment, I changed the if part and thats working better. – Warhawk May 11 '16 at 02:11