Как удалить исключение массива index out of bounds
исключение index out of range:индекс находится за пределами массива
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(); }
Что я уже пробовал:
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(); }