Wrox Programmer Forums
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 29th, 2011, 09:41 PM
Registered User
Join Date: Apr 2011
Posts: 9
Thanks: 5
Thanked 0 Times in 0 Posts
Cool Decimal to string

Hi there

Im doing the Try It in chapter 11 and cannot get around the decimal to string bit

any ideas on where i can look to try and solve where I went Wrong
I am using Visual C# 2010Express on Win7 64Bit

I have followed the example in the book but am getting an !

]FormatException was un handled

Input string was not in a correct format[

Could someone advise me on this message and point me in the right direction

this is what i have

[using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace salesTaxCalculator
public partial class salesTaxForm : Form
public salesTaxForm()

private void calculateButton_Click(object sender, EventArgs e)

//Get Input Values

decimal quantity1 = quantity1NumericUpDown.Value;
decimal quantity2 = quantity2NumericUpDown.Value;
decimal quantity3 = quantity3NumericUpDown.Value;
decimal quantity4 = quantity4NumericUpDown.Value;

decimal priceEach1 = decimal.Parse(priceEachTextBox1.Text);
decimal priceEach2 = decimal.Parse(priceEachTextBox2.Text);
decimal priceEach3 = decimal.Parse(priceEachTextBox3.Text);
decimal priceEach4 = decimal.Parse(priceEachTextBox4.Text);

decimal taxRate = decimal.Parse(taxRateTextBox.Text);
decimal shipping = decimal.Parse(shippingTextBox.Text);
decimal subTotal = decimal.Parse(subTotalTextBox.Text);

//Calculate Results

decimal extPrice1 = quantity1 * priceEach1;
decimal extPrice2 = quantity2 * priceEach2;
decimal extPrice3 = quantity3 * priceEach3;
decimal extPrice4 = quantity4 * priceEach4;

decimal subtotal = extPrice1 + extPrice2 + extPrice3 + extPrice4;
decimal salesTax = subTotal * taxRate;
decimal grandTotal= subTotal + salesTax + shipping;

//Display Results

extPriceTextBox1.Text = extPrice1.ToString("C");
extPriceTextBox2.Text = extPrice2.ToString("C");
extPriceTextBox3.Text = extPrice3.ToString("C");
extPriceTextBox4.Text = extPrice4.ToString("C");

subTotalTextBox.Text = subTotal.ToString("C");
salesTaxTextBox.Text = salesTax.ToString("C");
grandTotalTextBox.Text = grandTotal.ToString("C");/]



Last edited by harold4964; April 30th, 2011 at 05:01 AM.. Reason: Updating what I have
Old April 30th, 2011, 07:44 PM
Rod Stephens's Avatar
Wrox Author
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts

That error probably means one of the text boxes didn't have a decimal value in it when you called decimal.Parse. For example, if you entered "ABC" in the text box or left it blank, then calling decimal.Parse on it would give this error.

Try running the program again and make sure you enter valid decimal values like 1 or 1.5 in each text box.

(In later lessons you'll learn how to protect the program in case the user enters invalid values or leaves something blank.)

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
harold4964 (May 2nd, 2011)
Old May 2nd, 2011, 01:18 AM
Registered User
Join Date: Apr 2011
Posts: 9
Thanks: 5
Thanked 0 Times in 0 Posts

Hey thanks for that Rod
I understand now what I was doing wrong
I need to have a number in each field before I call the parse other wise it has no idea what I am Trying to do

You have first class aftermarket support for your product Well Done

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem converting from string to decimal georgetjojo Classic ASP Basics 1 November 29th, 2007 07:00 PM
decimal data type all 0's after decimal gobotsoup SQL Server ASP 1 February 28th, 2007 02:32 PM
Decimal to String ?!? TPP XSLT 1 January 18th, 2007 12:08 PM
decimal string to binary se7ss C# 5 July 19th, 2004 02:52 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.