Click here to Skip to main content
15,920,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
index out of range exception:index is out of the bounds of array

C#
string s;
       string t;
       string[] a= new string[6];

       protected void Page_Load(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
           dt.Columns.AddRange(new DataColumn[7]{ new DataColumn("product_name"),new DataColumn("product_desc"),
               new DataColumn("product_price"),new DataColumn("product_qty"),
               new DataColumn("product_image"),new DataColumn("id"),new DataColumn("product_id") });
           if (Request.Cookies["aa"] != null)
           {
               s = Convert.ToString(Request.Cookies["aa"].Value);
               string[] strArr = s.Split('|');

               for (int i = 0; i < strArr.Length; i++)
               {
                   t = Convert.ToString(strArr[i].ToString());
                   string[] strArr1 = t.Split(',');

                   for (int j = 0; j < strArr1.Length; j++)
                   {
                       a[j] = strArr1[j].ToString();
                   }//getting here index out of bounds Exception kindly help me



                   dt.Rows.Add(a[0].ToString(), a[1].ToString(), a[2].ToString(),
                  a[3].ToString(), a[4].ToString(), i.ToString(),a[5].ToString() );//i because e want cookie index

               }

           }


           d1.DataSource = dt;
           d1.DataBind();

       }


What I have tried:

index out of range exception:index is out of the bounds of array

<pre lang="c#">

 string s;
        string t;
        string[] a= new string[6];
    
        protected void Page_Load(object sender, EventArgs e)
        {  
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[7]{ new DataColumn("product_name"),new DataColumn("product_desc"),
                new DataColumn("product_price"),new DataColumn("product_qty"),
                new DataColumn("product_image"),new DataColumn("id"),new DataColumn("product_id") });
            if (Request.Cookies["aa"] != null)
            {
                s = Convert.ToString(Request.Cookies["aa"].Value);
                string[] strArr = s.Split('|');
    
                for (int i = 0; i < strArr.Length; i++)
                {
                    t = Convert.ToString(strArr[i].ToString());
                    string[] strArr1 = t.Split(',');
    
                    for (int j = 0; j < strArr1.Length; j++)
                    {
                        a[j] = strArr1[j].ToString();
                    }//getting here index out of bounds
    
            
    
                    dt.Rows.Add(a[0].ToString(), a[1].ToString(), a[2].ToString(),
                   a[3].ToString(), a[4].ToString(), i.ToString(),a[5].ToString() );//i because e want cookie index
    
                }
    
            }
            
    
            d1.DataSource = dt;
            d1.DataBind();
    
        }
Posted
Updated 26-Dec-17 22:59pm
v2

Hi,

The error "Index Out of Bound Array Exception" comes when there are no data for that index.
Suppose the array length is 4, then it will start looping from 0 to 4. It means it will loop 5 times.

a[j] = strArr1[j].ToString();

if there are no data at strArr1[j].ToString(), then you will get this error.
Debug & Check what data is coming at strArr1[J].ToString() before getting exception.

If you you have any doubt, please let me know.

Regards,
Rajesh
 
Share this answer
 
Validate the index of before accessing it from the collection
for (int j = 0; j < strArr1.Length; j++)
            {
                  if(a.Length >j) // add this line to validate
                    a[j] = strArr1[j].ToString();
            }
 
Share this answer
 

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