2012-02-24 21 views
1

私はデータベースを必要とするプロジェクトに取り組んでいます。
私はYouTubeの中で見てきたチュートリアルを発見した、それはSQL Server 2005の
とデータを接続して編集する方法を示しています。しかし、私はそれをしようとしたとき、私はこのエラーにInvalidOperationExceptionc#sql update - InvalidOperationException

を受ける。これは、全体で編集コード

con.Open(); 

DataTable dt = new DataTable(); 
//load all records from sample table 
SqlDataAdapter da = new SqlDataAdapter("select * from sampleEdit where ID=" + 
    textBox1.Text + " ", con); 
da.Fill(dt); 

//start the editing of the selected record 
dt.Rows[0].BeginEdit(); 

dt.Rows[0][1] = textBox2.Text; 

//stop the editing 
dt.Rows[0].EndEdit(); 

//declare the sql commandbuilder that allow saving of records 
SqlCommandBuilder cb = new SqlCommandBuilder(da); 

//update the database 
da.Update(dt); 

//close the connection 
con.Close(); 

//call the method that display the record to the gridview 
displayRecords(); 

エラーが更新部分に表示されます。
何が問題になるはずですか?

これは、あなたが最も可能性の高いテーブルの主キーを定義していないので、完全な例外エラー

System.InvalidOperationException: Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information. 
at System.Data.Common.DbDataAdapter.UpdatingRowStatusErrors(RowUpdatingEventArgs rowUpdatedEvent, DataRow dataRow) 
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) 
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping) 
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable) 
at DatabaseConnect3.Form1.btnEdit_Click(Object sender, EventArgs e) 
+1

スローされた例外の全文を含めると便利です。また、YouTubeの動画が一般公開されている場合は、YouTube動画をリンクすることも害はありません。 – Guvante

+0

@Guvante、申し訳ありません、私はリンクを忘れました。ちょうどビデオをダウンロードした –

答えて

2

はあなたのソーステーブルが無効ですでした。 ID列を表の主キーにするだけでコードが機能するはずです。

+0

それは動作します。ありがとうございました –