0

Sometimes I get this error in the server:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
-StackTrace-
at System.Collections.BitArray.Get(Int32 index)
at System.Data.DataSet.MarkModifiedRows(TableChanges[] bitMatrix, DataRowState rowStates)
at System.Data.DataSet.GetChanges(DataRowState rowStates)
at System.Data.DataSet.GetChanges()
at MapNetService.DataAccess.GPSDataProcessingProvider.Flush(String name)

I have two servers (production and test) that handle some data and mostly get this error only in one server

The function is very simple

 public void Flush(string name)
        {           
            bool isSuccess = true;
            try
            {
                GPSDataProcessingDS changedData = (GPSDataProcessingDS)gpsData.GetChanges();
                gpsData.AcceptChanges();

                RemoveExcessiveRows(name, gpsData.tblGPSReport);
                RemoveExcessiveRows(name, gpsData.tblIOReport);

                if (changedData != null
                    && (changedData.tblGPSReport.Count > 0 || changedData.tblIOReport.Count > 0
                                                           || changedData.tblLastGPSReadings.Count > 0))
                {
                    isSuccess = SaveChanges(changedData);
                }
                else
                {
                    Trace.WriteLine("No changes to flush.");
                }
            }
            catch (Exception e)
            {
                Logger.Log(name, "Flushing exception:" + e.Message);
                try
                {
                    if (gpsData.HasErrors)
                    {
                        if (gpsData.tblGPSReport.GetErrors().Length > 0)
                        {
                            foreach (var error in gpsData.tblGPSReport.GetErrors())
                            {
                                Logger.Log(name, "Error:" + error.RowError);
                            }
                        }
                        if (gpsData.tblIOReport.GetErrors().Length > 0)
                        {
                            foreach (var error in gpsData.tblIOReport.GetErrors())
                            {
                                Logger.Log(name, "Error:" + error.RowError);
                            }
                        }
                        if (gpsData.tblLastGPSReadings.GetErrors().Length > 0)
                        {
                            foreach (var error in gpsData.tblLastGPSReadings.GetErrors())
                            {
                                Logger.Log(name, "Error:" + error.RowError);
                            }
                        }
                    }
                }
                catch
                {
                    // ignored
                }
                gpsData.RejectChanges();
                throw;
            }
        }

The GetErrors() return nothing

Thanks a lot

  • Is issue occurring when application starts? When a constructor is called often events will occur during the construction stage of the application. The gpsDataObject could be empty which is giving the error which can either happen when the application is running or when the class is being constructed. – jdweng Mar 08 '20 at 09:43
  • thank for your comment, the error occur periodically on going, not on the start. – user3755993 Mar 08 '20 at 10:06
  • Still. Is gpsData null or contain no data? – jdweng Mar 08 '20 at 12:11
  • For sure not null, probably have a data (we found the error because some data was lost after RejectChanges) – user3755993 Mar 08 '20 at 13:08
  • The exception is occurring when the method GetChanges is calling MarkModifyRows. When a DataSet is modified in Net, each rows that is modified is marked with the boolean that the row was changed. Then when the update is performed each modify row is then put back into the database (from the dataset). So you are getting the exception "Must be non-negative and less than the size of the collection." I don't think in this case it is negative. I think a row got deleted but something is still references the deleted row. – jdweng Mar 08 '20 at 18:21
  • See : https://stackoverflow.com/questions/45045/what-can-i-do-to-resolve-a-row-not-found-or-changed-exception-in-linq-to-sql-o and https://learn.microsoft.com/en-us/archive/blogs/spike/system-data-linq-changeconflictexception-row-not-found-or-changed-finding-the-culprit – jdweng Mar 08 '20 at 18:21

0 Answers0