Autonumber Primary key value

N

Nathan Carroll

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();
}
 
N

Nathan Carroll

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top