Cannot Add New Record in Grid

5 posts, 0 answers
  1. Perry
    Perry avatar
    62 posts
    Member since:
    Jul 2015

    Posted 23 Aug 2019 Link to this post

    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?

  2. Perry
    Perry avatar
    62 posts
    Member since:
    Jul 2015

    Posted 26 Aug 2019 in reply to Perry Link to this post

    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();

     

  3. Perry
    Perry avatar
    62 posts
    Member since:
    Jul 2015

    Posted 26 Aug 2019 in reply to Perry Link to this post

    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

     

  4. Attila Antal
    Admin
    Attila Antal avatar
    440 posts

    Posted 28 Aug 2019 Link to this post

    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.
  5. Perry
    Perry avatar
    62 posts
    Member since:
    Jul 2015

    Posted 28 Aug 2019 in reply to Attila Antal Link to this post

    Thanks Attila that's a great tip
Back to Top