Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 29th, 2011, 09:41 PM
Registered User
Points: 42, Level: 1
Points: 42, Level: 1 Points: 42, Level: 1 Points: 42, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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()
{
InitializeComponent();
}

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
  #2 (permalink)  
Old April 30th, 2011, 07:44 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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

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)
  #3 (permalink)  
Old May 2nd, 2011, 01:18 AM
Registered User
Points: 42, Level: 1
Points: 42, Level: 1 Points: 42, Level: 1 Points: 42, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2011
Posts: 9
Thanks: 5
Thanked 0 Times in 0 Posts
Default

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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:28 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.