Retrieve an ID after record insertion

Jan 16 2009 8:02 AM

I have an SQL Datasource setup to insert a record into the DB. At the end of the Insert statement is a select statement allowing me to pull back the ID of the record I just inserted.

I need to output that ID to a querystring so that i can do a response.redirect to point the records information.


<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
SelectCommand="INSERT INTO [dbo.icoIconInformation] ([imageTypeID], [name], [information], [dateCreated], [gccStaff_id]) VALUES (@imageTypeID,@name,@information,@dateCreated,@gccStaff_id) SELECT @NewID = SCOPE_IDENTITY()">
<InsertParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="information" Type="String" />
<asp:Parameter Name="dateCreated" Type="DateTime" />
<asp:Parameter Name="imageThumbnail" Type="Object" />
<asp:Parameter Name="gccStaff_id" Type="Int32" />
<asp:Parameter Name="imageFull" Type="Object" />
<asp:Parameter Name="imageTypeID" Type="Int32" />
<asp:Parameter Direction="Output" Name="newID" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>

This is the kind of thing I need to do. I need to get the parameter value from the query and output it to the redirect.


protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
string newID;
newID = SqlDataSource1.SelectParameters.GetValues[
"@newID"];
Response.Redirect(
"imageDetails.aspx?img=" + newID);
}


Any help would be appreciated.

Kindest regards

Chris


Answers (1)