2010-01-20 314 views
1

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

根本原因可能在於您的Connection和SqlCommand實例化和配置的代碼之外。你可以將這些發佈到你的問題中嗎? – 2010-01-20 04:33:11

+0

我不使用任何SQL命令...只是數據集和tableadapter ... – Woody 2010-01-20 04:45:22

+3

您的數據適配器中的更新命令未設置。所以它不知道如何更新。它是自動生成的,但有時候設計師可能會搞砸並從設計器代碼文件中刪除它。 – affan 2010-01-20 04:48:54

回答

0

我刪除了我的表適配器,數據集,等...並重新創建整個表。用新數據填充它,並嘗試刪除一行。它的工作......不知道爲什麼,但它現在起作用。

+1

它的工作原理是,您現在有一個分配給DataAdapter的UpdateCommand屬性的命令。 – AMissico 2010-03-02 13:23:24

+0

是的,在DataSet設計器中,單擊FillBy查詢並查看屬性窗口。您應該看到選擇,更新,插入和刪除查詢的地方。它可以自動填充這些內容,並且在大多數情況下它很好,但請注意! – cjb110 2013-02-13 08:11:28

相關問題