Retrieve ID when Inserting from a ObjectDataSourcehttp://forums.asp.net/t/1778614.aspx/1?Retrieve+ID+when+Inserting+from+a+ObjectDataSourceSat, 10 Mar 2012 12:54:14 -050017786144872282http://forums.asp.net/p/1778614/4872282.aspx/1?Retrieve+ID+when+Inserting+from+a+ObjectDataSourceRetrieve ID when Inserting from a ObjectDataSource <p>Hi, does anyone know how to retrieve the ID when using a objectdatasource to insert records. &nbsp; Here's what I'm trying to do.</p> <p>I have a ODS that calls an insert method.</p> <p>&lt;asp:ObjectDataSource ID=&quot;ObjectDataSource1&quot; runat=&quot;server&quot; DataObjectTypeName=&quot;Person&quot;<br> InsertMethod=&quot;InsertPerson&quot; TypeName=&quot;Person&quot; <br> oninserted=&quot;ObjectDataSource1_Inserted&quot;&gt;<br> &lt;/asp:ObjectDataSource&gt;</p> <p>Insert Method Inserts a record into the database.</p> <p>public void InsertPerson(Person p)<br> {</p> <p>SqlConnection con = new SqlConnection(_connectionString);<br> SqlCommand cmd = new SqlCommand(&quot;Insert into Person (UserName, PersonNo, FirstName, LastName, MiddleName, PersonSuffix, PersonPrefix, FullName, BirthDate, Gender, EmergencyContactName, EmergencyContactPhone, EmergencyContactEmail, EmergencyContactRelationship, LegacyPersonIdentifier, Attribute1, Attribute2, Attribute3, Attribute4, Attribute5, Attribute6, Attribute7, Attribute8, Attribute9, Attribute10, ActiveFlag, StartDate, EndDate, CreationDate, CreatedBy, LastUpdateDate, LastUpdatedBy) values (@username, @personno, @firstname, @lastname, @middlename, @personsuffix, @personprefix, @fullname, @birthdate, @gender, @emergencycontactname, @emergencycontactphone, @emergencycontactemail, @emergencycontactrelationship, @legacypersonidentifier, @attribute1, @attribute2, @attribute3, @attribute4, @attribute5, @attribute6, @attribute7, @attribute8, @attribute9, @attribute10, @activeflag, @startdate, @enddate, @creationdate, @createdby, @lastupdatedate, @lastupdatedby)&quot;, con);<br> cmd.Parameters.AddWithValue(&quot;@username&quot;, p.UserName);<br> cmd.Parameters.AddWithValue(&quot;@personno&quot;, p.PersonNo);<br> cmd.Parameters.AddWithValue(&quot;@firstname&quot;, p.FirstName);<br> //removed other parameters to shorten code<br> using (con)<br> {<br> con.Open();<br> cmd.ExecuteNonQuery();<br> }<br> }<br> }</p> <p></p> <p>When I try to output the ID of the inserted record, I only get zero.</p> <p></p> <p>protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)<br> {</p> <p>int id = Convert.ToInt32(e.ReturnValue);<br> lblFormviewResult.Text = &quot;Record Successfully Added.&quot; &#43; id.ToString();<br> }</p> <p></p> <p>Any help would be greatly apprecated. &nbsp;Jason</p> 2012-03-09T19:15:00-05:004872298http://forums.asp.net/p/1778614/4872298.aspx/1?Re+Retrieve+ID+when+Inserting+from+a+ObjectDataSourceRe: Retrieve ID when Inserting from a ObjectDataSource <p>Two changes.&nbsp;&nbsp; First to your sql command, add this at the very end:</p> <p>...@lastupdatedate, @lastupdatedby)<strong>; SELECT SCOPE_IDENTITY();&quot;</strong>, con);</p> <p>Then change this:</p> <p>cmd.ExecuteNonQuery();</p> <p>to this:</p> <p>int id = cmd.ExecuteScalar();</p> 2012-03-09T19:28:03-05:004873079http://forums.asp.net/p/1778614/4873079.aspx/1?Re+Retrieve+ID+when+Inserting+from+a+ObjectDataSourceRe: Retrieve ID when Inserting from a ObjectDataSource <p>Thank you Tab. &nbsp;It worked!</p> 2012-03-10T12:54:14-05:00