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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnkelly11
    New Member
    • Oct 2011
    • 8

    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.FormatE xception' occurred in mscorlib.dll

    I think it occurs at

    decimal conversion1 = Convert.ToDecim al(txtConversio n1.Text);

    Code:
    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 Conversions
    {
        public partial class Conversions : Form
        {
    
            string[,] lengths = {
                                       {"Select conversion"," "," "," "},                       // 2D array to Load comboBox with lenghts
                                       {"Miles to Kilometers","Miles","Kilometers","1.6093"}, 
                                       {"Kilometers to Miles","Kilometers","Miles","0.6214"},
                                       {"Feet to Metres","Feet","Metres","0.3048"},
                                       {"Metres to Feet", "Metres", "Feet", "3.2808"},
                                       {"Inches to Centimetres", "Inches", "Centimetres","2.54"},
                                       {"Centimetres to Inches", "Centimetres", "Inches", "0.3937"}
                                   };
            public Conversions()
            {
                InitializeComponent();
    
                for (int i = 0; i < lengths.GetLength(0); i++)
                {
                    comboBox1.Items.Add(lengths[i, 0]);
                }
                comboBox1.SelectedIndex = 0;
            }
    
            private void btnCalculate_Click(object sender, EventArgs e)
            {
                int selectedIndex = comboBox1.SelectedIndex;               // select element in comboBox
                decimal convrate = Convert.ToDecimal(lengths[selectedIndex, 3]);
                decimal conversion1 = Convert.ToDecimal(txtConversion1.Text);
                decimal result = conversion1 * convrate;
                txtConversion2.Text = result.ToString("n");
            }
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int selectedIndex = comboBox1.SelectedIndex;
                lblConversion1.Text = lengths[selectedIndex, 1];
                lblConversion2.Text = lengths[selectedIndex, 2];
                txtConversion1.Text = "";
                txtConversion2.Text = "";
            }
        }
    }

    thanks
  • arie
    New Member
    • Sep 2011
    • 64

    #2
    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.

    Comment

    Working...