1

I would like to Implement the following feature: when a user selects a movie from the dropdownlist and clicks on the ‘Add to cart’ button, the movie is then added into the list box below.

protected void addImageButton_Click(object sender, ImageClickEventArgs e)
{
    string[] moviesArrayString = new string[10];

    moviesArrayString[1] = "Badboy 2";
    moviesArrayString[2] = "BadBoy 3";
    moviesArrayString[3] = "The Godfather";
    moviesArrayString[4] = "Inception";
    moviesArrayString[5] = "The Shawshank Redemtion";
    moviesArrayString[6] = "Star Wars";
    moviesArrayString[7] = "The Metrix";
    moviesArrayString[8] = "King Kong";
    moviesArrayString[9] = "Point Break";
    moviesArrayString[10] = "Top Gun";


    cartListBox.Items.AddRange(moviesArrayString);
}

It give me some error and I couldn't figure out how to do so? error is index was outside the bound of arrays?

2
  • You need to be a little more descriptive in your description of "some error" that's a little vague. Commented Sep 28, 2013 at 2:08
  • Are you use multiselect dropDownList ryt ? Commented Sep 28, 2013 at 6:25

3 Answers 3

3

Arrays in C# are zero-based. Use the indexes from 0 to 9:

protected void addImageButton_Click(object sender, ImageClickEventArgs e)
{
    string[] moviesArrayString = new string[10];

    moviesArrayString[0] = "Badboy 2";
    moviesArrayString[1] = "BadBoy 3";
    moviesArrayString[2] = "The Godfather";
    moviesArrayString[3] = "Inception";
    moviesArrayString[4] = "The Shawshank Redemtion";
    moviesArrayString[5] = "Star Wars";
    moviesArrayString[6] = "The Metrix";
    moviesArrayString[7] = "King Kong";
    moviesArrayString[8] = "Point Break";
    moviesArrayString[9] = "Top Gun";

    cartListBox.Items.AddRange(moviesArrayString);
}

Alternatively, you can use:

string[] moviesArrayString =
{
    "Badboy 2", "BadBoy 3", "The Godfather", "Inception",
    "The Shawshank Redemtion", "Star Wars", "The Matrix", "King Kong", "Point Break", "Top Gun"
};
Sign up to request clarification or add additional context in comments.

2 Comments

The index starts at zero meaning moviesArrayString[0] to moviesArrayString[9]. You're referencing moviesArrayString[10] in your code, that element does not exist. Hence you get the out-of-bound exception.
What does this question have to do with the array indexes you're using?
0

Just set the DataSource instead of adding item into listBox......

string[] moviesArrayString =
{
    "Badboy 2", "BadBoy 3", "The Godfather", "Inception",
    "The Shawshank Redemtion", "Star Wars", "The Matrix", "King Kong", "Point Break", "Top Gun"
};

 listBox1.DataSource = moviesArrayString;

Comments

-1

Use List<> or ArrayList in the place of string[].

For Example:

ArrayList lst=new ArrayList();
lst.Add("badboy2");
lst.Add("badbo32");
lst.Add("badb2");
lst.Add("bboy2");
lst.Add("bad2");
foreach(string str in lst)
{
cartListBox.Items.Add(str);
}

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.