Question: Hi i'm having trouble with inserting incremented userid inside my db below is my table,stored proc,and my code.

```
CREATE TABLE [dbo].[Assignment2]
(
userID int PRIMARY KEY IDENTITY(1,1),
Name varchar(255) NOT NULL,
Age int NOT NULL,
Hobbies varchar(255)
)

```

and a stored procedure 

```
ALTER PROCEDURE [db].[p_Assignment2_ins]
        @userID           int,
        @Name             nvarchar(100),
        @Age              int,
        @Hobbies          nvarchar(100)                   
AS

INSERT INTO [DB].[db].[Assignment2]
           ([Name]
           ,[Age]
           ,[Hobbies])
     VALUES
           (@Name
           ,@Age
           ,@Hobbies)

If @@Error <> 0
    Return -1

    Select @userID = @@Identity // this one just get the latest id that we inserted                 right?

Return 0 

```

I have some question :

1. I want to know how do we insert the `UserID` from the code behind because If the table is empty at first shouldn't we insert a data first into the table
2. How do we generate an `AutoIncrementID` from codebehind and insert it

```
        SqlConnection conn = new SqlConnection(ts.ConnMethod());
        SqlCommand cmd = new SqlCommand("p_Assignment2_ins", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        //I'm missing how we should add the IncrementedID 

        cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Age", TextBox2.Text);
        cmd.Parameters.AddWithValue("@Hobbies", TextBox3.Text);

        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

```

Any help is really appreciated thanks

Answer: 
If you have AUTOINCREMENT field, you **SHOULD NOT** insert and generate values at all. DB is doing all by itself. So, remove the code that inserts ID-s
```
   Select @userID = @@Identity // this one just get the latest id that we inserted   right?

```

No, you should use `SCOPE_IDENTITY()`. @@Identity returns the last id, yes, but not the last id from **your** insert. If you have a trigger, for example, that also produces an insert on a another table as a result of your insert, you will get the id inserted on the other table by that trigger.

[Here's a nice article](http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/) explaining the differences.

As far as inserting autoincrement values on your table; you can't do this unless you disable the constraint first but in essence, this shouldn't be necessary unless you are doing some sort of data import. In your scenario, you should be able to just insert values on the other columns except the identity field with autoincrement. The database will take care of inserting the appropriate value for you.