Question : Find the ID of inserted record from formview

Hey, I'm trying to set a hidden textbox to the value of the ID from a newly inserted record through a formview.

This is what I'm trying to accomplish:

User fills in formsview to insert new customer
-onclick of insert the ID of this new record is set to a hidden text box
--javascript function gets the ID and passes it back to the parent window.

Got everything working, just cant get the ID.

I've found some stuff on this but it all seems to be in c# and i'm using vb. read something about GET SCOPE(), didn't quite understand how I could apply it to this though.

Thanks in advance!

Answer : Find the ID of inserted record from formview

This is how i explained it above, except not in a stored procedure. The 'insertcommand' is making two seperate SQL Statements: first inserting (INSERT INTO) then the second is separated by the ';' and Selecting the same record using '@@Identity' as a new variable '@DepartmentID'.
The '@DepartmentID' needs to be defined as a new 'insertparameter' and since we are receiving a value from this and not sending into it's direction is 'Output'. Now that this @DepartmentID is output to our page we can grab that variable in our code behind using a new method and write it out to a label.

I believe the code translation to VB.Net is:
Protected Sub sdsDepartment_Inserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
        If (e.Exception Is Nothing) Then
            lblMessage.Text = String.Format("Department '{0}' successfully added.", e.Command.Parameters("@DepartmentID").Value)
        Else
            lblMessage.Text = "Unable to add department."
            e.ExceptionHandled = true
        End If
    End Sub

==========
If you post both your full .aspx and .vb code I can manipulate it for you.
Random Solutions  
 
programming4us programming4us