System.IndexOutOfRangeException on a webfarm


Here is another issue that a customer had that I thought would be helpful to others.

Problem

The customer said they are getting Exception type: System.IndexOutOfRangeException every minute or so when accessing their web site.  The error looks like:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
  at System.Web.UI.WebControls.GridView.LoadControlState(Object savedState)
  at System.Web.UI.Control.LoadControlStateInternal(Object savedStateObj)
  at System.Web.UI.Page.LoadAllState()
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Resolution

This is trying to load ViewState.  ViewState is not necessarily compatible between different versions of the runtime, including service pack changes. A given application, even if distributed in a web farm, must be running on the same exact runtime version on all servers.  This includes the bit-ness.  So they all need to be x86 or x64 as on the same version (Service Pack, etc).

Comments (6)

  1. Ramendra Kumar says:

    It gives An Exception for System.IndexOutOfRangeException:

    Sql Field IS int also. Could You Help ME

    totalsponsoe = Convert.ToInt32(sdr["AppMstSponsorTotal"]);

  2. Rob Awe says:

    There’s probably a typo in your field name.

  3. Good point Rob, remember that the SQL field names are case sensative.

  4. Ariady Putra says:

    my problem is similar to Ramendra  Kumar, but i only get the System.IndexOutOfRangeException if i try to call <b>char</b> or <b>text</b> data type  from SQL. other fields using int or varchar no problem. i’ve checked over and over the field name.

    bceData.Answer = sqlDataReader["AnswerField"].ToString();

    the "AnswerField" is <b>text</b> datatype in SQL. can you help me please?

    i have no permission to alter the database.

  5. Ariady Putra says:

    alright, please forget my previous comment 😀 the problem was in the stored procedure 😀 now everything is solved 😀 i’m still learning anyway 😀

  6. swapneel says:

    sql = “select Rlens_code,Llens_code from Spects where ” & oprator & ” = ‘” & Search & “‘”

           CM = New SqlCommand(sqlstr, frmMasterObj.cn)

           Dim dr1 As SqlDataReader = CM.ExecuteReader

           If dr1.HasRows = True Then

               While (dr1.Read)

                   If dr1(“Llens_code”) = “” Then

                       lblRtLensRef.Text = “”

                   Else

                       lblLtLensRef.Text = dr1(“Llns_code”).ToString()

                   End If

                   If dr1(“Rlens_code”) = “” Then

                       lblLtLensRef.Text = “”

                   Else

                       lblRtLensRef.Text = dr1(“Rlens_code”).ToString()

                   End If

               End While

               dr1.Close()

           End If

    in this code if some value is null so how can we catch that null value