PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Autonumber Primary key value

Reply

Autonumber Primary key value

 
Thread Tools Rate Thread
Old 13-09-2004, 09:04 PM   #1
Nathan Carroll
Guest
 
Posts: n/a
Default Autonumber Primary key value


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
Old 13-09-2004, 09:29 PM   #2
Nathan Carroll
Guest
 
Posts: n/a
Default Re: Autonumber Primary key value

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



  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off