3

I have dynamically created textbox in asp.net. Now i am extracting the values through following code.

string[] sublist = new string[] { };
int[] maxmarkslist = new int[] { };
int i;
for (i = 0; i < Convert.ToInt32(Label15.Text); i++)
{
    string sub = "subject" + i;
    string marks = "maxmarks" + i;
    TextBox subject = (TextBox)PlaceHolder1.FindControl(sub);
    TextBox maxmarks = (TextBox)PlaceHolder1.FindControl(marks);
    sublist[i] = subject.Text;
    maxmarkslist[i] = Convert.ToInt32(maxmarks.Text);
}

But I getting error "Index was outside the bounds of the array" for the below two lines:

sublist[i] = subject.Text;
maxmarkslist[i] = Convert.ToInt32(maxmarks.Text);

When I debugged it, values are coming in subject.Text and maxmarks.Text but not going to array.

Did I define the array in a wrong way?

3
  • I'm thinking Label15.Text doesn't have the value you think it does. Debug to see what value i is when you get the error Commented Nov 29, 2012 at 16:03
  • label15 has the value... Commented Nov 29, 2012 at 16:08
  • @LittleBobby that's the first tag which appears when writing just "asp". Many newbiews choose this by mistake. Commented Dec 2, 2012 at 8:16

2 Answers 2

2

You define both the arrays as empty arrays. So you will get index out of bound erros if you try to index into those.

Arrays are not dynamically expanding. If you want that, use a collection type and may be later convert to an array.

Try this:

int length = Convert.ToInt32(Label15.Text);
string[] sublist = new string[length-1];
int[] maxmarkslist = new int[length-1];

for (int i = 0; i < length; i++)
{
    string sub = "subject" + i;
    string marks = "maxmarks" + i;
    TextBox subject = (TextBox)PlaceHolder1.FindControl(sub);
    TextBox maxmarks = (TextBox)PlaceHolder1.FindControl(marks);
    sublist[i] = subject.Text;
    maxmarkslist[i] = Convert.ToInt32(maxmarks.Text);
}

Or here is how to do this with a collection (List) type:

        int length = Convert.ToInt32(Label15.Text);
        List<string> sublist1 = new List<string>();
        List<int> maxmarkslist1 = new List<int>();

        for (int i = 0; i < Convert.ToInt32(Label15.Text); i++)
        {
            string sub = "subject" + i;
            string marks = "maxmarks" + i;
            TextBox subject = (TextBox)PlaceHolder1.FindControl(sub);
            TextBox maxmarks = (TextBox)PlaceHolder1.FindControl(marks);
            sublist1.Add(subject.Text);
            maxmarkslist1.Add(Convert.ToInt32(maxmarks.Text));
        }

        string[] sublist = sublist1.ToArray();
        int[] maxmarkslist = maxmarkslist1.ToArray();

Note with collections you dont have to specify the size upfront. But keep adding items to it as it can expand as needed. But arrays can not do this.

Your string[] sublist = new string[] { }; is a shortcut method where you create and initialize the array. In that you don't have to specify the size, but compiler will count the elements between {} and set the size appropriately. In your case since there are no elements inside {} it will create an empty array.

Sign up to request clarification or add additional context in comments.

1 Comment

how to use collection type here.m novice to .net...plz help
1
        string[] sublist = new string[100];
        int[] maxmarkslist = new int[100];

Put this..replace 100 with the max possible value of your loop...but this is not a good practice...will come back to this thread if i found something better...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.