Results 1 to 9 of 9

Thread: Auto number increment

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    29

    Wink Auto number increment

    Hi,

    I set the auto increment in SQL server for CustNo. How do I display this CustNo in the customer form?? How to retrieve the last CustNo + 1 ??? i am using VB.NET.

    Thank you in advance!!

    viv

  2. #2
    Junior Member
    Join Date
    Jul 2004
    Location
    Port Huron, Michigan
    Posts
    20
    So you are using an autoincrementing value on the sql database, for a primary key? You can return @@IDENTITY in the sql procedure to get the incremented primary key of the record when you create it.

    Chris

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    29
    Hi there,

    still quite blur.

    Yes, the incrementing number is a primary key. How to return @@IDENTITY in the sql procedure to get the incremented primary key of the record ? What i wanted is to display the last number in the front end.

    Someone suggested me to use the key word lastResult, hows the syntax like??

    Any suggestion or example is appreciated.

    viv

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    If I am not mistaken your autoincrement field will have a name. I may have got this wrong but can't you just use the field name.

    How are you retrieving the records?
    Why do you need CustNo+1?


    Things I do when I am bored: DotNetable

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi vivsm.

    Do you know how to connect to a database. If so, do it.

    Then, do you know how to use datasets etc? If so, what is your specific problem?

    If not, you are going to have to read up on working with databases (there is plenty of guidance in the MSDN Help files) and then bring specific problems to this forum.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6
    Junior Member
    Join Date
    Jul 2004
    Location
    Port Huron, Michigan
    Posts
    20
    So you want the number of rows then? From your last post, this is how it sounded, or did you want the last ID value, because if you delete rows or use a seed the ID incremented value won't sync with the row count. To get your row count though you would use:

    SELECT Count(anyColumnName)
    FROM yourTable

    Now this is purely off the top of my head and its been a while since I had to write sql, so keep that in mind but I'll double check it when I get to my office later today.

    Chris

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    29
    yes, i want the last ID value, because if i delete rows or use a seed the ID incremented value won't sync with the row count. Thats tha problem i am facing.

    viv

  8. #8
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    Does this help

    Syntax
    IDENT_CURRENT('table_name')

    Arguments
    table_name

    Is the name of the table whose identity value will be returned. table_name is varchar, with no default.

    Return Types
    sql_variant

    Remarks
    IDENT_CURRENT is similar to the Microsoft® SQL Server™ 2000 identity functions SCOPE_IDENTITY and @@IDENTITY. All three functions return last-generated identity values. However, the scope and session on which 'last' is defined in each of these functions differ.

    IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.


    @@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.


    SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.


    Just add 1 to the returned value


    Things I do when I am bored: DotNetable

  9. #9
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    the best thing to do is create a stored procedure. in that, declare a variable such as @ReturnNum and at the end of the stored procedure, do this @ReturnNum = @@Identity.

    now, in your vb code, you want to do this:

    VB Code:
    1. 'InsertCmd is a predefined sqlclient.SqlCommand
    2.     Dim A_Number As SqlParameter = InsertCmd.Parameters.Add("@ReturnNum", SqlDbType.Int)
    3.         ticketNumber.Direction = ParameterDirection.Output

    then, you can do something like this:
    VB Code:
    1. messagebox.show("The number returned is " & A_number)

    hope that helps.

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