Error: The input string had the wrong format while converting to decimal value in C#

Last Reply one year ago By RPA

Posted one year ago

Hi! I used Below code its not show result:

input textbox1.text = 45

output result:

lblTaxAmt.Text = 7,65

lblVat.Text = 0,54

lblSubTotal.Text = 36,81

lblTotalAmt.Text = 45

FormatException not processed.

Unhandled exception of type "System.FormatException" in mscorlib.dll

Additional information: The input string had the wrong format.

When converting a string to a date / time (DateTime), parse the string to get the date before putting each variable in the date / time object

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.IO;
using System.Data;


namespace WindowsForms.Classes
{
    class clsSelect 
    {
        //database path usually this is the path called the connection string
        public string dbPath = @"Data Source=localhost;Initial Catalog=cindyDB;Integrated Security=True";
        SqlDataReader reader;
        public decimal tax1, tax2, tax3;
        public decimal tax1_Amt, tax2_Amt, tax3_Amt, netPrice;

        //call taxes
        public void getTaxables()
        {

            try
            {
                SqlConnection con = new SqlConnection(dbPath);
                con.Open();

                string sql = "select tax_1, tax_2, tax_3 from tblTax";


                SqlCommand cmd = new SqlCommand(sql, con);

                reader = cmd.ExecuteReader();

                if (reader.Read())
                {

                    tax1 = decimal.Parse(reader[0].ToString());
                    tax2 = decimal.Parse(reader[1].ToString());
                    tax3 = decimal.Parse(reader[2].ToString());
                }
                else
                {
                    tax1 = 0;
                    tax2 = 0;
                    tax3 = 0;

                }

                reader.Close();
                con.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }


        }

        public void calcTaxAmt(decimal tax1, decimal tax2, decimal tax3, decimal totalAmt)
        {
            try
            {
                tax1_Amt = (tax1 / 100) * totalAmt;//7,65
                tax2_Amt = (tax2 / 100) * totalAmt;//0,54
                tax3_Amt = (tax3 / 100) * totalAmt;//
                netPrice = totalAmt - (tax1_Amt + tax2_Amt + tax3_Amt);//36,81


            }
            catch (Exception ex)
            {

            }
        }
      
    }
}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsForms.Classes;
namespace WindowsForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        clsSelect selectClass = new clsSelect();
        string taxPercsent, vatPercsent;
        private void button1_Click(object sender, EventArgs e)
        {
            selectClass.calcTaxAmt(decimal.Parse(taxPercsent), decimal.Parse(vatPercsent), decimal.Parse("0.00"), decimal.Parse(textBox1.Text));
            lblTaxAmt.Text = selectClass.tax1_Amt.ToString();
            lblVat.Text = selectClass.tax2_Amt.ToString();
            lblSubTotal.Text = selectClass.netPrice.ToString();
            lblTotalAmt.Text = textBox1.Text.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            selectClass.getTaxables();

            taxPercsent = string.Format("{0:n2}", selectClass.tax1.ToString());
            vatPercsent = string.Format("{0:n2}", selectClass.tax2.ToString());

            
            selectClass.calcTaxAmt(decimal.Parse(taxPercsent), decimal.Parse(vatPercsent), decimal.Parse("0.00"), decimal.Parse(textBox1.Text));
            lblTaxAmt.Text = selectClass.tax1_Amt.ToString();
            lblVat.Text = selectClass.tax2_Amt.ToString();
            lblSubTotal.Text = selectClass.netPrice.ToString();
            lblTotalAmt.Text = textBox1.Text.ToString();
        }
    }
}
Posted one year ago

Problem solved.

Changed this line:

decimal.Parse("0.00")

by

decimal.Parse("0,00")