When I try to get the last ID inserted on Database via SQL Server it returns __Page. Here is my code :
query = "INSERT INTO
seekers(name, sname, lname, status, gender, dob, major, experince,
email, password, phone, valid, city)
values (@name, @sname, @lname, @status ,@gender, @dob, @major,
@exp, @email, @password, @phone, 0, @city);
SELECT SCOPE_IDENTITY();";
// some code here related to parameters
command = new SqlCommand(query, connection);
int id = Convert.ToInt32(command.ExecuteScalar());
SCOPE_IDENTITY() returns the IDENTITY value inserted in T1. This was the last insert that occurred in the same scope. The SCOPE_IDENTITY() function returns the null value if the function is invoked before any INSERT statements into an identity column occur in the scope.
From the documentation of SCOPE_IDENTITY(), emphasis mine: Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch.
The @@Identity function will return the last identity value inserted in the current session, in any table and in any scope. The Scope_Identity() function will return the last identity value inserted in the current scope (and session), in any table.
If just you want to SELECT
it use OUTPUT
;
INSERT INTO seekers(name,sname,lname,status,gender,dob,major,experince,email,password,phone,valid,city)
OUTPUT INSERTED.IDENTITY_COL_NAME
values(@name,@sname,@lname,@status,@gender,@dob,@major,@exp,@email,@password,@phone,0,@city);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With