Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
for (int i = 0; i < 2; i++) // Loop for Row
            {
                TableRow row = new TableRow();
                for (int j = 0; j < 1; j++) // Loop for Column
                {
                    TableCell cell = new TableCell();

                    Label lbl = new Label();
                    lbl.Text = i.ToString();
                    cell.Controls.Add(lbl);
                    arrLIst1.Add(i);
                    arrLIst1[0] = "Name0";
                    arrLIst1[1] = "Name1";//hear i got this error message
                 }
              }

I need id 0 = name1 and id 1 = name2. how to get it.
Error Message
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


output:
0 textbox
1 textbox

i need: name0 textbox
name1 textbox
Posted
Updated 17-Jul-12 18:16pm
v3
Comments
Trak4Net 18-Jul-12 0:22am    
I don't see where arrLIst1 is declared. More than likely the array arrLIst1 does not have 3 items in it to assign a value to. In your code example you are assigning value to arrLIst1 index 0 and arrLIst1 index 2 (nothing assigned to arrLIst1 index 1). Not sure if maybe you are attempting to create a multidimensional array?

1 solution

I think you need to read a basic programming book before flooding this site with inane questions.

C#
arrLIst1[0] = "Name1";
arrLIst1[2] = "Name2";//hear i got this error message


What do you notice about your indexes ? It goes from 0 to 2. The error means what it says. Actually, because you only add one thing to arrLIst1 ( seriously, what sort of variable name is that ? ), it's possible that it only contains 1 items, and that the index of 1 would also fail.

In short, I think you're flailing in the dark and need to learn some basics and then rethink what you're doing. This is sloppy, messy, and makes no sense that I can see, and if you can't understand that error message, then you don't know the most basic things about programming. You're clearly not being paid for this code, if you're self teaching, don't give up, but slow down and learn some basics. If it's for a class, talk to your teacher and ask him for help/recommended reading.

I do think I see the issue though.

lbl.Text = i.ToString();

This needs to be

lbl.Text = "name" + i.ToString();

That you don't see this, tells me you don't know how to use a debugger, or how to program, you're just guessing.
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 18-Jul-12 0:27am    
Well suggestion, well answer, well a +5!
[no name] 18-Jul-12 16:22pm    
Welcome back Christian

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900