2012-02-24 4 views
1

데이터베이스가 필요한 프로젝트에서 작업하고 있습니다.
은 내가 유튜브에서 본 적이 튜토리얼을 발견하고 어떻게 연결하고 나는 그것을 시도 할 때 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 비디오를 연결하는 것이 좋습니다. – Guvante

+0

@Guvante, 죄송합니다. 링크를 잊어 버렸습니다. 방금 비디오를 다운로드 –

답변

2

소스 테이블이 유효 전체 예외 오류였다. ID 열을 테이블의 기본 키로 만들고 코드가 작동해야합니다.

+0

그것은 작동합니다. 고맙습니다 –