4 Replies - 27045 Views - Last Post: 23 April 2013 - 10:20 AM Rate Topic: -----

#1 newdream   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-April 13

index was out of range. must be nonnegative and less than ...

Posted 23 April 2013 - 01:08 AM

Hi
I am geeting this error for one in thousands messages insert in data
index is out of range.must be non-negative and less than the size of the collection
parameter name:index

Any suggestions to solve this error would be f great help.
i am getting the error in the catch statetment in the below lines:-


 if (currentMsgcounter >= msgPerXMLFile || CASQueue.CurrentDepth == 0)
                                {
                                    string spErrMsg = "";

                                    try
                                    {
                                        RSEM_Function dbCall = new RSEM_Function();

                                        //write the RSEM MQ messages to the file with millisecond timestamp
                                        //AddDebugLine(" write the RSEM MQ messages to the  msg logs file with millisecond timestamp", true);
                                        MsgFile = Path.Combine(filePath + @"\RSEMEQP_MsgLogs", string.Format("RSEMQMsg-{0:yyyy-MM-dd_H-mm-ss-ff}.txt", DateTime.Now));
                                        logfile = new RSEM_FileLog(MsgFile);
                                        logfile.writeFile(RSEMcompleteMsg.ToString(), "COMPLETE");

                                        //calling the function to insert the Data to T1 table
                                        //int recordsInserted = dbCall.insertValues(sConnectionString, xmlFile.ToString());

                                        db = new SqlConnection { ConnectionString = sConnectionString };

                                        SqlCommand cmd = new SqlCommand("feed.spRSEM_Receive", db);
                                        //AddDebugLine("insert data to RSEM Receive", true);
                                        //calling the SP for to insert the values in RSEM staging table
                                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                                        //may not required
                                        //string strXml = data.ToString();

                                        // converting XML file to string
                                        string @xmlString = xmlFile.ToString();
                                        cmd.Parameters.Add(new SqlParameter("@xmlString", @xmlString));

                                        SqlParameter outParam = cmd.Parameters.Add("@recCount", SqlDbType.Int);
                                        outParam.Direction = System.Data.ParameterDirection.Output;

                                        SqlParameter outErr = cmd.Parameters.Add("@err", SqlDbType.NVarChar, 1000);
                                        outErr.Direction = System.Data.ParameterDirection.Output;

                                        cmd.CommandTimeout = 600;

                                        db.Open();
                                        cmd.ExecuteNonQuery();
                                        int recordsInserted = Int32.Parse(cmd.Parameters["@recCount"].Value.ToString());
                                        spErrMsg = cmd.Parameters["@err"].Value.ToString();
                                        db.Close();

                                        //Replace the UTF-16 encoding with standard one i.e. UTF-8
                                        xmlFile.Replace(Encoding.Unicode.WebName, Encoding.UTF8.WebName, 0, 56);

                                        if (spErrMsg == "" && recordsInserted == 99999)
                                        {
                                            //write the processed message to the Processed file with millisecond timestamp
                                           // AddDebugLine("write the processed message to the Processed file with millisecond timestamp", true);
                                            procFile = Path.Combine(filePath + @"\RsEMEQP_ProcessedLogs", string.Format("Processed-{0:yyyyMM-dd_H-mm-ss-ff}.xml", DateTime.Now));
                                            logfile = new RSEM_FileLog(procFile);
                                            logfile.writeFile(xmlFile + "\n", "COMPLETE");
                                        }
                                        else
                                        {
                                            elog.WriteErrorToLog(spErrMsg, 4);
                                        }

                                    }
                                    [color="#C0C0C0"]catch (Exception ex)
                                    {
                                        AddDebugLine("Error while Inserting the messages: " + ex.Message,true);
                                        var strError = " ";
                                        strError = ("Error while Inserting the messages: " + ex.Message);
                                        elog.WriteErrorToLog(strError, 4);
                                    }[/color]
                                    xmlFile.Remove(0, xmlFile.Length);
                                    RSEMcompleteMsg.Remove(0, RKEMcompleteMsg.Length);
                                }

This post has been edited by tlhIn`toq: 23 April 2013 - 05:26 AM
Reason for edit:: Code tags added


Is This A Good Question/Topic? 0
  • +

Replies To: index was out of range. must be nonnegative and less than ...

#2 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: index was out of range. must be nonnegative and less than ...

Posted 23 April 2013 - 03:06 AM

Error is self explanatory, you access the elements in array through index and they must be > 0 and less then the size of array and you violated that rule somewhere, and please use the code tags it makes it more readable
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: index was out of range. must be nonnegative and less than ...

Posted 23 April 2013 - 03:37 AM

Not always true, to say that the Indexer indices have to be in the range 0 <= i < collection.Count

Arrays can have negative indices.
Spoiler

This post has been edited by AdamSpeight2008: 23 April 2013 - 03:39 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: index was out of range. must be nonnegative and less than ...

Posted 23 April 2013 - 05:29 AM

Remember that arrays are zero indexed. This trips up a lot of rookies. If you have 10 items in your array they are indexed 0-9. So while you have 10 items, there is no element index 10 because that would be the 11th item.

Put in a breakpoint, walk through your code, look at the value of the index at run-time and you should be able to find where you are setting the index out of the bounds of your array.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Apr 2013
Spoiler



Was This Post Helpful? 1
  • +
  • -

#5 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: index was out of range. must be nonnegative and less than ...

Posted 23 April 2013 - 10:20 AM

I really appreciate that you keep updating your list tlhIn`toq
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1