Possible Duplicate:
How to get last inserted id?
I have a table Absences
| Id | Name | Job |
-------------------------
| 1 | James | 1 |
-------------------------
| 2 | Simon | 1 |
-------------------------
Where ID is an identity Primary Key
incrementing by 1.
I'm accessing this table from a program in C# and I need to do the following :
Insert Into Absences (Name, Job) Values ('aName', 'aJob')
The problem is I need to get the Id
column where i'm inserting at the same time because Name
and Job
are not unique so I won't be able to retreive this exact column after.
Is it possible to add a select on the Id
column in that query ?
Update
SqlConnection myConnection = new SqlConnection(@"SomeConnection");
myConnection.Open();
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "Insert Into Absences (Name, Job) Values ('aName', 'aJob')";
int currentAbs = (int)myCommand.ExecuteScalar();
I get an error on the ExecuteScalar Line. Object reference is not set to and instance of object.