Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
i have a form responsible for adding student classwork, IA, exams and give the result. base on the result it generates the Grade. my cdoes are working right but i keep getting this error message when i click on the save button "Input string was not in a correct format" please help me out

code behined the form
C#
 private void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
            conn.ConnectionString = "Data Source=MICKY-PC;Initial Catalog=SMS;User ID=sa;Password=mike";

            //save data
            try
            {

                clsStudentaccademics person = new clsStudentaccademics();

                var
                _with1 = person;
_with1.classworkfcs = txtFcs_classwork.Text;
                _with1.iafcs = txtFcs_ia.Text;
                _with1.examsfcs = txtFcs_exams.Text;
                _with1.totalsfcs = txtFcs_totals.Text;
                _with1.gradingfcs = txtFcs_grading.Text;

C#
_with1.saveRegister();
                clearScreen();
                MessageBox.Show("Record is successfully Added");

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



codes performing the calculation

C#
private void txtFcs_exams_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double classworkFcs, iaFcs, examsFcs, totalsFcs;

                classworkFcs = Convert.ToDouble(txtFcs_classwork.Text);
                iaFcs = Convert.ToDouble(txtFcs_ia.Text);
                examsFcs = Convert.ToDouble(txtFcs_exams.Text);


                totalsFcs = classworkFcs + iaFcs + examsFcs;
                txtFcs_totals.Text = totalsFcs.ToString();

                if (totalsFcs >= 75)
                {
                    txtFcs_grading.Text = "   A";
                }
                else if (totalsFcs >= 70)
                {
                    txtFcs_grading.Text = "   B";
                }
                else if (totalsFcs >= 65)
                {
                    txtFcs_grading.Text = "   C";
                }
                else if (totalsFcs >= 55)
                {
                    txtFcs_grading.Text = "   D";
                }
                else if (totalsFcs >= 45)
                {
                    txtFcs_grading.Text = "   E";
                }
                else
                {
                    txtFcs_grading.Text = "   F";
                }

            }
            catch (System.FormatException fmtE)
            {
                MessageBox.Show(fmtE.Message);
            }


        }



codes in class

C#
public object classworkfcs
       {
           get { return fcs_classwork; }
           set { fcs_classwork = Convert.ToDouble(value); }
       }

       public object iafcs
       {
           get { return fcs_ia; }
           set { fcs_ia = Convert.ToDouble(value); }
       }

       public object examsfcs
       {
           get { return fcs_exams; }
           set { fcs_exams = Convert.ToDouble(value); }
       }

       public object totalsfcs
       {
           get { return fcs_totals; }
           set { fcs_totals = Convert.ToDouble(value); }
       }
       public object gradingfcs
       {
           get { return fcs_grading; }
           set { fcs_grading = value.ToString(); }
       }
Posted
Updated 14-Jun-12 10:07am
v2

You did not say where you get the error? My guess is that something goes wrong here:

C#
classworkFcs = Convert.ToDouble(txtFcs_classwork.Text);
iaFcs = Convert.ToDouble(txtFcs_ia.Text);
examsFcs = Convert.ToDouble(txtFcs_exams.Text);


There is nothing wrong with the code here, but the data. For e.g.: if you classwork_txt is blank or someother string which can not be converted to double, you will get the error message which you currently get.
Option is to make sure that you get the correct data.

You can also try to use TryParse [^]method.
 
Share this answer
 
Comments
mikeoabban 14-Jun-12 16:23pm    
the error comes when you click the save button.
in the sql2008 db i have this
fcs_classwork float,
fcs_ia float,
fcs_exams float,
fcs_totals float,
fcs_grading varchar (10),

could it be from here
Kschuler 14-Jun-12 17:10pm    
You should be able to figure out exactly which line is causing the error. Put a breakpoint on the first line of code in the button's click event. Then step through the code one line at a time until you hit the error.

If you're a beginner and do not know about debugging, you could google for some tutorials. Here is a video I found that looks like it might help (it's for VB.net, but should provide you the basic concepts):
http://www.dailymotion.com/video/xd9sn0_visual-basic-visual-studio-tutorial_tech
mikeoabban 14-Jun-12 19:33pm    
i just found out that it is from my clearscreen method. and this is the problem this.txtFcs_exams.Text = "";
when i comment it out, there is no error message displayed. i tried this
if (string.IsNullOrEmpty(this.txtFcs_classwork.Text) && string.IsNullOrEmpty(this.txtFcs_ia.Text))
{
txtFcs_exams.Text = "";
}
but did not work. please how do i clear the text in txtFcs_exams
Sergey Alexandrovich Kryukov 14-Jun-12 20:47pm    
Makes sense, a 5.
--SA
Manas Bhardwaj 15-Jun-12 3:27am    
thanx!
The .Text values type have to be same with Table or object datatype

C#
_with1 = person;
_with1.classworkfcs = txtFcs_classwork.Text;
                _with1.iafcs = txtFcs_ia.Text;
                _with1.examsfcs = txtFcs_exams.Text;
                _with1.totalsfcs = txtFcs_totals.Text;
                _with1.gradingfcs = txtFcs_grading.Text;


www.Tanvtech.com
 
Share this answer
 
v2
C#
//this is what i used. its that simple

 if ((!string.IsNullOrEmpty(txtFcs_classwork.Text)) && (!string.IsNullOrEmpty(txtFcs_ia.Text)) && (!string.IsNullOrEmpty(txtFcs_exams.Text)))
            {
                try
                {
txtFcs_totals.Text = (Convert.ToDouble(txtFcs_classwork.Text) + Convert.ToDouble(txtFcs_ia.Text) + Convert.ToDouble(txtFcs_exams.Text)).ToString();
                    double totalsFcs = Convert.ToDouble(txtFcs_totals.Text);

                    if (totalsFcs >= 75)
                    {
                        txtFcs_grading.Text = "   A";
                    }
                    else if (totalsFcs >= 70)
                    {
                        txtFcs_grading.Text = "   B";
                    }
                    else if (totalsFcs >= 65)
                    {
                        txtFcs_grading.Text = "   C";
                    }
                    else if (totalsFcs >= 55)
                    {
                        txtFcs_grading.Text = "   D";
                    }
                    else if (totalsFcs >= 45)
                    {
                        txtFcs_grading.Text = "   E";
                    }
                    else
                    {
                        txtFcs_grading.Text = "   F";
                    }



                }
                catch (System.FormatException fmtE)
                {
                    MessageBox.Show(fmtE.Message);
                }

            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900