以下代碼嘗試從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