错误输入字符串的格式不正确 [英] error Input string was not in a correct format

查看:91
本文介绍了错误输入字符串的格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,负责添加学生的课堂作业,IA,考试并给出结果.根据结果​​生成成绩.我的cdoes工作正常,但是当我单击保存按钮输入字符串的格式不正确"时,我一直收到此错误消息,请帮帮我

表格后面的代码

 私有  void  btnSave_Click(对象发​​件人,EventArgs e)
        {
            SqlConnection conn =  SqlConnection(ConfigurationSettings.AppSettings [" ]);
            conn.ConnectionString = " ;

            // 保存数据
            尝试
            {

                clsStudentaccademics person =  clsStudentaccademics();

                变量
                _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(" );

            }
            捕获(例外)
            {
                MessageBox.Show(ex.Message);
            }
        } 




计算的代码

 私有 无效 txtFcs_exams_TextChanged(对象发​​件人,EventArgs e)
        {
            尝试
            {
                 double  classworkFcs,iaFcs,examsFcs,totalsFcs;

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


                totalsFcs =课堂工作Fcs + iaFcs +考试Fcs;
                txtFcs_totals.Text = totalsFcs.ToString();

                如果(总计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);
            }


        } 




类中的代码

 公共 对象 classworkfcs
       {
           获取 {返回 fcs_classwork; }
            set  {fcs_classwork = Convert.ToDouble( value ); }
       }

       公共 对象 iafcs
       {
           获取 {返回 fcs_ia; }
            set  {fcs_ia = Convert.ToDouble( value ); }
       }

       公共 对象 examsfcs
       {
           获取 {返回 fcs_exams; }
            set  {fcs_exams = Convert.ToDouble( value ); }
       }

       公共 对象 totalsfcs
       {
           获取 {返回 fcs_totals; }
            set  {fcs_totals = Convert.ToDouble( value ); }
       }
       公共 对象 gradingfcs
       {
           获取 {返回 fcs_grading; }
            set  {fcs_grading =  value  .ToString(); }
       } 

解决方案

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

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

            }


这篇关于错误输入字符串的格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆