469,646 Members | 1,339 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

DataColumn Autoincrement value

Hi:

How to reset the autoincrement value generated by DataTable ?

I've master and detail table, the detail table has a autoincrement
column, each time i add a new master record, i need to reset the
detail table autoincrement column to start from 1.

I set the dataColumn AutoIncrementSeed=0 and AutoIncrementStep=1 each
time i append a new master row, but it seem not work, it still remain
the previous value, any idea ?

Thanks
JCVoon
Nov 21 '05 #1
5 7835
JC,

When the increment is 0 than you would get directly (I thought) a
concurrency vialation error.

Cor
Nov 21 '05 #2
Dim dt As New DataTable

Dim myColumn As DataColumn = New DataColumn("ID")
myColumn.ReadOnly = True
'myColumn.DataType = System.Type.GetType("System.Int32")
myColumn.DataType = GetType(Integer)
With myColumn
.AutoIncrement = True
.AutoIncrementSeed = 1
.AutoIncrementStep = 1
End With

' Add the column to a new DataTable.
dt.Columns.Add(myColumn)

Dim myKey(0) As DataColumn
myKey(0) = dt.Columns("ID")

dt.PrimaryKey = myKey

HTH,
Greg

"JC Voon" <jc*******@yahoo.com> wrote in message
news:41***************@msnews.microsoft.com...
Hi:

How to reset the autoincrement value generated by DataTable ?

I've master and detail table, the detail table has a autoincrement
column, each time i add a new master record, i need to reset the
detail table autoincrement column to start from 1.

I set the dataColumn AutoIncrementSeed=0 and AutoIncrementStep=1 each
time i append a new master row, but it seem not work, it still remain
the previous value, any idea ?

Thanks
JCVoon

Nov 21 '05 #3
Sorry! I didn't read the question fully.

I assume you detail table has a composite primary key...

MasterKey, DetailKey

100,1
100,2
100,3

101,1
101,2

102,1
102,2
102,3
102,4

etc.

If this was in SQL Server your could write a trigger to determine your
DetailKey value. This starts to get real ugly when you need to later delete
a row or insert in between values. Not sure how to do this using a
datatable.

You maybe better off using a GUID as your DetailKey. (I've had success,
using this strategy)

100, {F6C3B751-59EB-49BF-AE8F-0009097087D8}
100, {9258B3BD-4683-4DBE-822F-000B3757BACB}

101, {E39C2D34-11E4-4645-9921-00130E4AB40E}

etc.

If order is important (I assume it is, hence your desire for the sequential
numbers). You could add a nonkey column that is a timestamp or something.

Greg

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:e%******************@TK2MSFTNGP12.phx.gbl...
Dim dt As New DataTable

Dim myColumn As DataColumn = New DataColumn("ID")
myColumn.ReadOnly = True
'myColumn.DataType = System.Type.GetType("System.Int32")
myColumn.DataType = GetType(Integer)
With myColumn
.AutoIncrement = True
.AutoIncrementSeed = 1
.AutoIncrementStep = 1
End With

' Add the column to a new DataTable.
dt.Columns.Add(myColumn)

Dim myKey(0) As DataColumn
myKey(0) = dt.Columns("ID")

dt.PrimaryKey = myKey

HTH,
Greg

"JC Voon" <jc*******@yahoo.com> wrote in message
news:41***************@msnews.microsoft.com...
Hi:

How to reset the autoincrement value generated by DataTable ?

I've master and detail table, the detail table has a autoincrement
column, each time i add a new master record, i need to reset the
detail table autoincrement column to start from 1.

I set the dataColumn AutoIncrementSeed=0 and AutoIncrementStep=1 each
time i append a new master row, but it seem not work, it still remain
the previous value, any idea ?

Thanks
JCVoon


Nov 21 '05 #4
Greg Burns, Cor:

Thanks for the reply.
MasterKey, DetailKey

100,1
100,2
100,3

101,1
101,2

102,1
102,2
102,3
102,4
Yes this is exactly what i need
If order is important (I assume it is, hence your desire for the sequential
numbers). You could add a nonkey column that is a timestamp or something.


So it is impossible to reset the autoincrement value ?
Regards
JCVoon
Nov 21 '05 #5
> So it is impossible to reset the autoincrement value ?

AFAIK, it is impossible.

"JC Voon" <jc*******@yahoo.com> wrote in message
news:41**************@msnews.microsoft.com...
Greg Burns, Cor:

Thanks for the reply.
MasterKey, DetailKey

100,1
100,2
100,3

101,1
101,2

102,1
102,2
102,3
102,4


Yes this is exactly what i need
If order is important (I assume it is, hence your desire for the
sequential
numbers). You could add a nonkey column that is a timestamp or something.


So it is impossible to reset the autoincrement value ?
Regards
JCVoon

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by RA | last post: by
6 posts views Thread by Dennis | last post: by
4 posts views Thread by Tim | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.