Last string not getting added to datatable

Padmanabhan, Venkatesh 120 Reputation points

Hi. I am creating a string array from web.config and then trying to create a datatable from it. The below code is able to create the datatable, but the last item in the string array is not getting added. How to fix this ?

<section name="T1" type="System.Configuration.NameValueSectionHandler"/>

<add key="PDaily" value="PDaily,Daily,Region,WWP,WWPDA,WWPDI5" ></add>
<add key="PWeekly" value="PWeekly,Weekly,Region,WWPDD,WWPF6D" ></add>
<add key="PThales" value="PThales,Daily,Region,WWPDAI,WF7D,WWIP7D,WWPDA7D,WWPB7D,WWPC7D" ></add>
<other keys in similar nature>

// c# code

string[] pDailyvalues = new string[] { };
 table = new DataTable();
table.Columns.Add("Application", typeof(string));
table.Columns.Add("Frequency", typeof(string));
table.Columns.Add("Region", typeof(string));
 table.Columns.Add("JobName", typeof(string));

 var applicationSettings = ConfigurationManager.GetSection(ddlAppln.SelectedValue.ToString().Trim()) as NameValueCollection; 

foreach (string str in applicationSettings[key].ToString().Split(','))
     pDailyvalues = pDailyvalues.Append<string>(str).ToArray();
 for (int a = 3; a < pDailyvalues.Length - 1; a++)
 DataRow _approw = table.NewRow();
 _approw["Application"] = pDailyvalues[0].ToString();
 _approw["Region"] = pDailyvalues[2].ToString();
 _approw["Frequency"] = pDailyvalues[1].ToString();
 _approw["JobName"] = pDailyvaluess[a].ToString();
 if (_approw["JobName"].ToString().Contains(']'))
_approw["JobName"] = _approw["JobName"].ToString().Substring(0, _approw["JobName"].ToString().LastIndexOf("]")).TrimEnd();
 IEnumerable<string> items = pDailyvaluess.Skip(3);
 pDailyvaluess = items.ToArray();


An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,361 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112.9K Reputation points

    Try a < pDailyvalues.Length instead of a < pDailyvalues.Length - 1.