This is a migrated thread and some comments may be shown as answers.

GridCalculatedColumn causing error when last row deleted in RadGrid

1 Answer 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 26 Feb 2014, 10:00 PM
Hello,

I recently made a change to a RadGrid by adding a Calculated column.  My data source is an in-memory dataset.  When I delete the final row in the grid and remove it from the dataset (in the code behind) I receive the following error:

Sys.WebForms.PageRequestManagerServerErrorException: Exception has been thrown by the target of an invocation.

I was able to get a little bit more information by adding a Rebind() to my grid in the deletion method.  I received the following inner exception
{"This row has been removed from a table and does not have any data.  BeginEdit() will allow creation of new data in this row."}

I have further proven the calculated field is the cause, by simply removing the GridCalculatedColumn and re-running the code with no issues.  The error return as soon as the calculated grid is added to the form.

Here's the markup:
<telerik:RadGrid ID="rdTicketGrid" runat="server" Width="70%" AllowAutomaticUpdates="false" AllowAutomaticDeletes="false" AllowAutomaticInserts="false" AllowSorting="false"
                            OnNeedDataSource="rdTicketGrid_NeedDataSource" AutoGenerateColumns="false" ShowFooter="true"
                            OnInsertCommand="rdTicketGrid_InsertCommand"
                            OnUpdateCommand="rdTicketGrid_UpdateCommand"
                            OnItemCreated="rdTicketGrid_ItemCreated"
                            OnDeleteCommand="rdTicketGrid_DeleteCommand"
                            OnItemDataBound="rdTicketGrid_ItemDataBound">
            <PagerStyle Mode="NextPrevAndNumeric" />
            <MasterTableView DataKeyNames="TicketEntryID" CommandItemDisplay="Top" InsertItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage">
                <Columns>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn>
                    <telerik:GridBoundColumn DataField="TicketEntryID" UniqueName="TicketEntryID" Display="false"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="NumOfTickets" HeaderText="Number of Tickets"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="NumberTicketsInSet" HeaderText="Number of Tickets per Set"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="PriceOfTickets" HeaderText="Price of Tickets" DataFormatString="{0:C}"></telerik:GridBoundColumn>
                    <telerik:GridCalculatedColumn HeaderText="Total Amount" UniqueName="TotalSetAmount" DataType="System.Double"  DataFormatString="{0:C}"
                                                    DataFields="NumOfTickets, NumberTicketsInSet, PriceOfTickets" Expression="{0}/{1}*{2}" FooterText="Total: " Aggregate="Sum"
                                                    FooterAggregateFormatString="{0:C}">
                    </telerik:GridCalculatedColumn>
                    <telerik:GridButtonColumn ConfirmText="Delete this ticket entry?" ConfirmDialogType="RadWindow"
                    ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" />
                </Columns>
                <EditFormSettings>
                    <EditColumn ButtonType="ImageButton"></EditColumn>
                </EditFormSettings>
            </MasterTableView>
            <ClientSettings>
                <ClientEvents OnRowDblClick="rowDblClick" />
            </ClientSettings>
        </telerik:RadGrid>
 
        <telerik:RadInputManager runat="server" ID="rdInputMgr" Enabled="true">
            <telerik:NumericTextBoxSetting BehaviorID="TicketEntryIDTBSetting" Type="Number" AllowRounding="false" DecimalDigits="0"></telerik:NumericTextBoxSetting>
            <telerik:NumericTextBoxSetting BehaviorID="NumTicketsTBSetting" Type="Number" AllowRounding="false" DecimalDigits="0"></telerik:NumericTextBoxSetting>
            <telerik:NumericTextBoxSetting BehaviorID="PriceTicketsTBSetting" Type="Currency" AllowRounding="true" DecimalDigits="0"></telerik:NumericTextBoxSetting>
            <telerik:NumericTextBoxSetting BehaviorID="NumberTicketsInSetTBSetting" Type="Number" AllowRounding="true" DecimalDigits="0"></telerik:NumericTextBoxSetting>
        </telerik:RadInputManager>

And the code behind data source.  The dataset is built and then stored to session.  The data will be gathered later on submit.:

DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("TicketEntryID");
dt.Columns.Add("NumOfTickets", typeof(int));
dt.Columns.Add("NumberTicketsInSet", typeof(int));
dt.Columns.Add("PriceOfTickets", typeof(double));
 
ds.Tables.Add(dt);               
Session["ticketGridSrc"] = ds;.

Finally the offending method that brakes the grid:

protected void rdTicketGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editableItem = ((GridEditableItem)e.Item);
 
            try
            {
                int ticketEntryID = int.Parse(((GridDataItem)e.Item).GetDataKeyValue("TicketEntryID").ToString());
 
                //Access the data stored in the session
                DataSet currentDataSrc = ((DataSet)Session["ticketGridSrc"]);
 
                if (currentDataSrc != null)
                {
                    //build the select string we need to get the data from the table to delete
                    String selectStmt = string.Format("TicketEntryID = '{0}'", ticketEntryID);
 
                    //Get the row we are deleting from the source.              
                    DataRow[] updateRow = currentDataSrc.Tables[0].Select(selectStmt);
 
                    updateRow[0].Delete();
                    currentDataSrc.AcceptChanges();
                }
 
                Session["ticketGridSrc"] = currentDataSrc;
            }
            catch (Exception ex)
            {
                //TODO: Error Handling Here
            }
        }

Any help on determining why the error occurs would be greatly appreciated.

Thank you,

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 03 Mar 2014, 01:32 PM
Hello Dave,

Please refer to the answer in the other forum thread that you have opened with a related issue.

As for the exception that you are receiving, this is a general exception and in order to locate the exact cause of it you should temporarily disable the AJAX on your page and see the full stack trace of the error. Additionally, bellow are some forum thread related to that error:

For all sides convenience and for better tracking of the issues I suggest that we proceed with any further communication in the other forum thread.


Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Dave
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or