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

dotnet ado.net : DataSet auto increment problem


Mark
5/29/2007 11:02:01 AM
Hi Everyone:
I have set up a dataset that uses an auto increment field that is set to
start at minus one and step by minus one when I add a new record to the
dataset.

Example dataset:
1. Existing Record 1
2. Existing Record 2
3. Existing Record 3
4. Existing Record 4
5. Existing Record 5

The ID field is an auto increment field in SQL Server. To prevent accidental
data corruption, whenever I add a new record to the dataset I start with -1.
So the dataset will look like this when I add new records:

1. Existing Record 1
2. Existing Record 2
3. Existing Record 3
4. Existing Record 4
5. Existing Record 5
-1. 1st New Record
-2. 2nd New Record
-3. 3rd New Record

Here is my problem when I try to save the new record into the dataset I get
the following error:
“Constraint Exception was unhandled. Column ‘ID’ is constrained to be
unique. Value ‘4’ is already present.”

I don’t understand why this is happening. The dataset’s auto increment field
should put a -1 on the new record I’m adding. Instead it looks like it is
starting at the last record ‘5’, subtracts one to get four and is trying to
place the new information in record four instead of creating a new record in
the dataset and starting the ID number at -1.

What is going on?

Thank you,

--
David Thielen
5/29/2007 3:18:06 PM
A lot of databases will only use your start value if there are no values
greater than it. If there are greater values, it will go one over that so
that it can always just do an inc for each new record.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




[quoted text, click to view]
RobinS
5/30/2007 12:04:43 AM
Make sure that your update query is not trying to insert or update that
column. That's the only reason I can think of for it to do this to you.

Robin S.
---------------------------------------
[quoted text, click to view]

AddThis Social Bookmark Button