Last string not getting added to datatable

Padmanabhan, Venkatesh 120 Reputation points
2023-04-18T04:50:57.9366667+00:00

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 ?

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

<T1>
<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>
</T1>

// 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();
  }
table.Rows.Add(_approw);
 }
 IEnumerable<string> items = pDailyvaluess.Skip(3);
 pDailyvaluess = items.ToArray();

Thanks

C#
C#
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,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 114.7K Reputation points
    2023-04-18T05:24:21.0366667+00:00

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