For a ‘new record’ site I have a FormView attached to an SqlDataSource. The user inputs the data and the InsertCommand has the SQL INSERT statement. The thing is that I want to do other queries as part of the initialization, and these queries involve other tables.
Basically I want to execute all these queries when the user clicks the “Insert” button.
The first INSERT gets the values from the input form, but the other three are always the same.
INSERT INTO proceso (nombreProceso, macroProceso, analista_id)
VALUES (@nombreProceso,@macroProceso,@analista_id)
INSERT INTO marcador_progreso (marcador_id,state,proceso_id) VALUES (1,'False',@id)
INSERT INTO marcador_progreso (marcador_id,state,proceso_id) VALUES (2,'False',@id)
INSERT INTO marcador_progreso (marcador_id,state,proceso_id) VALUES (3,'False',@id)
When I run this, the first statement apparently gets executed (i.e a new proceso record gets inserted) but the other 3 INSERTs do not. How should I proceed to solve this?
The SqlDataDource contains the id parameter
<InsertParameters>
... a few parameters...
<asp:Parameter Name="id" />
</InsertParameters>
Please correct me if I’m wrong but it seems to me you want the
@idthing to be the ID of the row just inserted intoproceso. At least that seems to follow from the correspondingmarcador_progresocolumn’s name (proceso_id). If my surmise is correct then you don’t need a parameter calledid. The@idin your query should be a local variable initialised by the ID of the row inserted by the firstINSERT.If
proceso‘s ID is an integerIDENTITYcolumn, then change your script like this:And remove the
idparameter.The
SCOPE_IDENTITY()function returns the last value inserted into anIDENTITYcolumn of any table in the current scope of execution.