473,396 Members | 1,702 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.

auto-increment identity column

Using Visual C# 2008 Express and SQL Server 2008 Express, I would like to insert new records into database "AdventureWorks", table "Person.Contact".

To my surprise, this table's int-value identity column "ContactID" does not appear to be auto-increment. I don't know how to confirm for sure that this is so. (I have installed SQL Server Management Studio, if that helps.)

How can I confirm this, and how can I make ContactID auto-increment?

And incidentally, will auto-increment just start incrementing from the highest existing ContactID, or will it first use lesser ContactID values that are absent from the table (presumably due to deletions)?
Mar 24 '09 #1
13 37527
ck9663
2,878 Expert 2GB
Open the Management Studio. Right click on the table then choose modify. On the Identity Specification, check if the property is set to YES.


-- CK
Mar 25 '09 #2
ck9663:

In SQL Server 2008 Management Studio Express, only three options appear under Identity Specification: (Is Identity), Identity Increment and Identity Seed, which are set to Yes, 1 and 1, respectively. Identity Increment = 1 would seem to imply that auto-increment is on, but it doesn't seem to be working.

But maybe I just don't know what I'm doing. I create the new DataRow as follows:

Expand|Select|Wrap|Line Numbers
  1. DataRow row = dataTable.NewRow (); // create new row (new record)
  2. dataTable.Rows.Add (row);          // add new row to table
But at this point (and ever after, unless I assign a value in my code, which I shouldn't be doing if ContactID is supposed to auto-increment), row ["ContactID"].ToString () is just null. What else should I be doing to implement auto-increment?
Mar 26 '09 #3
ck9663
2,878 Expert 2GB
Try to do an insert on the table itself. Or maybe that it's a sample DB so microsoft only allow read-only.


-- CK
Mar 26 '09 #4
ck9663:

Try to do an insert on the table itself.
I'm not sure what you mean by "the table itself". I'm trying to modify a DataRow, but that row has already been added to the table (see code below). Here are the problems I've been having:

When I try to save a new (inserted) record via the following code:

Expand|Select|Wrap|Line Numbers
  1. DataRow row = dataTable.Rows [currRec];
  2. row.BeginEdit ();
  3.  
  4. // try { row ["ContactID"] = txtContactID.Text; }
  5. // catch (Exception exc) { MessageBox.Show (exc.Message); }
  6.  
  7. row ["Title"] = txtTitle.Text;
  8. row ["FirstName"] = txtFirstName.Text;
  9. row ["MiddleName"] = txtMiddleName.Text;
  10. row ["LastName"] = txtLastName.Text;
  11. row ["Suffix"] = txtSuffix.Text;
  12. row ["Phone"] = txtPhone.Text;
  13. row ["EmailAddress"] = txtEmailAddress.Text;
  14.  
  15. row.EndEdit ();
  16.  
  17. try { dataAdapter.Update (dataSet, "Person.Contact"); }
  18. catch (System.Runtime.InteropServices.ExternalException exc)
  19. {
  20.     MessageBox.Show (exc.Message + "\n\nSystem.Runtime.InteropServices.ExternalException");
  21. }
I get the following ExternalException message:

Cannot insert the value NULL into column 'PasswordHash', table 'AdventureWorks.Person.Contact'; column does not allow nulls. INSERT fails. The statement has been terminated.

When I uncomment

Expand|Select|Wrap|Line Numbers
  1. try { row ["ContactID"] = txtContactID.Text; }
  2. catch (Exception exc) { MessageBox.Show (exc.Message); }
and enter ContactID in my WinForm, I get the same ExternalException. With the above uncomment made, when I DON'T enter a ContactID, I get the following exception message:

Input string was not in correct format. Couldn't store <> in ContactID Column. Expected type is int32.

Or maybe that it's a sample DB so microsoft only allow read-only.
How can I tell? I looked at the table in SQL Server 2008 Management Studio Express, but I couldn't see anything pertinent.
Mar 26 '09 #5
ck9663
2,878 Expert 2GB
All your errors has nothing to do with ContactID being an IDENTITY.

Your first error happens because you're saving a NULL into a column that does not allow nulls. Your second error looks like a problem on your sql string.


--- CK
Mar 26 '09 #6
ck9663:

What I mean is: when the value of the highest-numbered ContactID is auto-incremented, where do I copy that newly-incremented value from to put it into the ContactID column of the new record that is being inserted?
Mar 26 '09 #7
ck9663
2,878 Expert 2GB
When you do an insert into a table with an IDENTITY column, you must not include that column on the column that you're trying to populate. SQL Server will handle that increment for you and will populate the IDENTITY column even if you did not specify it.


--- CK
Mar 26 '09 #8
ck9663:

OK then, when the value of the highest-numbered ContactID is auto-incremented, where do I find that newly-incremented value so I can display it onscreen? And is it not incremented until the record is actually saved to the DataSet? Because if that's so, and if I want to display it onscreen before the new record is saved, I would have to increment it myself so I can display it. Then what would be the point of auto-incrementing?
Mar 27 '09 #9
ck9663
2,878 Expert 2GB
And is it not incremented until the record is actually saved to the DataSet?
YES. Although you can capture this inside a trigger, the actual value is not incremented until it's written. You can not just handle the increment. What if your table is accessed by multiple users? Then you'll have problem who has the next value of your IDENTITY.

What are you trying to do? Maybe there are other ways of doing it.


--- CK
Mar 27 '09 #10
ck9663:

And is it not incremented until the record is actually saved to the DataSet?
YES.

Is it actually incremented when saved to the DataSet or when the app actually reconnects to the database and saves it there?

Although you can capture this inside a trigger...
Do you mean inside try and catch blocks for handling exceptions?

You can not just handle the increment. What if your table is accessed by multiple users? Then you'll have problem who has the next value of your IDENTITY.
Point taken.

What are you trying to do?
I would have liked to display the ContactID (the indentity column) for the new record as the new record is being entered by being typed in. But perhaps that will not be possible.
Mar 27 '09 #11
I'm doing a insert of a dat file into a SQL table and if the ID column is auto-incremented then I will turn this off and insert the dat file then turn back on. How would have the program check if the ID column is auto-incremented from my program. I'm doing a insert of a dat file into a SQL table and if the ID column is auto-incremented then I will turn this off and insert the dat file then turn back on.
Jul 1 '09 #12
My opinion:

Is it actually incremented when saved to the DataSet or when the app actually reconnects to the database and saves it there?

Yes. The increment is done only when new data is created or inserted.

I would have liked to display the ContactID (the indentity column) for the new record as the new record is being entered by being typed in. But perhaps that will not be possible.

I suggest you try this

connect to table and get last ContactID
increment last ContactID by 1 eg lastContactID +=1
insert new record immediately with the values typed
display your new ContactID as incrementedContactID
After record typing is complete, Update new record with complete Record where ContactID = incrementedContactID

Hope this helps.
Jul 1 '09 #13
MrMancunian
569 Expert 512MB
@owofeminew
I'd advise you to be very careful with this method, as you can't verify if lastContactID +=1 is the same as the new Identity. I think I would create a new record with null values the moment someone starts typing and return the Identity Seed from the Insert query. After that, you simply update the row by using the new identity.

Steven
Jul 15 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Manlio Perillo | last post by:
Hi. This post follows "does python have useless destructors". I'm not an expert, so I hope what I will write is meaningfull and clear. Actually in Python there is no possibility to write code...
1
by: Lew | last post by:
Hi all, I'm trying to create a page that has a user-selectable page auto-refresh option (IE 5.5). Essentially, it's a page that contains a checkbox, when the user checks the checkbox, I'd like...
1
by: Glabbeek | last post by:
I'm changing the layout of my site. Instead of using tables, I will use DIVs. It's working fine, except for 1 thing: In IE6 some DIVs are not the correct width. Mozilla and Opera are showing the...
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...
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: Samuel | last post by:
Hi, I am running into a problem of mixing UICulture = auto and allowing users to select culture using a dropdown list. I am detecting a querystring, "setlang", and when found, setting the...
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...
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...
2
by: Piotr K | last post by:
Hi, I've encountered a strange problem with Firefox which I don't have any idea how to resolve. To the point: I've <divelement with a style "height: auto" and I want to retrieve this value...
21
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
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
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...

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.