Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8228269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:24:32+00:00 2026-06-07T16:24:32+00:00

Error is: Index was out of range. Must be non-negative and less than the

  • 0

Error is: Index was out of range. Must be non-negative and less than the size of the collection.

Scenario: I have a desktop application which loads XML Files and display the data in Grid. Now, I want to insert another file and want to append the data in both files. But, when I try to merge the data (I mean add the rows to DataTable which has rows of perviously opened file)…I am getting this error.

if (strPreviousFile != "")   
{  
  dgvBooksDetails.DataSource = dtBooks;   
  int intCurrentRows = dgvBooksDetails.Rows.Count;   
  intBooksCounter = intBooksCounter + intCurrentRows;   
  for (int c = intCurrentRows; c < intBooksCounter; c++)  
  {   
    Book objBook = new Book();   
    objBook.ID = BookID[c];   
    objBook.Title = BookTitle[c];  
    objBook.Author = BookAuthor[c];  
    objBook.Genre = BookGenre[c];  
    objBook.Price = Double.Parse(BookPrice[c]);  
    objBook.PublishDate = DateTime.Parse(BookPublish_Date[c]);  
    objBook.Description = BookDescription[c];  
    dtBooks.Rows.Add(objBook.ID, objBook.Title, objBook.Author, objBook.Genre,
                     objBook.Price, objBook.PublishDate, objBook.Description);   
  }
}

How can I overcome this error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:24:37+00:00Added an answer on June 7, 2026 at 4:24 pm

    The line causing trouble is this:

    int intCurrentRows = dgvBooksDetails.Rows.Count;
    

    You take this value as start for your loop. However, the rows collection counts from 0 to Count-1, so using Count to access a value of the rows collection causes an index out of bounds error.

    Another thing: BookTitle, BookAuthor etc. are also indexed from 0 to Count-1 (or Length-1 if they are arrays). I’m not sure from what you’ve told us, but are you sure that these collections can be accessed by index the way you do? I mean, could it be they only contains the items to be added and thus need to be indexed from 0 to X instead of number of existing items to number of existing items + number of new items (that’s what you do in your code)?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Error: Index was out of range Must be non-negative and less than the size
I am getting Index was out of range. Must be non-negative and less than
I am getting an error: Index was out of range. Must be non-negative and
I receive the exception Index was out of range. Must be non-negative and less
Why do i get this error(Index was out of range. Must be non-negative and
My soluton here: http://www.mediafire.com/?rzrhvc71musz793 I get error Additional information: Index was out of range.
playerProfileID = (int)transfersDGV.SelectedRows[transfersDGV.CurrentRow.Index] .Cells[0].Value; I get an error saying: Index was out of range.
Please, what is wrong with this: Form2_Closing: Form1.DataGridView1.Rows[0].Cells[1].Value = 323; Error: Index was out
At runtime the program says an index is out of range, but I don't
When I run php-closure i get a PHP error Undefined index: HTTP_IF_NONE_MATCH in <b>/php-closure.php</b>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.