C# 无法强制转换类型为';system.windows.forms.textbox';输入';System.IConvertible';错误

C# 无法强制转换类型为';system.windows.forms.textbox';输入';System.IConvertible';错误,c#,mysql,C#,Mysql,我知道我的代码很糟糕,但我仍在学习,但我不明白这条信息,有人能解释我哪里出错了吗 private void btnCalculate_Click(object sender, EventArgs e) { //These are my variables which will take form of the values from the database and display whatever the user has input int iDay1, iDay2, iDa

我知道我的代码很糟糕,但我仍在学习,但我不明白这条信息,有人能解释我哪里出错了吗

private void btnCalculate_Click(object sender, EventArgs e)
{
    //These are my variables which will take form of the values from the database and display whatever the user has input
    int iDay1, iDay2, iDay3, iDay4, iDay5, iDay6, iDay7;
    int iScore;
    iDay1 = Convert.ToInt32(txtBox1);
    iDay2 = Convert.ToInt32(txtBox2);
    iDay3 = Convert.ToInt32(txtBox3);
    iDay4 = Convert.ToInt32(txtBox4);
    iDay5 = Convert.ToInt32(txtBox5);
    iDay6 = Convert.ToInt32(txtBox6);
    iDay7 = Convert.ToInt32(txtBox7);
    iScore = Convert.ToInt32(iDay1 + iDay2 + iDay3 + iDay4 + iDay5 + iDay6 + iDay7);
    string Query = "insert into sql370447.calorie (day1, day2, day3, day4, day5, day6, day7) values('" + txtBox1.Text + "','" + txtBox2.Text + "','" + txtBox3.Text + "','" + txtBox4.Text + "','" + txtBox5.Text + "','" + txtBox6.Text + "','" + txtBox7.Text + "');";
    MySqlConnection myConn = new MySqlConnection(connStr);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
    MySqlDataReader myReader;
    myConn.Open();
    myReader = cmdDataBase.ExecuteReader();
    //A simple If statement to determine wether the user is healthy or not the users results will change depending on gender
    if (iScore >= 8000 && iScore <= 10000)
    {
        MessageBox.Show("Congratulations you are a Healthy male, your calorie intake is " + iScore);
    }
    else if (iScore >= 10001)
    {
        MessageBox.Show("You are eating more than you should be you are an unhealthy male, your calorie intake is " + iScore);
    }
    else
    {
        MessageBox.Show("You are eating way less than the healthy amount, you are an unhealthy male atm your calorie intake is " + iScore);
    }
}
private void btnCalculate\u单击(对象发送者,事件参数e)
{
//这些是我的变量,它将采用数据库中的值的形式,并显示用户输入的任何内容
国际iDay1、iDay2、iDay3、iDay4、iDay5、iDay6、iDay7;
内部核心;
iDay1=转换为32(txtBox1);
iDay2=转换为32(TXTX2);
iDay3=转换为32(txtBox3);
iDay4=转换为32(txtBox4);
iDay5=转换为32(txtBox5);
iDay6=转换为32(txtBox6);
iDay7=转换为32(txtBox7);
iScore=转换为32(iDay1+iDay2+iDay3+iDay4+iDay5+iDay6+iDay7);
string Query=“插入sql370447.carorie(第1天、第2天、第3天、第4天、第5天、第6天、第7天)值(“+txtBox1.Text+”、“+txtBox2.Text+”、“+txtBox3.Text+”、“+txtBox4.Text+”、“+txtBox5.Text+”、“+txtBox6.Text+”、“+txtBox7.Text+”);”;
MySqlConnection myConn=新的MySqlConnection(connStr);
MySqlCommand cmdDataBase=新的MySqlCommand(查询,myConn);
MySqlDataReader;
myConn.Open();
myReader=cmdDataBase.ExecuteReader();
//一个简单的If语句来确定用户是否健康,用户结果将根据性别而变化
如果(iScore>=8000&&iScore=10001)
{
Show(“你吃得太多了,你是一个不健康的男性,你的卡路里摄入量是”+iScore);
}
其他的
{
MessageBox.Show(“你的饮食量远远低于健康水平,你是一个不健康的男性,你的卡路里摄入量是”+iScore);
}
}

您当前正在尝试将文本框本身转换为整数。那是行不通的。您需要转换文本框中的文本:

(等等)

不过,您绝对应该停止那样构建SQL。阅读有关参数化SQL的内容,以避免SQL注入攻击,使代码更易于阅读,并避免转换问题

你还应考虑:

  • 对所有这些文本框使用数组或其他集合,这样您就可以循环
  • 删除
    i
    前缀-特别拒绝匈牙利符号
  • 使用
    int.TryParse
    而不是
    Convert.ToInt32
    可以检测无效输入,而不会出现异常

要访问文本框中提交的文本,您需要查看属性:


警告:您的代码极易受到sql注入攻击!您需要
textboxN.Text
。您需要引用文本框的.Text属性,而不是文本框本身。…。@DanielA.White:除非这是一个kiosk应用程序,否则客户端已经可以访问
connStr
。这并不是说他不应该使用参数化查询,但在本例中,与其说是安全性,不如说是健壮性(和良好习惯)
iDay1 = Convert.ToInt32(txtBox1.Text);
iDay1 = Convert.ToInt32(txtBox1.Text);