• Register
0 votes
41 views

Problem :

 

I want to add the data as one by one row to my datagridview.  But I am facing the following error:

"Index was out of range. Must be non-negative and less than the size of the collection parameter name: index"

Why am I facing this error ? Do I need to make anychanges in my following code:

String Sqlstr = "select ItemName from Item where ItemID = '" + tbItemID.Text + "'";
db.DataRead(Sqlstr);
string ItemName = db.dr["ItemName"].ToString(); 
DataGridView myDataGridView = new DataGridView();
myDataGridView.Columns[0].Name = "ItemID";
myDataGridView.Columns[1].Name = "ItemName";
myDataGridView.Columns[2].Name = "Qty";
myDataGridView.Columns[3].Name = "UnitPrice";
myDataGridView.Columns[4].Name = "Amount";
string ColumOne = tbItemID.Text;
string ColumTwo = ItemName;
string ColumThree = tbQuantity.Text;
string ColumFour = Convert.ToString(UnitPrice);
string ColumFive = Convert.ToString(sum);
string[] row = new string[]{ ColumOne, ColumTwo, ColumThree, ColumFour, ColumFive };
myDataGridView.Rows.Add(row);
6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

Your error is saying "The index is out of range". It means you wanted to index an object with a value that was invalid. If you have five Chairs, and I ask you to give me the sixth Chair, then you will look at me in funny manner. Same is your computer looking at you in funny way. You asked him - "create a collection" So it created one for you but initially the collection is empty: and there is nothing in it  and no space to hold anything. "It has no hands".

After that you told him "Now the first element of the collection is the 'ItemID'". But the computer says "You never asked me to create space for a 'first item'." I have no hands to grab this item which you are giving me.

So in terms of your code, you just created the view, but never specified its size. You need the

myDataGridView.ColumnCount = 5;

Before even trying to access the columns. Pleaase update

DataGridView myDataGridView = new DataGridView();
myDataGridView.Columns[0].Name = "ItemID";

to the below code:

DataGridView myDataGridView = new DataGridView();
myDataGridView.ColumnCount = 5;
myDataGridView.Columns[0].Name = "ItemID";
9 7 4
38,600 points

Related questions

0 votes
1 answer 82 views
82 views
Problem : I am facing this Kind of Error like:: Specified argument was out of the range of valid values.Parameter name: site While Debugging any of the Project. I have also tried after Reinstalling the Visual ... +9873912 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
asked Jan 20, 2020 jwilliam 3.9k points
0 votes
1 answer 103 views
103 views
Problem : I am getting bellow error related to datetime the conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
1 answer 15 views
15 views
Problem: I am new to programming and currently working with arrays. I declared and initialized an array but the compiler threw “index was outside the bounds of array” error. Following is the declaration and definition of my array: int[] array = new int[6]; ... The error is occuring on last line. What does this error mean and what am I doing wrong? If someone knows, please explain.
asked Nov 3, 2020 Code Learner 5.1k points
0 votes
1 answer 20 views
0 votes
1 answer 25 views
25 views
Problem : I have written some code and when it executes, it throws the IndexOutOfRangeException, saying, Index was outside the bounds of the array. What does this actually mean, and what can I do to fix it? Depending on the classes used it can also be the ... not handled in user code Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
asked Jan 20, 2020 jwilliam 3.9k points
0 votes
1 answer 20 views
20 views
Problem: I have a dataset (as a .txt file) used to generate a histogram, but the log asked me to normalize the data and plot a histogram of the normalized data instead. whereas, what I want "AttributeError: max should be bigger than least in the range parameter." error ... figure() plt.subplot(111) plt.hist(norm1, nbins1, color='purple', alpha=0.5) plt.ylabel('Frequency', fontsize=20) plt.show()
asked Dec 24, 2020 sasha 5.3k points
0 votes
1 answer 20 views
20 views
Problem: I have recently joined a new project in WPF C# and at this time I am trying to animate the image to simply move down. I have already found out the "MoveTo" function on an Internet but when I pasted it in my code then the below error occurred: “ ... button_Click(object sender, RoutedEventArgs e)     {         MoveTo(image, 130);     } } Kindly help me in fixing my above C# code.
asked Jul 28, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 41 views
41 views
Problem : I want to create the simple user control that should be the slider. When I try to add the AjaxToolkit SliderExtender to my user control I usually get below error: Server Error in '/' Application. The Controls collection cannot be modified because ... control and also tried adding the textbox and the slider extender to my placeholder programmatically but I am still facing the above error.
asked Dec 14, 2019 alecxe 7.5k points