Results 1 to 5 of 5

Thread: [RESOLVED] Question About Primary key

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    123

    Resolved [RESOLVED] Question About Primary key

    I have a database running on SQL server primary key is all set, my question is i dont want to allow duplicates in the primary key, i want everything to be uniqure. The database already has information in it, is there a away to set it so when i add a new row through the table adapter that it will recognise the last primary key value and increment it by +1?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Question About Primary key

    No... actually, you don't want the code setting the PKey. The way it should be working (at this this is how it works for us)... is that when you add a row, if the datatable knows it's a PKey (which it should if it's been setup right) the first add will get -1. The next -2, and so on. When the datatable is then .Updated with the database, the database generates the PKey (using what ever method is setup) and sends the PKey back to the datatable. the datatable will then have the correct PKey. The reason you don't want the datatable/code doing it is because if two people add a record.... and both get the same PKey.... you've got a problem.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    Re: Question About Primary key

    cant you just set it to autoincrement in your sql server management studio(assume you are using this) and if it is a primary key, there should be unique.
    Just a newbie view.
    sorry...techgnome...ignore mine addntox
    -- Please rate me if I am helpful --

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    123

    Re: Question About Primary key

    techgnome : - thanks i see where i made my mistake now

    cengineer : - also thanks for you input too

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] Question About Primary key

    cengineer - no, no, that's a valid point.... and one I glossed over. I assumed that the table in the database already had that autoincrement (Identity in SQL Server) already set. In fact, that's the only way it really does work.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width