In my application, I’m trying to redirect to another page after a record is inserted into my datasource. The insert part works fine, but the page never redirects. Any ideas? Here’s my current code:
ASPX
<asp:FormView ID="FormView1" runat="server" DataKeyNames="RecapID"
DataSourceID="RecapDataSource" DefaultMode="Insert">
<InsertItemTemplate>
Recap Date:
<asp:TextBox ID="RecapDateTextBox" runat="server"
Text='<%# Bind("RecapDate") %>' Width="80px" /><asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator1" ControlToValidate="RecapDateTextBox" ErrorMessage="*" display="Dynamic" />
<asp:Button ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Create"/>
</InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="RecapDataSource" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO [ftsrms_Recap] ([RecapDate]) VALUES (@RecapDate) SELECT @NewRecapID = SCOPE_IDENTITY()"
OldValuesParameterFormatString="original_{0}">
<InsertParameters>
<asp:Parameter DbType="Date" Name="RecapDate" />
<asp:Parameter Direction="Output" Name="NewRecapID" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
ASPX.VB
Protected Sub RecapDataSource_Inserted(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
'Read the value of the @NewProductID OUTPUT parameter
Dim newRecapID As Integer = Convert.ToInt32(e.Command.Parameters("@NewRecapID").Value)
Response.Redirect("Profile.aspx?")
End Sub
You need to add event handler to RecapDataSource:
oninserted=”RecapDataSource_Inserted”
So your aspx code becomes:
Edit:
Also Change this:
to this:
If it still does not works, remove this line (or comment) and try again:
May be it rises exception.