I have a gridview that I am attempting to update the row. If I put a breakpoint on the row updating event it gets hit the first time through and the row is updated successfully. As I continue to step through that code is not hit a second time, however I get an exception that the stored procedure from that update has too many parameters and throws an error.
I do have the autoeventwireup set to true. Is this a problem? I tried setting it to false but I have other things going on in the page_load that doesn’t happen and breaks the page. Plus, from what I’ve read it might not be the issue. Any help is greatly appreciated!
Here is the stack trace on the error
[SqlException (0x80131904): Procedure or function RecordingUpdateName has too many arguments specified.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2084358
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5096328
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2294
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +215
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +178
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +394
System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +697
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +95
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1226
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +855
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +121
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
And:
<asp:GridView ID="grdvwRecordings" runat="server"
DataSourceID="sqldataRecordings"
EmptyDataText="No recordings have been saved. Select Call Now below. "
DataKeyNames="RecordingID"
onrowdeleting="grdvwRecordings_RowDeleting"
onrowupdating="grdvwRecordings_RowUpdating"
onrowediting="grdvwRecordings_RowEditing"
AutoGenerateColumns="False"
onrowdatabound="grdvwRecordings_RowDataBound">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField HeaderText="Recording">
<ItemTemplate>
<asp:Label ID="lblRecordingName" Text='<%# Bind("Name") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtRecordingName" Text='<%# Bind("Name") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Recorded">
<ItemTemplate>
<asp:Label ID="lblDateRecorded" Text='<%# Bind("EntryDate") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration">
<ItemTemplate>
<asp:Label ID="lblDuration" Text='<%# Bind("Duration") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqldataRecordings" SelectCommand="[RecordingSelectByClient]"
SelectCommandType="StoredProcedure" runat="server"
ConnectionString="<%$ ConnectionStrings:VB %>"
onselecting="AddClientIDParameter_Selecting"
UpdateCommand="RecordingUpdateName"
UpdateCommandType="StoredProcedure"
DeleteCommand="RecordingDelete"
DeleteCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="RecordingName" Type="String" />
<asp:Parameter Name="RecordingID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
And:
protected void grdvwRecordings_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
this.sqldataRecordings.UpdateParameters["RecordingID"].DefaultValue = e.Keys["RecordingID"].ToString();
this.sqldataRecordings.UpdateParameters["RecordingName"].DefaultValue = e.NewValues["Name"].ToString();
this.sqldataRecordings.Update();
}
Please try the following:
Remove
onrowupdating="grdvwRecordings_RowUpdating"from the GridView attributesIn the
SqlDataSourcetag replace theUpdateParameterswith:I think what is happening is you are specifying your update command once in the GridView (using the
OnRowUpdatedattribute) and once in theSqlDataSource.Edit:
As Tim Brooks indicated in the comments below, he needed to use
<%# Eval("Name") %>instead of Bind, as the bound fields were automatically being added as parameters to the update command.Also, I would not worry about exposing your primary key by making my suggested changes. Since this is a web app and you are getting the RecordingID from the GridViewUpdateEventArgs you are already passing the information down to the user’s computer in the ViewState. If this is not acceptable I suggest you encrypt the ViewState. There may be a noticeable performance hit, but the ViewState will be safe from reading.
See Page.ViewStateEncryptionMode Property, Encrypt ViewState in ASP.NET 2.0, and Securing View State for more information.
If you need a primer on ViewState You might start with this post, but I am sure there are many other good ones out there.