PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft ADO .NET
Autonumber Primary key value
Forums
Newsgroups
Microsoft DotNet
Microsoft ADO .NET
Autonumber Primary key value
![]() |
Autonumber Primary key value |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
Attempting to implment solution to for autonumber primary key for Access DB.
As seen below I call the add record procedure to add new record to the table. Then I update the changes (RowState.Added) to the database. This causes the Dataadapter update event: to fire which returns the TimeID from the Table and updates the appropriate row. When I try to make a change to the newly created row I get no response from the Update Data Base Procedure: until I make a second change to the row. The response I get is: System.Data.DBConcurrencyException: Concurrency violation: the UpdateCommand affected 0 records. at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet) at TimeV3.frmMain.UpdateData() in c:\vsprojects\timev4\frmmain.cs:line 1420 Dataadapter update event: private void daBillable_RowUpdated(object sender, System.Data.OleDb.OleDbRowUpdatedEventArgs e) { if (e.StatementType == StatementType.Insert) { try { System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("SELECT @@IDENTITY", e.Command.Connection); //, e.Command.Transaction); e.Row["TimeID"] = Convert.ToInt32(cmd.ExecuteScalar()); e.Row.AcceptChanges(); cmd.Dispose(); } catch(System.Exception ex) { Console.WriteLine(ex); } } } Add Procedure: private void miAddRecord_Click(object sender, System.EventArgs e) { bm.AddNew(); bm.EndCurrentEdit(); TimeV3.dsTimeBase Changes=((TimeV3.dsTimeBase)(dsTime.GetChanges(System.Data.DataRowState.Adde d))); this.daBillable.Update(Changes); } Update Data Base Procedure: private void UpdateData() { bm.EndCurrentEdit(); // deletes: try { TimeV3.dsTimeBase Changes=((TimeV3.dsTimeBase)(dsTime.GetChanges(System.Data.DataRowState.Dele ted))); if (Changes!=null) { this.daBillable.Update(Changes); } } catch(System.Exception ex) { Console.WriteLine (ex); } //Updates: try { TimeV3.dsTimeBase Changes=((TimeV3.dsTimeBase)(dsTime.GetChanges(System.Data.DataRowState.Modi fied))); if (Changes!=null) { this.daBillable.Update(Changes); } } catch(System.Exception ex) { Console.WriteLine (ex); } this.dsTime.AcceptChanges(); } |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Figured out problem with update and delete commands of the adapter. Since I
my only have on key I changed the WHERE in the sql to select for primary key only and it fixed the problem. "Nathan Carroll" <thelosthorizon@Bhutan.com> wrote in message news:#M4t1RcmEHA.2880@TK2MSFTNGP14.phx.gbl... > Attempting to implment solution to for autonumber primary key for Access DB. > As seen below I call the add record procedure to add new record to the > table. Then I update the changes (RowState.Added) to the database. This > causes the Dataadapter update event: to fire which returns the TimeID from > the Table and updates the appropriate row. When I try to make a change to > the newly created row I get no response from the Update Data Base Procedure: > until I make a second change to the row. The response I get is: > > System.Data.DBConcurrencyException: Concurrency violation: the UpdateCommand > affected 0 records. > at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, > DataTableMapping tableMapping) > at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String > srcTable) > at System.Data.Common.DbDataAdapter.Update(DataSet dataSet) > at TimeV3.frmMain.UpdateData() in c:\vsprojects\timev4\frmmain.cs:line > 1420 > > > > Dataadapter update event: > private void daBillable_RowUpdated(object sender, > System.Data.OleDb.OleDbRowUpdatedEventArgs e) > { > if (e.StatementType == StatementType.Insert) > { > try > { > System.Data.OleDb.OleDbCommand cmd = new > System.Data.OleDb.OleDbCommand("SELECT @@IDENTITY", > e.Command.Connection); > //, e.Command.Transaction); > e.Row["TimeID"] = Convert.ToInt32(cmd.ExecuteScalar()); > e.Row.AcceptChanges(); > cmd.Dispose(); > } > catch(System.Exception ex) > { > Console.WriteLine(ex); > } > } > } > > > Add Procedure: > private void miAddRecord_Click(object sender, System.EventArgs e) > { > > bm.AddNew(); > bm.EndCurrentEdit(); > TimeV3.dsTimeBase > Changes=((TimeV3.dsTimeBase)(dsTime.GetChanges(System.Data.DataRowState.Adde > d))); > this.daBillable.Update(Changes); > > } > > Update Data Base Procedure: > private void UpdateData() > { > bm.EndCurrentEdit(); > > // deletes: > try > { > TimeV3.dsTimeBase > Changes=((TimeV3.dsTimeBase)(dsTime.GetChanges(System.Data.DataRowState.Dele > ted))); > if (Changes!=null) > { > this.daBillable.Update(Changes); > } > } > catch(System.Exception ex) > { > Console.WriteLine (ex); > } > > //Updates: > try > { > TimeV3.dsTimeBase > Changes=((TimeV3.dsTimeBase)(dsTime.GetChanges(System.Data.DataRowState.Modi > fied))); > if (Changes!=null) > { > this.daBillable.Update(Changes); > } > } > catch(System.Exception ex) > { > Console.WriteLine (ex); > } > this.dsTime.AcceptChanges(); > } > > |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

