【发布时间】:2014-08-21 00:44:01
【问题描述】:
当用户修改 Windows 窗体上 DataGridView 中的单元格时,我正在尝试更新 SQL Server 数据库。我收到错误
Failed to convert parameter value from a DataGridViewTextBoxCell to a String
在“update.ExecuteNonQuery()”行上。我正在运行 Visual Studio C# Express 2008 和 SQL Server Management Studio Express 2008 R2。任何帮助将不胜感激。
private void buttonUpdate_Click(object sender, EventArgs e)
{
// Create the UpdateCommand.
SqlCommand update = new SqlCommand(
"UPDATE clients SET " +
"client_name_first = @client_name_first " +
"WHERE client_id = @modifiedRowNum;", connect);
// Add the parameters for the UpdateCommand.
update.Parameters.Add(
"@client_name_first", SqlDbType.VarChar, 50).Value =
dgvClients.Rows[dgvClients.CurrentCell.RowIndex + 1].Cells[dgvClients.CurrentCell.ColumnIndex];
update.Parameters.Add(
"@modifiedRowNum", SqlDbType.Int, 5).Value = dgvClients.CurrentCell.RowIndex + 1;
// Execute the operation.
update.ExecuteNonQuery();
}
【问题讨论】:
标签: winforms visual-studio-2008 datagridview sql-server-2008-r2