错误输入字符串的格式不正确 [英] error Input string was not in a correct format
问题描述
表格后面的代码
私有 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 todouble
, 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屋!