locked
Index was out of range RRS feed

  • Question

  • HI,

    I am facing some problem with image button. I found an event log error.I am not able to reproduce this when i tried,but it is logging in my event log.



     

    Event time: 2008-05-04 08:53:57 PM -07:00
    Event type: Error
    Event source: Application
    User:
    Event ID: 0

    The message '0' for application 'Application' could not be formatted using library(ies): ''. The log entry contains the following replacement strings:'********************************************************************************Sunday, May 04, 2008Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index Error Guid:ac2fc6fe-0bd9-450d-8155-bbdd7479bf5b SOURCE:mscorlib STACK TRACE:   at System.Collections.ArrayList.get_Item(Int32 index)   at System.Web.UI.WebControls.DataGridItemCollection.get_Item(Int32 index)   at App_WebControls_Ctrl_ClientLookup.imgbtnCommClientID_Command(Object sender, CommandEventArgs e)Please provide the cause.

    Please find the below code and do needful.


    protected void imgbtnCommClientID_Command(object sender, CommandEventArgs e)
     {
      if (!bIsTrackerPage)
      {
       try
       {

        objCtrl_ErrorSummary.ClearError();
        lblFindByName.CssClass = "form_item_text";
        DataSet dsClientQuotesPolicies = null;
        int nSelectedClientID = Casting.ToInt(e.CommandArgument);
        int nRowNumber = 0;
        int nPreviousRow = 0;

        if (hidSelectedRowNumber.Value != null)
        {
         nRowNumber = Casting.ToInt(hidSelectedRowNumber.Value);
        }
        if (hidPreviousRowNumber.Value != null)
        {
         nPreviousRow = Casting.ToInt(hidPreviousRowNumber.Value);
        }

        dsClientQuotesPolicies = objClientController.GetClientPoliciesandQuotes(nSelectedClientID, 1);

        if (nPreviousRow > 0)
        {
                        if ((nPreviousRow != nRowNumber) && (grdComClientLookUp.Items.Count > (nPreviousRow - 1)))
         {
          ImageButton objimgPreviousRow = new ImageButton();
          objimgPreviousRow = (ImageButton)grdComClientLookUp.Items[nPreviousRow - 1].Controls[0].FindControl("imgbtnClientID");
          objimgPreviousRow.ImageUrl = "~/Images/icons/row_arrow_closed.gif";
          objPreviousComGrid = (DataGrid)grdComClientLookUp.Items[nPreviousRow - 1].Controls[0].FindControl("grdQuoteDetails");
          objPreviousComGrid.Visible = false;
         }
        }
                    if ((nRowNumber > 0) && (grdComClientLookUp.Items.Count > (nRowNumber-1)))
                    {
                        ImageButton objimgPresentRow = new ImageButton();
                        objimgPresentRow = (ImageButton)grdComClientLookUp.Items[nRowNumber - 1].Controls[0].FindControl("imgbtnClientID");
                        objimgPresentRow.ImageUrl = "~/Images/icons/row_arrow_open.gif";
                        objComClientDetails = (DataGrid)grdComClientLookUp.Items[nRowNumber - 1].Controls[0].FindControl("grdQuoteDetails");
                  

        if (!UISupport.IsEmptyDataSet(dsClientQuotesPolicies))
        {
         if (nPreviousRow != nRowNumber)
         {
          objComClientDetails.Visible = true;
          objComClientDetails.DataSource = dsClientQuotesPolicies;
          objComClientDetails.DataBind();
         }
        }
        else
        {
         objimgPresentRow.ImageUrl = "~/Images/icons/row_arrow_closed.gif";
        }
        if (nPreviousRow == nRowNumber && !UISupport.IsEmptyDataSet(dsClientQuotesPolicies))
        {
         if (objComClientDetails.Visible == true)
         {
          objComClientDetails.Visible = false;
          objimgPresentRow.ImageUrl = "~/Images/icons/row_arrow_closed.gif";
         }
         else
         {
          objComClientDetails.Visible = true;
          objimgPresentRow.ImageUrl = "~/Images/icons/row_arrow_open.gif";
         }
        }
                }
                    //System.Diagnostics.Process obj = new System.Diagnostics.Process()
                   
                    if ((nRowNumber != -1) && (nRowNumber != nPreviousRow) && (grdComClientLookUp.Items.Count > (nRowNumber - 1)))
                    {
                        grdComClientLookUp.Items[nRowNumber-1].CssClass = "GridSelectedStyle";
                        if ((nPreviousRow != -1) && (grdComClientLookUp.Items.Count > (nPreviousRow - 1)))
                        {
                            if (grdComClientLookUp.Items[nPreviousRow - 1].ItemType == ListItemType.AlternatingItem)
                                grdComClientLookUp.Items[nPreviousRow - 1].CssClass = "GridRowSelected";
                            else
                                grdComClientLookUp.Items[nPreviousRow - 1].CssClass = "GridRowUnselected";
         }
        }
       }
       catch (Exception ex)
       {
        UIExceptionManager.LogAndRedirect(ex, false, Context.Items["RequestID"].ToString());
       }
      }
     }

    Thanks



     

    • Moved by Andrew BrennerMicrosoft employee Friday, July 25, 2008 5:17 AM not suggest (Moved from Suggestions and Feedback for the Forums to Where is the Forum For…?)
    Thursday, July 24, 2008 1:24 PM

Answers

  • Looks like a pretty straight-forward bug to me. The index is out of bounds - nRowNumber or nPreviousRow is less than or equal to zero (but not -1):

    if((nRowNumber != -1) && (nRowNumber != nPreviousRow) && (grdComClientLookUp.Items.Count > (nRowNumber - 1)))  
    {  
        grdComClientLookUp.Items[nRowNumber - 1].CssClass = "GridSelectedStyle";  
        if((nPreviousRow != -1) && (grdComClientLookUp.Items.Count > (nPreviousRow - 1)))  
        {  
            if(grdComClientLookUp.Items[nPreviousRow - 1].ItemType == ListItemType.AlternatingItem)  
                grdComClientLookUp.Items[nPreviousRow - 1].CssClass = "GridRowSelected";  
            else 
                grdComClientLookUp.Items[nPreviousRow - 1].CssClass = "GridRowUnselected";  
        }  
    }

    Friday, July 25, 2008 3:31 PM