[Solved] ASP.Net Error: System.IndexOutOfRangeException: Index was outside the bounds of the array

comunidadmexicanaroma
 
on Mar 23, 2021 05:36 AM
1853 Views

Hi,

I want to dynamically generate a new Row in GridView respecting the sequence of the column sID_c.

The reference is

Add distinct Row to DataTable and display in ASP.Net GridView using C# and VB.Net

I have using this example but in my case I have this error

System.IndexOutOfRangeException: Index was outside the bounds of the array

on this line of my code

dr["contents"] = string.Format("New row Thread {0}", (row.FindControl("lblContents") as Label).Text.Split('.')[1]);

My complete code below

Any help would greatly appreciate.

Thank you.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 23, 2021 05:45 AM
comunidadmexicanaroma says:
dr["contents"] = string.Format("New row Thread {0}", (row.FindControl("lblContents") as Label).Text.Split('.')[1]);

Replace with below code.

 string[] contents = (row.FindControl("lblContents") as Label).Text.Split('.');
 dr["contents"] = string.Format("New row Thread {0}", contents.Length > 1 ? contents[1] : contents[0]);