"指定演员表无效" WinForm应用程序出错

时间:2015-12-13 18:02:13

标签: c# mysql winforms

我正在使用Microsoft Visual Studio社区版本2015.关于这个问题有很多问题,但我的问题并未完全涵盖在内。基本上我在Windows Form Application(C#)中使用MySql(phpmyadmin - wamp)数据库。我正在创建一个简单的Windows窗体应用程序来获取输入,更新/删除数据,显示数据网格视图中的元素和搜索任何记录。我在更新记录时遇到问题。编译器不断给我同样的错误,那就是; "指定演员表无效"。我的守则如下:

private void button_update(object sender, EventArgs e)
{
try
   { 
    MySqlConnection cn = new MySqlConnection("server=localhost;port=3306;database=hyder;uid=root;Encrypt=false;AllowUserVariables=True;Usecompression=True;");
    int id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
    cn.Open();
    MySqlCommand cmd = new MySqlCommand();
    cmd.Connection = cn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "update record set r_name='" + textBox1.Text + "',r_address='" + textBox2.Text + "',r_phone='" + textBox3.Text + "',r_comment='" + textBox4.Text + "' where r_id='" + id;
    cmd.ExecuteNonQuery();
    MessageBox.Show("Record Updated!");
    cn.Close();
 }
catch (Exception x) { MessageBox.Show("Error:" + x.Message); }
}

此代码有什么问题?

1 个答案:

答案 0 :(得分:0)

尝试替换

int id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;

int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);