473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can someone please help me with An unhandled exception in Conversion code

Hi

can someone help me with this error in microsoft visual studio for the following code.

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

I think it occurs at

decimal conversion1 = Convert.ToDecimal(txtConversion1.Text);

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Conversions
  11. {
  12.     public partial class Conversions : Form
  13.     {
  14.  
  15.         string[,] lengths = {
  16.                                    {"Select conversion"," "," "," "},                       // 2D array to Load comboBox with lenghts
  17.                                    {"Miles to Kilometers","Miles","Kilometers","1.6093"}, 
  18.                                    {"Kilometers to Miles","Kilometers","Miles","0.6214"},
  19.                                    {"Feet to Metres","Feet","Metres","0.3048"},
  20.                                    {"Metres to Feet", "Metres", "Feet", "3.2808"},
  21.                                    {"Inches to Centimetres", "Inches", "Centimetres","2.54"},
  22.                                    {"Centimetres to Inches", "Centimetres", "Inches", "0.3937"}
  23.                                };
  24.         public Conversions()
  25.         {
  26.             InitializeComponent();
  27.  
  28.             for (int i = 0; i < lengths.GetLength(0); i++)
  29.             {
  30.                 comboBox1.Items.Add(lengths[i, 0]);
  31.             }
  32.             comboBox1.SelectedIndex = 0;
  33.         }
  34.  
  35.         private void btnCalculate_Click(object sender, EventArgs e)
  36.         {
  37.             int selectedIndex = comboBox1.SelectedIndex;               // select element in comboBox
  38.             decimal convrate = Convert.ToDecimal(lengths[selectedIndex, 3]);
  39.             decimal conversion1 = Convert.ToDecimal(txtConversion1.Text);
  40.             decimal result = conversion1 * convrate;
  41.             txtConversion2.Text = result.ToString("n");
  42.         }
  43.  
  44.         private void btnExit_Click(object sender, EventArgs e)
  45.         {
  46.             this.Close();
  47.         }
  48.  
  49.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  50.         {
  51.             int selectedIndex = comboBox1.SelectedIndex;
  52.             lblConversion1.Text = lengths[selectedIndex, 1];
  53.             lblConversion2.Text = lengths[selectedIndex, 2];
  54.             txtConversion1.Text = "";
  55.             txtConversion2.Text = "";
  56.         }
  57.     }
  58. }
  59.  
  60.  

thanks
Nov 26 '11 #1
1 1886
arie
64
This exception means that: "value is not a number in a valid format."

You have to check what exactly is in your txtConversion1.Text at the moment of convertsion. Set a breakpoint there and find out. The string you're trying to convert may be invalid, or empty.
Nov 28 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tommy DN | last post by:
I'm working with Oracle Form Builder and I needed a trigger to check something when I delete a record. I made a trigger on ' KEY-DELREC ' and I've written this code ( it's just a test, the delete...
3
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
2
by: Tony Hamill | last post by:
Hi, I am trying to set up a VERY simple project to send message to MSMQ from ..Net using C#. When I run my code it throws an exception "An unhandled exception of type...
4
by: Anders Borum | last post by:
Hello! I am working on improving my threading skills and came across a question. When working with the ReaderWriterLock class, I am getting an unhandled exception if I acquire a WriterLock with...
4
by: Craig831 | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
7
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote...
5
by: Lucvdv | last post by:
Can someone explain why this code pops up a messagebox saying the ThreadAbortException wasn't handled? The first exception is reported only in the debug pane, as expected. The second (caused by...
5
by: Simon Tamman {Uchiha Jax} | last post by:
Now this is bugging me. I just released software for a client and they have reported an unhandled stack overflow exception. My first concern is that the entirity of the UI and any threaded...
2
by: royjm18 | last post by:
Dim MyMoney As Single Dim DRolls As Integer Dim NRolls As Integer Dim PRolls As Integer Dim QRolls As Integer Dim RemainingChange As Single MyMoney = 28.24 QRolls = Int(MyMoney / 10)...
5
by: sweety56 | last post by:
The error I get is Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index This points to using System; using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.