PC Review


Reply
Thread Tools Rate Thread

Autonumber Primary key value

 
 
Nathan Carroll
Guest
Posts: n/a
 
      13th Sep 2004
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();
}


 
Reply With Quote
 
 
 
 
Nathan Carroll
Guest
Posts: n/a
 
      13th Sep 2004
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" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> 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();
> }
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the primary key in an autonumber Clarkey Microsoft Access Form Coding 2 11th Dec 2009 08:29 PM
Autonumber as primary key J E Jensen Microsoft Access Database Table Design 5 10th Sep 2009 04:30 AM
Autonumber primary key Annie Microsoft Access 4 16th Feb 2009 01:58 AM
Data Primary key vs. Artificial (Autonumber) primary key M. Microsoft Access Database Table Design 78 14th Dec 2007 10:10 AM
Primary Key Autonumber Microsoft Access Form Coding 4 16th Jun 2004 10:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:28 PM.