Got exception when enabling INSERT in DetailView

Last post 01-17-2007 6:10 PM by Trevor311. 3 replies.

Sort Posts:

  • Got exception when enabling INSERT in DetailView

    12-30-2006, 9:31 AM
    • Member
      point Member
    • marcuskn
    • Member since 12-30-2006, 2:24 PM
    • Posts 2

    Hi, I'm getting the following error when I try to enable AutoGenerateInsertButton in DetailView.  When I debug the app and press the link 'New', I got this error:

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Line 57:                     {
    Line 58:                         DetailsViewRow row = ControlAsDetailsView.Rows[iRow];
    Line 59:                         if ((!ControlAsDetailsView.AutoGenerateRows) &&
    Line 60:                             ((row.RowState & DataControlRowState.Insert) == DataControlRowState.Insert) &&
    Line 61:                             (!ControlAsDetailsView.Fields[row.RowIndex].InsertVisible))

    This happens to me even when I modify the DetailView.aspx in the sample application solution (CssAdapterTutorial1).  I have read a past post and knew that this is a bug in the beta version.  And I checked the detailviewadapter.cs and it does have the fix suggested from that post.  But it seems there is still a problem.  Can anyone help?  I'm using the CSSFriendlyControlAdapter v1.0.

    Thanks a  lot.

  • Re: Got exception when enabling INSERT in DetailView

    01-04-2007, 2:15 PM
    • Contributor
      3,298 point Contributor
    • Russ Helfand
    • Member since 09-14-2005, 6:22 PM
    • Groovybits.com
    • Posts 741

    Hi Marcus, thank you for contributing this posting (above). You have, indeed, found a bug in the code I wrote.  Please accept my apologies for the inconvenience.

    The fix is, happily, quite simple.  I forgot an important clause in the expression used in the "if" statement shown in red in your posting.  I believe the correct logic should read:

    if (useFields &&
        (!ControlAsDetailsView.AutoGenerateRows) &&
        ((row.RowState & DataControlRowState.Insert) == DataControlRowState.Insert) &&
        (!ControlAsDetailsView.Fields[row.RowIndex].InsertVisible))

    This fix is at line 59 of App_Code\Adapters\DetailsViewAdapter.cs.  If you are using the VB version the syntax is slightly different.  Use AndAlso in place of the &&.

    The variable called useFields is set outside of the "for" loop that this "if" statement is within.  The useFields boolean dictates whether or not we can reliably use the Fields collection in the DetailsView.  We can't use it reliably when the field count doesn't match the row count because some rows are being autogenerated, as is the case in your scenario.  In those situations, we always want to guard against referring to Fields by first checking that we are allowed to, via the useFields boolean.  I simply forgot to do so here.  If you look further down in the code below that "if" you'll see useFields is used properly in other "if" checks that are similar.  Again, sorry for the inconvenience caused by this oversight.

    Please post here if you run into further problems with this scenario.  It's always possible that I'm out-to-lunch so-to-speak with my diagnosis and solution... though I was able to duplicate your exact error message here by adding AutoGenerateInsertButton="true" to a copy of DetailsView.aspx (the sample page for the DetailsView adapter from the kit).

    Russ Helfand
    Groovybits.com
  • Re: Got exception when enabling INSERT in DetailView

    01-07-2007, 6:59 PM
    • Member
      point Member
    • marcuskn
    • Member since 12-30-2006, 2:24 PM
    • Posts 2

    Hi Russ, It's all good now after putting your fix in.  Thank you very much.

     

  • Re: Got exception when enabling INSERT in DetailView

    01-17-2007, 6:10 PM
    • Member
      18 point Member
    • Trevor311
    • Member since 12-13-2006, 7:13 AM
    • Posts 7

    I wanted to take this a step further and list out a possible fix for another bug in this adapter:

     

    if ((useFields || iRow < ControlAsDetailsView.Fields.Count) &&
           (!ControlAsDetailsView.AutoGenerateRows) &&
           ((row.RowState & DataControlRowState.Insert) == DataControlRowState.Insert) &&
           (!ControlAsDetailsView.Fields[row.RowIndex].InsertVisible))
          {
           continue;
          }

     

    Note that I have replaced "useFields" with "useFields || iRow < ControlAsDetailsView.Fields.Count.  This is because if you have more rows than fields (such as when the insert button is visible), then the code above to ignore elements with InsertVisible == false in InsertMode is bypassed and these fields are still rendered.

     This is possibly simply a quick-fix/hack and may be buggy itself, but it does fix the issue in my own application.

Page 1 of 1 (4 items)