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

Cannot Add New Record in Grid

4 Answers 24 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Perry
Top achievements
Rank 1
Perry asked on 23 Aug 2019, 07:20 PM

With this RADGrid setup

<telerik:RadGrid ID="RgComparisonGrid" runat="server"
                                        OnInsertCommand="RgComparisonGrid_InsertCommand"
                                        OnUpdateCommand="RgComparisonGrid_UpdateCommand"
                                        OnItemCreated="RgComparisonGrid_ItemCreated"
                                        OnItemInserted="RgComparisonGrid_ItemInserted"
                                        OnItemDataBound="RgComparisonGrid_ItemDataBound"
                                     OnPreRender="RgComparisonGrid_PreRender"
                                        AllowAutomaticInserts="false"
                                        AutoGenerateColumns="false"
                                        OnNeedDataSource="RgComparisonGrid_NeedDataSource"
                                        AllowAutomaticDeletes="false"
                                        AllowAutomaticUpdates="false"
                                        MasterTableView-AllowAutomaticInserts="false">
                                        <MasterTableView AutoGenerateColumns="false" CommandItemDisplay="Top" AllowAutomaticInserts="false"
                                            DataKeyNames="ComparisonId, SubjectId_FK, ResultsId" Font-Size="Medium"
                                            NoMasterRecordsText="No Comparisons Added" CommandItemSettings-AddNewRecordText="Add New Comparison">

I am not able to add a new record I am getting this error message:

Unhandled exception at line 1, column 123034 in http://localhost:52028/bundles/MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81

0x800a139e - JavaScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

 

Anyone have a suggestions as to why?

4 Answers, 1 is accepted

Sort by
0
Perry
Top achievements
Rank 1
answered on 26 Aug 2019, 11:42 AM

more information the error comes up only when trying to add a new record.  In the ItemDataBound method I am setting the value of some drop down lists and the selected value of one of the drop downs.  with this C# code: Since I am adding a new record the SubjectID_FK is going to be null.  How can I correct this problem?

listSubjectName.SelectedValue = item.GetDataKeyValue("SubjectId_FK").ToString();

 

0
Perry
Top achievements
Rank 1
answered on 26 Aug 2019, 03:53 PM

The problem was I was trying to provide the value of a datakey while doing an insert.  Datakey was not assigned at that time.  Had to distinguish if I was in insert mode or update mode and handle the code based on the current mode.  More info at this web link

 

0
Attila Antal
Telerik team
answered on 28 Aug 2019, 08:43 AM

Hi Perry,

Good catch! New records are indeed missing the DataKeyValues since there is no data bound to the Insert item. When executing logic while inserting/editing, it is very important to make a difference between the two, thus you can avoid issues like the one you have experienced.

Another tip that I would like to share, and it happens to be a crucial one when developing apps with AJAX enabled. Disable AJAX during the time of developing the application: Get more descriptive errors by disabling AJAX

What AJAX does, is trying to communicate with the server without making an entire PostBack, thus the page flickering and others are eliminated. AJAX is not the key component in an application, it is more like applying polish on an existing product. 

Here comes the ISSUE.

If developing an application, which is not ready for deployment, most probably it will be tested several times for potential errors. If there are runtime errors, it is expected to throw the exception and show it on the surface, but nothing happens. The reason is that AJAX will most likely catch the error comming back from the server and prevent it from showing up on the client. Until the developer realizes, that there is an error and is hidden, he most likely spends lot of his time to revert back the changes he made, or change other lines, etc... 

Here comes the SOLUTION:

By not having AJAX enabled on the page during the development process, all errors will come up and face the surface. This will precisely tell the developers where the error is originating, and what is its nature. Having that instantly as you make a change would drastically reduce developing time.

Kind regards,
Attila Antal
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Perry
Top achievements
Rank 1
answered on 28 Aug 2019, 01:08 PM
Thanks Attila that's a great tip
Tags
Grid
Asked by
Perry
Top achievements
Rank 1
Answers by
Perry
Top achievements
Rank 1
Attila Antal
Telerik team
Share this question
or