Issues with autoincrement

timh

Well-known member
Joined
Nov 28, 2008
Messages
50
Programming Experience
Beginner
An element in a dataset I am working with is an integer datatype, with autoincrement set to "True". The idea is that this field is a unique ID for each entry in the database, which is saved as an XML file. This is not an SQL database.

If I open my application with the relevant XML loaded into the dataset, the autonumber field picks up as expected, however if I switch XML sources (load a different set of data based on the same schema) whilst the application is running, the ID field for the new entry starts from the last number of the previous dataset. I am unable to reset the autoincrement seed value manually (by resetting the autoincrement seed value etc), though I am clearing the dataset ( ds.clear() ) prior to loading in the new XML data.

Short of ditching the autoincrement feature altogether and manually allocating a unique ID to each record, is there any other way of achieving the desired result, i.e an autoincrement value that picks up from the last record? All the advice I have found seems to relate to using autoincrement with an SQL based system, which I can't seem to relate to my application.

A workaround would be to automatically restart the application when I load a new XML database, but that seems like the equivalent of using a hammer to do precision work...

Thanks,

Tim
 
DataColumn.AutoIncrementSeed Property (System.Data)

Generally without setting a seed, the column will automatically continue counting from highest existing value in table - if source is not ordered that means it will count from highest value plus number of subsequent rows. You can't set any lower seed than that.
 
DataColumn.AutoIncrementSeed Property (System.Data)

Generally without setting a seed, the column will automatically continue counting from highest existing value in table - if source is not ordered that means it will count from highest value plus number of subsequent rows. You can't set any lower seed than that.

Thanks. I understand that, but the issue is this...

Say I have a dataset loaded with 19 records. When I add a new record, the new record is given the ID number 20, which is fine.

If however I then load a new dataset, without first closing the application, with say 3 records, when I add a new record to this dataset, the new record is given the ID number 24.

When I load the new dataset, I first empty the old data by
VB.NET:
 ds.clear()

What clearing the dataset doesn't seem to do is reset the autoincrement value. I mistakenly thought that if I redefined the seed value, it would work out.

VB.NET:
 ds.Tables("entrant").Columns("ID").AutoIncrementSeed=1

I set a break point at these two lines, and found that there is a property "AutoIncrementCurrent", that holds the next number in sequence. However, this doesn't appear to be accessible to me? I don't really understand why clearing the dataset doesn't reset this value to zero, though I'm sure there's a very good reason for it.
 
Clear method only removes all data rows from table, nothing else. If you can clear all rows in all tables in the whole dataset, why can't you create a new DataSet object? DataSet.Reset method and reloading schema will also do.
 
Got there in the end thanks, although having created a new object, I also needed to recreate all the bindingsources associated with it...

Is there are a quicker way than going through each bindingsource and setting the datasource and datamember again? These were initially set in the design window.
 
Changing the datasource object will have that side effect, although DataMember is just a string in this case and don't have to be set again.

If you don't create a new object and instead do DataSet.Reset, I found the opposite to be happening, you don't set BindingSource.DataSource now since it's the same object, but table objects has changed and resetting+setting DataMember was necessary for the grid to rebind to this.
 
Back
Top