SearchForKnowledge
SearchForKnowledge

Reputation: 3751

How to edit an entry in the GridView using CommandField

I have the following commandfield inside my gridview:

<asp:CommandField ShowEditButton="True" HeaderText="EDIT" CausesValidation="false"
    HeaderStyle-BackColor="#CCCCCC" HeaderStyle-CssClass="infoBoldBlueSmall">
    <HeaderStyle BackColor="#CCCCCC"></HeaderStyle>
</asp:CommandField>

Entry looks like this:

enter image description here

When I click on the edit button I see an Update Cancel:

enter image description here

For some of the entry when I click on the edit button, I get the following error:

Server Error in '/' Application.
--------------------------------------------------------------------------------

'Ddl_c' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: 'Ddl_c' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[ArgumentOutOfRangeException: 'Ddl_c' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value]
   System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +2732917
   System.Web.UI.WebControls.ListControl.PerformSelect() +49
   System.Web.UI.Control.DataBindChildren() +11143343
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +182
   System.Web.UI.Control.DataBindChildren() +11143343
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +182
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +267
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3340
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +72
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +18
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +147
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +261
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +46
   System.Web.UI.Control.PreRenderRecursiveInternal() +108
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5483 

If there are more information needed, please let me know.

The gridview is populated so I know the data is there and I also checked the table and it is there as well.

How can I resolve the issue?

UPDATE:

The code is posted here:

http://pastebin.com/ezr9uNMc

Image which shows the GridView:

enter image description here

What is firing off when Edit is clicked for each row? If I click on Edit for 52, it works but for 57, 58, and 59 I get the stacktrace error above.

Please help me resolve the issue.

Upvotes: 0

Views: 120

Answers (2)

Steve
Steve

Reputation: 549

It would help to have the code, but I may have run into this before. It's possible you are setting the selected item before the data in the row is bound, so you might need to set the initially selected item in the RowDataBound event.

If that doesn't do it, make sure you are dealing with situations where you don't get any items in the list so you don't want to set the selected item.

Upvotes: 1

vkh
vkh

Reputation: 23

The problem must be the dropdownlist which is populated for each row.
When you click the edit button, selected value for dropdownlist does not exist.
Make sure that, bound value exists in dropdown's items.

Upvotes: 0

Related Questions