Restoring DataColumn.AutoIncrement value

M

Mario Vázquez

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
 
M

Mario Vázquez

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;

}
 
A

AMDRIT

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.
 

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