且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误输入字符串的格式不正确

更新时间:2023-09-13 09:53:28

您没有说出错误的出处?我的猜测是这里出了点问题:

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



这里的代码没有错,但是数据没有错.例如:如果您的classwork_txt为空白或无法转换为double的其他字符串,您将收到当前得到的错误消息.
选项是确保您获取正确的数据.

您也可以尝试使用 TryParse [ _ 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


 // 这就是我用的.就这么简单

 如果((!string.IsNullOrEmpty(txtFcs_classwork.Text))&&(!string.IsNullOrEmpty(txtFcs_ia.Text))&&(!string. IsNullOrEmpty(txtFcs_exams.Text)))
            {
                尝试
                {
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);

                    如果(总计Fcs >  =  75 )
                    {
                        txtFcs_grading.Text = " ;
                    }
                    其他 如果(总计Fcs >  =  70 )
                    {
                        txtFcs_grading.Text = " ;
                    }
                    其他 如果(总计Fcs >  =  65 )
                    {
                        txtFcs_grading.Text = " ;
                    }
                    其他 如果(总计Fcs >  =  55 )
                    {
                        txtFcs_grading.Text = " ;
                    }
                    其他 如果(总计Fcs >  =  45 )
                    {
                        txtFcs_grading.Text = " ;
                    }
                    其他
                    {
                        txtFcs_grading.Text = " ;
                    }



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

            } 


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

 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;


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

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




codes performing the calculation

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

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(); }
       }

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

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.


The .Text values type have to be same with Table or object datatype

_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


//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);
                }

            }