Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5939505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:50:16+00:00 2026-05-22T15:50:16+00:00

I kept getting FormatException was unhandled by user Code Following is the Code: using

  • 0

I kept getting “FormatException was unhandled by user Code”
Following is the Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Eventmanagement
{
    public partial class Registration : Form
    {
        SqlConnection aConnection;
        string firstname= string.Empty; 
        string lastname= string.Empty;
        int aid;
        string date = DateTime.Now.ToShortDateString();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dta;


        public Registration(string fname, string lname, int attID)
        {
            this.firstname = fname;
            this.lastname = lname;
            this.aid = attID;
            InitializeComponent();
        }
        //--------------------------------------------//

        private void Registration_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'insertEventDataSet.Events' table. You can move, or remove it, as needed.

            populateEventSalesPersonList();
            populateEventNameIdTypeList();

            //+++++++++++++++++++++++++++++++++++++++++++//
            txtSalesTaxRate_Registration.Text = Convert.ToString( SalesTaxRate());
            txtRegistrationID_Registration.Text = regID().ToString();
            //+++++++++++++++++++++++++++++++++++++++++++//

            //+++++++++++++++++++++++++++++++++++++++++++//
            txtAttendee_Registration.Text = (this.firstname+" "+this.lastname);
            txtRegistrationDate_Registration.Text = date.ToString();


        }

        //--------------------------------------------//

        public string getConnectionString()
        {
            try
            {
                string sConnection = "";

                // Get the mapped configuration file.
                System.Configuration.ConnectionStringSettingsCollection ConnSettings = ConfigurationManager.ConnectionStrings;
                sConnection = ConnSettings["DBConnectionString"].ToString();

                return sConnection;


            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return "";
            }
        }



        //--------------------------------------------//
        public void populateEventNameIdTypeList()
        {
            try
            {
                cmbEvent_Registration.DataSource = getDataTable4();
                //----------------------------
                cmbEvent_Registration.ValueMember = "EventID";
                cmbEvent_Registration.DisplayMember = "EventName";




            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }


        }


        //--------------------------------------------//

        public void populateEventSalesPersonList()
        {
             try
            {

                cmbSalesPerson_Registration.DataSource = getDataTable5();
                cmbSalesPerson_Registration.ValueMember = "EmployeeID";
                cmbSalesPerson_Registration.DisplayMember = "SalesPerson";

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        //-------------------------------------------//

        public void populateFeeScheduleByEventList()
        {

            try
            {

                cmbFeeSchedule_Registration.DataSource = getDataTable6(); 
                cmbFeeSchedule_Registration.ValueMember = "FeeScheduleID";
                cmbFeeSchedule_Registration.DisplayMember = "Fee";




                //--------------------------------------------------//

                //saleTax();
                //txtSalesTax_Registration.Text = Convert.ToString(saleTax());
                //txtTotalCharges_Registration.Text = Convert.ToString(totalCharges());
                //txtAmountDue_Registration.Text = Convert.ToString(amountDue());

                //--------------------------------------------------//
                }

            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }

        //------------------------------------------//

        //------------------------------------------//



        private void btnclose_Registration_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInsert_Registration_Click(object sender, EventArgs e)
        {
            try
            {
                aConnection = new SqlConnection(getConnectionString());
                aConnection.Open();

                //Calling the Stored Procedure

                da.InsertCommand = new SqlCommand("RegistrationInsert", aConnection);
                da.InsertCommand.CommandType = CommandType.StoredProcedure;

                //da.InsertCommand.Parameters.Add(@"RegistrationID", SqlDbType.Int).Value =  Convert.ToInt16( txtRegistrationID_Registration.Text);
                da.InsertCommand.Parameters.Add(@"AttendeeID", SqlDbType.Int).Value =  Convert.ToInt16( aid); 
                da.InsertCommand.Parameters.Add(@"RegistrationDate", SqlDbType.SmallDateTime).Value = Convert.ToDateTime(txtRegistrationDate_Registration.Text=date.ToString());
                da.InsertCommand.Parameters.Add(@"PurchaseOrderNumber", SqlDbType.VarChar).Value = txtPoNumber_Registration.Text; 
               // da.InsertCommand.Parameters.Add(@"SalesPerson", SqlDbType.Int).Value = Convert.ToInt64(cmbSalesPerson_Registration.SelectedValue.ToString());
                da.InsertCommand.Parameters.Add(@"EventID", SqlDbType.Int).Value = cmbEvent_Registration.SelectedValue.ToString();
                da.InsertCommand.Parameters.Add(@"FeeScheduleID", SqlDbType.Int).Value = cmbFeeSchedule_Registration.SelectedValue.ToString();
                da.InsertCommand.Parameters.Add(@"RegistrationFee", SqlDbType.Money).Value = txtRegistrationFee_Registration.Text;
                da.InsertCommand.Parameters.Add(@"EmployeeID", SqlDbType.Int).Value = Convert.ToInt16(cmbSalesPerson_Registration.SelectedValue.ToString());
                da.InsertCommand.Parameters.Add(@"SalesTaxRate", SqlDbType.Float).Value = txtSalesTaxRate_Registration.Text;
                //da.InsertCommand.Parameters.Add(@"EndTime", SqlDbType.SmallDateTime).Value = Convert.ToDateTime(txtEndTime.Text.ToString());
                da.InsertCommand.ExecuteNonQuery();


                MessageBox.Show("Inserted successfully!!!");

                aConnection.Close();
                da.InsertCommand.Dispose();

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }


        }

        //-------------------------------------------//

        public DataTable getDataTable4()
        {
            try
            {
                dta = new DataTable();
                aConnection = new SqlConnection(getConnectionString());
                aConnection.Open();
                da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand("EventsSelectAll", aConnection);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.ExecuteNonQuery();

                da.Fill(dta);
                aConnection.Close();
                return dta;

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return null;
            }
        }
        public DataTable getDataTable5()
        {
            try
            {
                dta = new DataTable();
                aConnection = new SqlConnection(getConnectionString());
                aConnection.Open();
                da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand("sp_RegistrationSalesPerson", aConnection);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.ExecuteNonQuery();
                dta.Clear();
                da.Fill(dta);
                aConnection.Close();
                return dta;

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return null;
            }
        }
        public DataTable getDataTable6()
        {
            try
            {
                dta = new DataTable();
                da = new SqlDataAdapter();
                aConnection = new SqlConnection(getConnectionString());

                aConnection.Open();

                da.SelectCommand = new SqlCommand("sp_FeeScheduleSelectAllByEventID", aConnection);
                da.SelectCommand.Parameters.Add("EventID", SqlDbType.Int).Value = Convert.ToInt32(cmbEvent_Registration.SelectedValue.ToString());
                da.SelectCommand.CommandType = CommandType.StoredProcedure;


                da.SelectCommand.ExecuteNonQuery();
                da.Fill(dta);
                aConnection.Close();
                return dta;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return null;
            }


        }



        private void cmbEvent_Registration_SelectedIndexChanged(object sender, EventArgs e)
        {
                populateFeeScheduleByEventList();

                txtRemainingSeats_Registration.Text = Convert.ToString(spaces());
        }




        public double saleTax()
        {                               
            double regFee = Convert.ToDouble(cmbFeeSchedule_Registration.SelectedText);
            double sTR = Convert.ToDouble(SalesTaxRate());
            double sr = regFee * (sTR / 100);
            return sr;
        }
        public int totalRegisteredAttendees()
        {
            da = new SqlDataAdapter();
            aConnection = new SqlConnection(getConnectionString());
            da.SelectCommand = new SqlCommand("RegistrationSelectAllByEventID", aConnection);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add("EventID", SqlDbType.Int).Value = Convert.ToInt32(cmbEvent_Registration.SelectedValue.ToString());

            aConnection.Open();
            int val = (int)da.SelectCommand.ExecuteScalar();
            aConnection.Close();
            return val;
        }
        public int spaces()
        {
            da = new SqlDataAdapter();
            aConnection = new SqlConnection(getConnectionString());
            da.SelectCommand = new SqlCommand("EventsSelect", aConnection);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add("EventID", SqlDbType.Int).Value = Convert.ToInt32(cmbEvent_Registration.SelectedValue.ToString());
            aConnection.Open();

            int spaces = Convert.ToInt16(da.SelectCommand.ExecuteScalar());
            aConnection.Close();

            int value = totalRegisteredAttendees();
            int totalspaces = spaces - value;
            return totalspaces;

        }

        public double SalesTaxRate()
        {
            da = new SqlDataAdapter();
            aConnection = new SqlConnection(getConnectionString());
            da.SelectCommand = new SqlCommand("CompanyInfo", aConnection);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;

            aConnection.Open();
            double sale = Convert.ToDouble(da.SelectCommand.ExecuteScalar());
            aConnection.Close();
            return sale;
        }


        public double totalCharges()
        {
            double tc = Convert.ToDouble(txtRegistrationFee_Registration.Text) +  (saleTax());
            return tc;
        }
        public double amountDue()
        {
            double aD;
            if (txtTotalPaid_Registration.Text == string.Empty)
            {
                aD = Convert.ToDouble(txtTotalCharges_Registration.Text.ToString());
            }
            else
            {
                double a = Convert.ToDouble(txtTotalPaid_Registration.Text.ToString());
                 double b=    (Convert.ToDouble(txtTotalCharges_Registration.Text.ToString()));
                 aD = b-a;
            }
                return aD;
        }

        public int regID()
        {
            da = new SqlDataAdapter();
            aConnection = new SqlConnection(getConnectionString());
            da.SelectCommand = new SqlCommand("RegistrationID", aConnection);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;

            aConnection.Open();
            int id = ((int)da.SelectCommand.ExecuteScalar())+1;

            aConnection.Close();
            return id;  
        }

        private void cmbFeeSchedule_Registration_SelectedIndexChanged(object sender, EventArgs e)
        {
           saleTax();
        }

        //------------------------------------------//
    }
}

When I call SaleTax() Method in following

private void cmbFeeSchedule_Registration_SelectedIndexChanged(object sender, EventArgs e)
        {
           saleTax();
        }

I get “FormatException was unhandled by user Code” Error
I debugged the Code and find out that it is happening because cmbFeeSchedule_Registration is not selecting the first index, hence it is giving the error. I am confused what to do?
How I get around this Problem?

Edit:
Well I resolved the issue. It was the problem of in ` public void populateFeeScheduleByEventList()
{

        try
        {

            cmbFeeSchedule_Registration.DataSource = getDataTable6(); 
            cmbFeeSchedule_Registration.ValueMember = "FeeScheduleID";
            cmbFeeSchedule_Registration.DisplayMember = "Fee";
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }

    }`  

When SaleTax() was called, pointer leaves the DisplayMember and ValueMember. Hence I readjusted the code little bit and placed cmbFeeSchedule_Registration.ValueMember = "FeeScheduleID"; cmbFeeSchedule_Registration.DisplayMember = "Fee"; before
cmbFeeSchedule_Registration.DataSource = getDataTable6(); and it resolved the Problem. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:50:17+00:00Added an answer on May 22, 2026 at 3:50 pm

    If it is required that index 0 be selected in your ComboBox then you need to do some basic checking. In your SelectedIndexChanged EventHandler wire-up, you need to add:

    if (cmbFeeSchedule_Registration.SelectedIndex == 0) {
        salesTax();
    }
    

    Also if the user is able to change the text in the ComboBox, you are not doing any error handling to handle the FormatException that will be caused by Convert.ToDouble when trying to convert a non-double value that is not parsable.

    double regFee = Convert.ToDouble(cmbFeeSchedule_Registration.SelectedText);            
    double sTR = Convert.ToDouble(SalesTaxRate());
    

    Furthermore, you could actually use double.Parse or double.TryParse instead of ConvertTo, which seems more proper. On another note, if the user cannot edit the text in the ComboBox (say you have it set to DropDownList), you should use the SelectedObject or SelectedValue property instead of SelectedText depending if it is DataBound or not.

    Bottom line is you need to make sure you are checking that the correct index is selected before calling salesTax() or any operation that will potentially throw an exception, and do error checking to ensure that your values are what they should be. If regFee is anything but a double, because the correct index is not selected, ConvertTo will fail. (Use double.Parse/TryParse instead, anyway)

    A tip of advice when posting questions is to only post the affected segment of code, and provide a StackTrace when one is available. It’s too difficult to navigate all of your code on here, and using a StackTrace it is easy to break down.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was following the help.github.com/win-set-up-git/ process and kept getting the Host key verification failed
Consider a case of a form that is getting its data from a service
After upgrading to PHPUnit v > 3.5 (I forgot) I kept on getting this
I am using UIDatePicker in my iPhone application.I have kept a button on clicking
I am trying to process 2 independent tasks using the new .NET 4.0 Task
I have a ListView that allows the user to change the ViewBase through a
How to read and write strings in text file in android which is kept
I have created a Conceptual Model in my project using a table in my
I am trying to authenticode sign a .NET application with a new code signing
At work we have a script that does a mysql data dump everynight. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.