以下代码尝试从datagridview中删除选定的行并更新数据库。CommandText属性尚未初始化
但它不更新数据库......它只是发出错误,“CommandText属性尚未初始化”。 ...有任何想法吗?我认为这是因为它一开始并没有受到束缚,但在这一点上我很无能,而且我的头很疼。
private void deleteRow()
{
DialogResult dr = MessageBox.Show("Are you sure you want to delete this row?", "Confirmation",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
while (dataGridView1.SelectedRows.Count > 0)
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
try
{
this.Validate();
this.tradesBindingSource.EndEdit();
this.tradesTableAdapter.Update(this.tradesDataSet.Trades);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred during the update process: " + ex);
// Add code to handle error here.
}
this.tradesTableAdapter.Fill(this.tradesDataSet.Trades); // refresh table
}
}
根本原因可能在于您的Connection和SqlCommand实例化和配置的代码之外。你可以将这些发布到你的问题中吗? – 2010-01-20 04:33:11
我不使用任何SQL命令...只是数据集和tableadapter ... – Woody 2010-01-20 04:45:22
您的数据适配器中的更新命令未设置。所以它不知道如何更新。它是自动生成的,但有时候设计师可能会搞砸并从设计器代码文件中删除它。 – affan 2010-01-20 04:48:54