Advertisement

03.17.2008 at 04:37PM PDT, ID: 23248836
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

How to make DataTable.Columns("PrimaryKeyCol").AutoIncrement work with a SQL Server Primary Key field
Tags: vb.net
I have a table in SQL server 2000 that I am displaying in a vb.net 1.1 VS 2003 windows forms datagrid.
I am using drag and drop connection and dataadapter objects and generating the dataset in the designer.
I am instantiating a commandbuilder in code to handle CRUD commands.
The Sql server table has a primary key col and has existing records.
The Sql server table primary key column is not set to be an Identity column.

I am not displaying the primary key column in the datagrid, but when I add a new row, I want the dataadapter to properly insert the new row.
Currently I have set the autoincrement properties as follows:
   DataTable.Columns("PrimaryKeyCol").AutoIncrementSeed = -1
   DataTable.Columns("PrimaryKeyCol").AutoIncrementStep = -1
   DataTable.Columns("PrimaryKeyCol").AutoIncrement = True

So far so good and I can add new records sucessfully.
Therefore, not surprisingly, the new records added via my app in sql server have primary key incrementing from  -1 step -1.
Instead of this, I want sql server to automatically assign the next highest primary key value.

I recall reading somewhere that I can set the Primary key col to Identity = Yes.
Doing this does indeed seem to work.
SQL server seems to ignore the values set in autoincrement in the app and correctly inserts its own id value.

My question is, exactly how are the dataadapter and sql server working under the covers to make this happen?

Thanks in advance.
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: bobinorlando
Solution Provided By: Sancler
Participating Experts: 2
Solution Grade: A
Views: 217
Translate:
Loading Advertisement...
03.18.2008 at 12:05AM PDT, ID: 21148866

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.18.2008 at 12:25AM PDT, ID: 21148923

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.18.2008 at 03:27AM PDT, ID: 21149554

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.18.2008 at 03:41AM PDT, ID: 21149623

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.18.2008 at 10:18AM PDT, ID: 21153433

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.18.2008 at 12:05AM PDT, ID: 21148866

Rank: Wizard



        Dim oDatarow As DataRow
        Dim Id As Integer


                 'set the " Columns(0) " to which contains a unique id in the datatable
                 Dim pk(0) As DataColumn
                 pk(0) = ds.Tables("Test").Columns("PrimaryKeyCol").
                ds.Tables("Test").PrimaryKey = pk

                ds.Tables("Test")..Rows.Find(Id) ' find the particular row with that primary key
                oDatarow.Delete()' e.g delete this row

btw

DataTable.Columns("PrimaryKeyCol").AutoIncrementStep = -1
has to be
DataTable.Columns("PrimaryKeyCol").AutoIncrementStep = +1

vbturbo
 
03.18.2008 at 12:25AM PDT, ID: 21148923
 
03.18.2008 at 03:27AM PDT, ID: 21149554

Rank: Genius

>>
exactly how are the dataadapter and sql server working under the covers to make this happen?
<<

I'm not sure about all the details of this.  

But a central point from the application - ADO.NET - end, I think, is that when a commandbuilder autogenerates an INSERT statement for a database table on which the Primary Key is AutoIncrement it does not include any Primary Key information from the datatable.  For UPDATE and DELETE commands, the Primary Key is essential, because the record which is to be updated or deleted has to be uniquely identified.  If a record to be inserted is not going to have its Primary Key generated automatically by the database, again it will be essential (and must be unique) or else the insert will violate the rules that a Primary Key must not be null and must be unique.  This is why, for instance, if you use the configuration wizards or a commandbuilder in relation to a database table which does not return any Primary Key information it may well generate the SELECT and INSERT commands but it will tell you it cannot generate the UPDATE and DELETE commands.

So, so far as the dataadapter is concerned, it is - in effect - simply saying to the server "Here's the details for a new record, generate the primary key yourself".  There are - as I mentioned in your question

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23245288.html

- techniques for the dataadapter to add "and then please tell me what it is".

At the server end, when it receives a message in that form and you've "set the Primary key col to Identity = Yes" it simply does as it's asked.

What I'm less sure about is what happens when - rather than the dataadapter using an autogenerated INSERT command - you code your own and include primary key information which would be inconsistent with the server autogenerating the Identity Primary Key.  But is what I've said above sufficient for your present purposes?

Roger
Accepted Solution
 
03.18.2008 at 03:41AM PDT, ID: 21149623

Rank: Wizard

>SQL server seems to ignore the values set in autoincrement in the app and correctly inserts its own id value.

This is because the adapter reads the table schema data and connection info amoung others.
The schema contains the tables attributes and their datatypes, also which column that is setup as pk column.
That way the adapter has knowledge about the starting point (when incrementing on the pk column)
which gets transfered to the datatable setup.

There also issues regarding concurrency , imagine that you have multiple users having their own instance of a table
and starting performing crud and in the end updating the same table in your datasource.

Its a big topic (an plenty opinions on how deal with that)

this is the short explanation of it
Assisted Solution
 
03.18.2008 at 10:18AM PDT, ID: 21153433
Guys thanks for taking a stab at this.
 

I found the source of the autoincrement = -1 idea.

Exploring the ADO.NET DataRow
http://msdn2.microsoft.com/en-us/magazine/cc164076.aspx

 
 
20080236-EE-VQP-29 / EE_QW_2_20070628