Asp.net, Parameters and SQLDataSource
<p>Just started learning ASP.NET...</p>

<p>I have a formview control that is bound to table REPORTS. This formview is INSERT only. In the "_Inserted" event, I grab the ID of the record that was just added. (@NewReportID)</p>

<p>Table REPORTS contains field UserID. I need to get the UserID of the currently authenticated user and insert it into the REPORTS table.</p>

<p>I then want to add a second formview control to the page and bind it to table ACTIVITY. It too will be INSERT only. Table ACTIVITY is related to table REPORTS by field ReportID. What I'd like to do is retrieve the value of @NewReportID and insert it into table ACTIVITY ReportID field.</p>

<p>Below the "ACTIVITY" formview, I want to add a gridview that shows all of the activity records for the current report. Essentially, it should work as follows:</p>

<ol>
<li>User fills out REPORT form. Data is inserted into table.</li>
<li>User fills out ACTIVITY form. Data is inserted into table.</li>
<li>Gridview updates as each activity record is added.</li>
</ol>

<p>I'm not sure how to use the @NewReportID parameter in the SQLDataSource of another control, like a formview or gridview. Can anyone provide assistance?</p>

<p><strong>ASPX</strong></p>

<pre><code>     &lt;asp:FormView ID="FormView1" runat="server" DataKeyNames="ReportID" 
            DataSourceID="ReportDataSource" DefaultMode="Insert"&gt;
                &lt;InsertItemTemplate&gt;
                    ReportDate:
                    &lt;asp:TextBox ID="ReportDateTextBox" runat="server" 
                        Text='&lt;%# Bind("ReportDate") %&gt;' Width="80px" /&gt;
                    &amp;nbsp;&amp;nbsp;&amp;nbsp; StartingHours:
                    &lt;asp:TextBox ID="StartingHoursTextBox" runat="server" 
                        Text='&lt;%# Bind("StartingHours") %&gt;' Width="40px" /&gt;
                    &amp;nbsp;&amp;nbsp;&amp;nbsp; EndingHours:
                    &lt;asp:TextBox ID="EndingHoursTextBox" runat="server" 
                        Text='&lt;%# Bind("EndingHours") %&gt;' Width="40px"/&gt;
                    &lt;br /&gt;
                    &lt;asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                        CommandName="Insert" Text="Create Report" /&gt;
                    &amp;nbsp;&lt;asp:LinkButton ID="InsertCancelButton" runat="server" 
                        CausesValidation="False" CommandName="Cancel" Text="Cancel" /&gt;
                &lt;/InsertItemTemplate&gt;
                &lt;/asp:FormView&gt;
        &lt;asp:SqlDataSource ID="ReportDataSource" runat="server" 
            ConflictDetection="CompareAllValues" 
            ConnectionString="&lt;%$ ConnectionStrings:ConnectionString %&gt;"  
            InsertCommand="INSERT INTO [ftsrms_Report] ([ReportDate], [StartingHours], [EndingHours]) VALUES (@ReportDate, @StartingHours, @EndingHours) SELECT @NewReportID = SCOPE_IDENTITY()" 
            OldValuesParameterFormatString="original_{0}" &gt;
            &lt;InsertParameters&gt;
                &lt;asp:Parameter DbType="Date" Name="ReportDate" /&gt;
                &lt;asp:Parameter Name="StartingHours" Type="Decimal" /&gt;
                &lt;asp:Parameter Name="EndingHours" Type="Decimal" /&gt;
                &lt;asp:Parameter Direction="Output" Name="NewReportID" Type="Int32" /&gt;
            &lt;/InsertParameters&gt;
        &lt;/asp:SqlDataSource&gt;
&lt;asp:Label ID="NewReportAddedMsg" runat="server" Text=""&gt;
&lt;/asp:Label&gt;
</code></pre>

<p><strong>ASPX.VB Code</strong></p>

<pre><code>Protected Sub ReportDataSource_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles ReportDataSource.Inserted

    'Read the value of the @NewProductID OUTPUT parameter
    Dim newReportID As Integer = Convert.ToInt32(e.Command.Parameters("@NewReportID").Value)

    'Display confirmation message
    NewReportAddedMsg.Text = String.Format(newReportID)

End Sub
</code></pre>

<p>I added label "NewReportAddedMsg" to confirm that I captured @NewReportID correctly. It will not be used going forward.</p>
