473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Q: Primary Key

Hi

I have a problem with the values of a primary key in a table.

I am deleting all the rows of an existing data table in a database by using
code:

For Each x As DataRow In myDataSet.Tables(0)
x.Delete()
Next

myDataAdaptor.Update(myDataSet.Tables(0))

This works fine, HOWEVER, when I add more rows later on to this table, the
index ID does not start from 1, rather it appears that it is working from
the last value of the set of rows deleted earlier.

I guess there must be some command I'm missing to re-set the ID counter.

Can anybody help?

Thanks in advance

Geoff
Nov 20 '05 #1
10 1036
Geoff,

Never attach a question to a question, the Original Poster would think that
you are giving and answer.

A dataset is a disconnected so it just start adding autokeys from the latest
it had when you not are using the autoincrement seed.

The best is to set the seed to -1 and the increment as well.

http://msdn.microsoft.com/library/de...ementtopic.asp

I hope this helps?

Cor
Nov 20 '05 #2
Apologies Cor for posting in the above. Only after sending did I realise
that the first responder had not stayed in the same thread.

I'll have a look at your link.

Thanks

Geoff

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:eq****************@TK2MSFTNGP10.phx.gbl...
Geoff,

Never attach a question to a question, the Original Poster would think that you are giving and answer.

A dataset is a disconnected so it just start adding autokeys from the latest it had when you not are using the autoincrement seed.

The best is to set the seed to -1 and the increment as well.

http://msdn.microsoft.com/library/de...ementtopic.asp
I hope this helps?

Cor

Nov 20 '05 #3
Hi Cor

Err, I'm afraid I don't follow you:

"A dataset is a disconnected so it just starts adding autokeys from the
latest
it had when you not are using the autoincrement seed.

The best is to set the seed to -1 and the increment as well."

I understand that a dataset is disconnected. I'm deleting all the records in
the database itself by deleting the records in the dataset and then using
Update to transfer the changes to the database itself. I'm also, as a matter
of interest, deleting all the records in the DataSet afterwards using
RemoveAt.

However, as I said in the first post, when I add a record to the DataSet
(which in a DataGrid has the ID of 1 - which is what I expect) and then
Update it to the database, the ID is not 1! Instead, it is one more than the
number of records deleted initially. So, IMHO, the databases ID does not
know that the earlier records have been deleted.

Does this make sense?

Geoff
Nov 20 '05 #4
Hi Geoff,

What you write is what it does to me as well. However, I did not investigage
it.

Do you have this simple code, with that you can see what is in your dataset
at any moment. (You need to have somewhere a textbox on your form otherwise
it is so difficult to read)

Dim sw As New System.IO.StringWriter
ds.WriteXml(sw, XmlWriteMode.WriteSchema)

Me.txtTekst.Text = sw.ToString

Cor


Nov 20 '05 #5
Hi Cor

Do you mean that you get the same thing?

Maybe the question I should be asking is how to delete all the entries in a
table of a database. Maybe it is the methods I am currently using that are
causing the problems?

Geoff

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:O$**************@TK2MSFTNGP09.phx.gbl...
Hi Geoff,

What you write is what it does to me as well. However, I did not investigage it.

Do you have this simple code, with that you can see what is in your dataset at any moment. (You need to have somewhere a textbox on your form otherwise it is so difficult to read)

Dim sw As New System.IO.StringWriter
ds.WriteXml(sw, XmlWriteMode.WriteSchema)

Me.txtTekst.Text = sw.ToString

Cor

Nov 20 '05 #6
Hi Geoff,

I never had this as a problem, however did I not show you how to create a
table?

When I had this problem I would drop the table and create the table new.

http://msdn.microsoft.com/library/de...de-dz_7uud.asp

I hope we get something further?

Cor
Nov 20 '05 #7
"Geoff Jones" <ge***@NODAMNSPAM.com> wrote in
news:41***********************@news.dial.pipex.com :
However, as I said in the first post, when I add a record to the
DataSet (which in a DataGrid has the ID of 1 - which is what I expect)
and then Update it to the database, the ID is not 1! Instead, it is
one more than the number of records deleted initially. So, IMHO, the
databases ID does not know that the earlier records have been deleted.


What database are you using?

If you're using SQL Server look here:
http://msdn.microsoft.com/library/de...l=/library/en-
us/tsqlref/ts_dbcc_5lv8.asp
Anyhow, why reseed? The PK number should not matter...

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #8
Geoff

If you are using SQL Server:

Try using Truncate Table. This clears all the records, and resets the
identity column to its seed value, which is usually 1. Truncate is
quicker than dropping a table and creating a new one, though the result
is the same.

To use Truncate Table use SQLCommand.ExecuteNonQuery with the
SQLCommand.CommandText = "Truncate Table <mytable>".

James

Cor wrote :
Hi Geoff,

I never had this as a problem, however did I not show you >how to create atable?

When I had this problem I would drop the table and create >the table new.
http://msdn.microsoft.com/library/default.asp?>url=/library/en-us/tsqlr ef/ts_de-dz_7uud.asp
I hope we get something further?

Cor

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #9
Hi James

Unfortunately, I'm using Access. Is there an equivalent?

Geoff

"James R" <an*******@devdex.com> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...
Geoff

If you are using SQL Server:

Try using Truncate Table. This clears all the records, and resets the
identity column to its seed value, which is usually 1. Truncate is
quicker than dropping a table and creating a new one, though the result
is the same.

To use Truncate Table use SQLCommand.ExecuteNonQuery with the
SQLCommand.CommandText = "Truncate Table <mytable>".

James

Cor wrote :
Hi Geoff,

I never had this as a problem, however did I not show you >how to

create a
table?

When I had this problem I would drop the table and create >the table

new.

http://msdn.microsoft.com/library/default.asp?>url=/library/en-us/tsqlr

ef/ts_de-dz_7uud.asp

I hope we get something further?

Cor

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #10
Hi Lucas

I'm using Access.

Geoff

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"Geoff Jones" <ge***@NODAMNSPAM.com> wrote in
news:41***********************@news.dial.pipex.com :
However, as I said in the first post, when I add a record to the
DataSet (which in a DataGrid has the ID of 1 - which is what I expect)
and then Update it to the database, the ID is not 1! Instead, it is
one more than the number of records deleted initially. So, IMHO, the
databases ID does not know that the earlier records have been deleted.


What database are you using?

If you're using SQL Server look here:
http://msdn.microsoft.com/library/de...l=/library/en-
us/tsqlref/ts_dbcc_5lv8.asp
Anyhow, why reseed? The PK number should not matter...

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John Simmons | last post by:
How is it that even though I have the column "username" in my database set as a Primary key, using my PHP script to add new users to the database works without any errors even when signing up using...
5
by: Ghulam Farid | last post by:
Hi i have a table with primary key defined on col1 and col2. now i want to have col3 also included in primary key. when i alter the table it gives me error for duplicate rows. there is an option...
4
by: Mavis Tilden | last post by:
Hi all, So I've been reading the newsgroups, and reading a few books trying to learn SQL and SQL Server 2000. The books tell me I need a Primary Key, and that every table should have one. I know...
9
by: 101 | last post by:
Taking a course on SQL. They are saying you can get better performance by having multiple files for a group. They then graphically show an example of "Primary" with multiple data files. I have...
4
by: serge | last post by:
I ran into a table that is used a lot. Well less than 100,000 records. Maybe not a lot of records but i believe this table is used often. The table has 26 fields, 9 indexes but no Primary Key at...
5
by: shenanwei | last post by:
I have a primary server and backup server located in different physical sites. The primary server is live and ship logs to backup site every 5 minutes. The primary server is being full online...
4
by: misscrf | last post by:
Ok I have 2 issues. 1) I have a main candidate form with 2 subforms on a tab control: http://www.geocities.com/misscrf/images/contactcontinouscheckbox.jpg I have been encouraged to add these...
18
by: Thomas A. Anderson | last post by:
I am a bit confused in creating a composite primary key. I have three table with two of the tables containing primary keys. I have two of the tables (each with a primary key) having one to many...
8
by: Challenge | last post by:
Hi, I got error, SQL1768N Unable to start HADR. Reason code = "7", when I tried to start hadr primary database. Here are the hadr configuration of my primary db: HADR database role ...
4
by: Peter | last post by:
I am interested in informed feedback on the use of Constraints, Primary Keys and Unique. The following SQL statement creates a Bands tables for a database of bookings Bands into Venues, where the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.