Groups | Blog | Home
all groups > dotnet ado.net > january 2007 >

dotnet ado.net : Restoring DataColumn.AutoIncrement value



Mario Vázquez
1/11/2007 12:37:34 PM
Hi All,

I'm deriving the AutoIncrement, AutoIncrementSeed and AutoIncrementStep
properties of DataColumn objects by inspecting the underlying properties of
the source tables in Sql-Server.
When I create and empty row of that table, ado.net generates a new identity
value with the apropiate seed and step.
But, if this new row is not finally saved on the database, I would like to
restore the autoincrement value to its previous state.
How achive this?
Where are stored these generated vales?

Thanks in advance,
Mario Vazquez

Mario Vázquez
1/11/2007 1:45:39 PM
This is not much elegant, but it works:

public static void RestoreAutoIncrementValue(DataColumn autonumericColumn)

{

long step = autonumericColumn.AutoIncrementStep;

DataTable table = autonumericColumn.Table;

DataRow dtr;

autonumericColumn.AutoIncrementStep = -(step);

dtr = table.NewRow();

table.Rows.Add(dtr);

table.Rows.Remove(dtr);

autonumericColumn.AutoIncrementStep = step;

}


"Mario Vázquez" <alguien@microsoft.com> escribió en el mensaje
news:eKSxiTXNHHA.1816@TK2MSFTNGP06.phx.gbl...
[quoted text, click to view]

AMDRIT
1/11/2007 2:52:59 PM
I normally set my initial seed and step to -1, then if I roll back a row or
delete a new row the negative numbers do not confuse anyone (i.e. negative
numbered rows are uncommitted new rows.) Once these rows are committed to
the database, the identities are updated by the database.

The reset autoincrement is only a viable solution when you are rolling off
the last row in the list, and then when you are not asynchronous. Gaps
shouldn't have an impact on client side data with new rows.


[quoted text, click to view]

AddThis Social Bookmark Button