Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieving the ID of insert record

Status
Not open for further replies.

sthmpsn1

MIS
Sep 26, 2001
456
US
I am inserting a record into the database using the following statement and was wondering how I could get the ID of that record inserted. Here is my statement.

myConnection = New SqlConnection("")
strUpdateStmt = "Insert monkeys (loginID, thedate, fromemp, toemp, employeename, dollaramount, comments) Values (@loginID, @date, @fromemp, @toemp, @employee, @dollaramount, @comments)"
myCommand = new SqlCommand(strUpdateStmt, myConnection)
mycommand.Parameters.Add("@loginID", SqlDBType.nvarchar, 50).Value = employees.SelectedItem.value
mycommand.Parameters.Add("@date", SqlDBType.smalldatetime).Value = pagedate.text
mycommand.Parameters.Add("@fromemp", SqlDBType.nvarchar, 50).Value = fromemp
mycommand.Parameters.Add("@toemp", SqlDBType.nvarchar, 50).Value = supervisor.text
mycommand.Parameters.Add("@employee", SqlDBType.nvarchar, 50).Value = employeesname.text
mycommand.Parameters.Add("@dollaramount", SqlDBType.money).Value = dollar.text
mycommand.Parameters.Add("@comments", SqlDBType.text).Value = comments.text


myConnection.open()
myCommand.ExecuteNonQuery()
myConnection.close()

Is there a way to have it return the id in this statement?
 
Check out the t-sql @@identity, and how to use it in a stored procedure (Google &/or SQLBOL should be sufficient).

Then, specify the value of that as an output parameter and grab it after executing the sproc.
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top