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

  • Home
  • SEARCH
  • 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 8495989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:43:36+00:00 2026-06-10T23:43:36+00:00

I have a multiple values in ListBox. When I am Selecting a Single Value

  • 0

I have a multiple values in ListBox. When I am Selecting a Single Value my code is working fine.

But when I am Selecting Multiple Values it is giving me this Exception:-

Index was outside the bounds of the array.

My code is:

if (submitButton == "Enroll Trainee")
{
    if (Request.Form["NonEnroll"] != null)
    {
        int i = 0;
        string[] selected = Request.Form["NonEnroll"].Split(',');
        if (selected != null)
        {
            if (selected.Count() != 0)
            {
                foreach (var item in selected)
                {
                    enrollDetails.TraineeID = Convert.ToInt32(item[i].ToString());//Getting Exception here
                    enrollDetails.TrainerID = Convert.ToInt32(Session["user"].ToString());
                    enrollDetails.dt = DateTime.Now;
                    db.EnrollTrainee.Add(enrollDetails);
                    db.SaveChanges();
                    i++;
                }

            }
        }
        populatelistbox();
        return View();
    }
}

During the First Iteration it is working fine and also save first iteration result in my database. But when it starts second iteration it give me the above exception

  • 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-10T23:43:37+00:00Added an answer on June 10, 2026 at 11:43 pm

    You misused the loop variable, I guess you need:

    enrollDetails.TraineeID = Convert.ToInt32(item);
    

    Let me explain why you original code item[i].ToString() didn’t worked:

    Lets assume you got the list "2,1,3" then with the string split you have created the array of strings : new [] { "2", "1", "3" }

    Then in your loop

    1. In the first iteration

      • item was "2"
      • i was 0

      So item[i] resolved in "2"[0] which is "2" and it worked.

    2. In the second iteration

      • item was "1"
      • i was 1

      and your code resolved in "1"[1] which thrown an exception because "1" is only one character long so the Index was outside the bounds of the array.

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

Sidebar

Related Questions

I am trying to have multiple values showing in a alertbox but the last
I have inserted this form with multiple checkbox items which collects some values, What
I have a listbox that a user can select multiple values from . each
I have a listbox bound to a view model observable collection: This works fine,
I have an asp:ListBox that contains multiple values. A user is required to always
i have a list: <asp:ListBox ID=lstProblems runat=server SelectionMode=Multiple></asp:ListBox> and i am populating it this
I have multiple arrays with string values and I want to compare them and
I have a form with multiple dropdowns consisting of multiple values. I'm trying to
I have added a parameter to my report with the option Allow Multiple Values
I have a function that uses out parameters to return multiple values to the

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.