473,416 Members | 1,766 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,416 software developers and data experts.

What to do with AUTO-ID column when I add a new record to the data

I retrieve a table with only 2 columns. One is a auto-generated primary key
column and the 2nd is a string. When I add a new row to the dataset to be
updated back to the database. What should I do with the 1st column ? (Below
I have a "1" in place for now). Also, Does the datase.AcceptChanges();
updates the changes to the database? Which command do I use to update the
changes in dataset back to the Access database table? Thanks, Alpha

dtSI.Rows.Add(new Object[] {1, txtNewitem.Text});
dsServiceItems1.AcceptChanges();
Nov 17 '05 #1
6 5042
Usefula ADO.NET article:

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

Excerpt from above article:

Avoiding Auto-Increment Value Conflicts
Like most data sources, the DataSet enables you to identify columns that
automatically increment their value when new rows are added. When using
auto-increment columns in a DataSet, with auto-increment columns from a data
source, avoid conflicts between the local numbering of rows added to the
DataSet and rows added to the data source.

For example, consider a table with an auto-incrementing primary key column
of CustomerID. Two new rows of customer information are added to the table
and receive auto-incremented CustomerID values of 1 and 2. Then, only the
second customer row is passed to the Update method of the DataAdapter, the
newly added row receives an auto-incremented CustomerID value of 1 at the
data source, which does not match the value 2, in the DataSet. When the
DataAdapter fills the second row in the table with the returned value, a
constraint violation occurs because the first customer row already has a
CustomerID of 1.

To avoid this behavior, it is recommended that, when working with
auto-incrementing columns at a data source and auto-incrementing columns in a
DataSet, you create the column in the DataSet with an AutoIncrementStep of -1
and an AutoIncrementSeed of 0, as well as ensuring that your data source
generates auto-incrementing identity values starting from 1 and incrementing
with a positive step value. As a result, the DataSet generates negative
numbers for auto-incremented values that do not conflict with the positive
auto-increment values generated by the data source. Another option is to use
columns of type Guid instead of auto-incrementing columns. The algorithm that
generates Guid values should never generate the same Guid in the DataSet as
is generated by the data source.

If your auto-incremented column is used simply as a unique value, and does
not have any meaning, consider using Guids instead of auto-incrementing
columns. They are unique and avoid the extra work necessary to work with
auto-incremented columns.

"Alpha" wrote:
I retrieve a table with only 2 columns. One is a auto-generated primary key
column and the 2nd is a string. When I add a new row to the dataset to be
updated back to the database. What should I do with the 1st column ? (Below
I have a "1" in place for now). Also, Does the datase.AcceptChanges();
updates the changes to the database? Which command do I use to update the
changes in dataset back to the Access database table? Thanks, Alpha

dtSI.Rows.Add(new Object[] {1, txtNewitem.Text});
dsServiceItems1.AcceptChanges();

Nov 17 '05 #2
I need to do this for both listbox controls and datagrid control. In the DB
there is a primary key, auto-genrated, for each of these tables. I bring
that key along with other columns into dataset. The key has to be part of
the dataset so I know which record the users have selected or udpated. I
also want to allow users to add additonal records(rows) to the controls. Is
there a easier way to do this? It sees like a lot of work just to add some
records. Doesn't the dataset automatically know to increment the primary key
column or should I increment that myself?

"Richard" wrote:
Usefula ADO.NET article:

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

Excerpt from above article:

Avoiding Auto-Increment Value Conflicts
Like most data sources, the DataSet enables you to identify columns that
automatically increment their value when new rows are added. When using
auto-increment columns in a DataSet, with auto-increment columns from a data
source, avoid conflicts between the local numbering of rows added to the
DataSet and rows added to the data source.

For example, consider a table with an auto-incrementing primary key column
of CustomerID. Two new rows of customer information are added to the table
and receive auto-incremented CustomerID values of 1 and 2. Then, only the
second customer row is passed to the Update method of the DataAdapter, the
newly added row receives an auto-incremented CustomerID value of 1 at the
data source, which does not match the value 2, in the DataSet. When the
DataAdapter fills the second row in the table with the returned value, a
constraint violation occurs because the first customer row already has a
CustomerID of 1.

To avoid this behavior, it is recommended that, when working with
auto-incrementing columns at a data source and auto-incrementing columns in a
DataSet, you create the column in the DataSet with an AutoIncrementStep of -1
and an AutoIncrementSeed of 0, as well as ensuring that your data source
generates auto-incrementing identity values starting from 1 and incrementing
with a positive step value. As a result, the DataSet generates negative
numbers for auto-incremented values that do not conflict with the positive
auto-increment values generated by the data source. Another option is to use
columns of type Guid instead of auto-incrementing columns. The algorithm that
generates Guid values should never generate the same Guid in the DataSet as
is generated by the data source.

If your auto-incremented column is used simply as a unique value, and does
not have any meaning, consider using Guids instead of auto-incrementing
columns. They are unique and avoid the extra work necessary to work with
auto-incremented columns.

"Alpha" wrote:
I retrieve a table with only 2 columns. One is a auto-generated primary key
column and the 2nd is a string. When I add a new row to the dataset to be
updated back to the database. What should I do with the 1st column ? (Below
I have a "1" in place for now). Also, Does the datase.AcceptChanges();
updates the changes to the database? Which command do I use to update the
changes in dataset back to the Access database table? Thanks, Alpha

dtSI.Rows.Add(new Object[] {1, txtNewitem.Text});
dsServiceItems1.AcceptChanges();

Nov 17 '05 #3

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:A1**********************************@microsof t.com...
I need to do this for both listbox controls and datagrid control. In the DB there is a primary key, auto-genrated, for each of these tables. I bring
that key along with other columns into dataset. The key has to be part of
the dataset so I know which record the users have selected or udpated. I
also want to allow users to add additonal records(rows) to the controls. Is there a easier way to do this? It sees like a lot of work just to add some records. Doesn't the dataset automatically know to increment the primary key column or should I increment that myself?


The problem is that the database is generating the key, not the DataSet. So
when you insert a record the DataSet would need to re-read the record to get
the key. This is messy, messy, messy - and it's not DB independant. If you
have any control over the database (and specifically key generation) then I
would recommend you change it (either use a stored procedure in the DB to
generate the key or use GUIDs).
Nov 17 '05 #4
Ok, what if I don't bring in the primary key into the dataset because I have
another column that is unique contrained. Now can I just use the update from
the dbadapter to insert the records back to the database? Will that work? I
mean, shouldn't the database generate the key for the new record but then
it's not passed back to the dataset which has it's own indexing, right?

Thanks, Alpha

"Scott Roberts" wrote:

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:A1**********************************@microsof t.com...
I need to do this for both listbox controls and datagrid control. In the

DB
there is a primary key, auto-genrated, for each of these tables. I bring
that key along with other columns into dataset. The key has to be part of
the dataset so I know which record the users have selected or udpated. I
also want to allow users to add additonal records(rows) to the controls.

Is
there a easier way to do this? It sees like a lot of work just to add

some
records. Doesn't the dataset automatically know to increment the primary

key
column or should I increment that myself?


The problem is that the database is generating the key, not the DataSet. So
when you insert a record the DataSet would need to re-read the record to get
the key. This is messy, messy, messy - and it's not DB independant. If you
have any control over the database (and specifically key generation) then I
would recommend you change it (either use a stored procedure in the DB to
generate the key or use GUIDs).

Nov 17 '05 #5

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:AB**********************************@microsof t.com...
Ok, what if I don't bring in the primary key into the dataset because I have another column that is unique contrained. Now can I just use the update from the dbadapter to insert the records back to the database? Will that work?


Presumably, yes. But then why have an auto-generated primary key?
Nov 17 '05 #6
I was going to use the auto-generated key for uniquely identifying each
record in database. I did run into another column where it also needs to be
unique but it's entered by user so I put a unique constrain on it too. I'm
more comfortable using the auto-key for query and identifying records but for
this case I think this is just what I have to do to accomodat the ability for
users to add records. Thanks for your help. I'll give it a try.

But why the database wasn't updated when I use the
dbadapater.update(dataset, tablename) when the application closed to save the
changes I made in the datagrid. Did I forget to do somehting else?
Thanks.

"Scott Roberts" wrote:

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:AB**********************************@microsof t.com...
Ok, what if I don't bring in the primary key into the dataset because I

have
another column that is unique contrained. Now can I just use the update

from
the dbadapter to insert the records back to the database? Will that work?


Presumably, yes. But then why have an auto-generated primary key?

Nov 17 '05 #7

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

Similar topics

5
by: Robert Downes | last post by:
I'm using the following in a page that I'm testing in Mozilla: p.actionLinkBlock {border: 1px #000000 dashed; padding: 0.2cm; width: auto} But the dashed border is extending to the right-edge...
1
by: Theodore A. Jencks | last post by:
Hi all, I posted a question about a side navigation bar earlier where I was encountering this problem. I thought I'd repost the question in a more general format because I'm not sure people...
7
by: Ugrasena via DBMonster.com | last post by:
Hi team i am using DB2 7.2 on Windows2000. We have lot many SQL Procedure Calls, is there any way i can Turn off the Auto Commit on that Database, the Problem i have is many of our Application...
20
by: Vijay Kumar R. Zanvar | last post by:
Hello, Unlike register, auto keyword can not be used to declare formal parameter(s). Is there any specific reason for this? Kind regards, Vijay Kumar R. Zanvar
5
by: maya | last post by:
at work they decided to center divs thus: body {text-align:center} #content {width: 612px; text-align:left; margin: 0 auto 0 auto; } this works fine in IE & FF, EXCEPT in FF it doesn't work if...
2
by: Ehud Banai | last post by:
Hi All, Is there any script or dll that allows me to do auto correction to digital pictures from PHP? Thanks in advance, Ehud
4
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... Following the samples online, we implemented a windows service in .Net. Like most services, there's a worker thread actually doing stuff in the background. The problem we've run into is...
22
by: nospam_news | last post by:
I currently get asked about my usage of "auto". What is it for? The keyword is clearly superflous here. In contrast to the huge majority of C/C++ developers I write definitions very explicitly...
13
by: LC's No-Spam Newsreading account | last post by:
I have happily lived for 15 years (our first web server started in 1993) without CSS, but I now decided to learn and experiment with it. My personal preference has always been that the width of...
21
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.