Getting Error Object reference not set to an instance of an object when populating data table

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
see the code

DataTable dt = new DataTable();

var AllQuater = _FundamData.Where(b=> b.AnnualQuater=="Q").Select(a=> a.Period).Distinct()
.OrderBy(s => Regex.Match(s, @"\d\d\d\d").Value)
.ThenBy(s => Regex.Match(s, @"^.Q").Value + '5')
.ThenBy(s => !Regex.Match(s, @"FYA$").Success)
.ThenBy(s => !Regex.Match(s, @"FY$").Success)
.ToList().TakeLast(8);

var AllFY = _FundamData.Where(b => b.AnnualQuater == "A").Select(a => a.Period).Distinct()
.OrderBy(s => Regex.Match(s, @"\d\d\d\d").Value)
.ThenBy(s => Regex.Match(s, @"^.Q").Value + '5')
.ThenBy(s => !Regex.Match(s, @"FYA$").Success)
.ThenBy(s => !Regex.Match(s, @"FY$").Success)
.ToList().TakeLast(2);

try
{
dt.Columns.Add("FORM NAME", typeof(string));
dt.Columns.Add("As Reported Date", typeof(string));
dt.Columns.Add("StandardCode", typeof(string));
dt.Columns.Add("StandardName", typeof(string));
dt.Columns.Add("AsReportedCode", typeof(string));
dt.Columns.Add("AsReportedName", typeof(string));

foreach (var q in AllQuater)
{
dt.Columns.Add(q, typeof(string));
}

foreach (var fy in AllFY)
{
dt.Columns.Add(fy, typeof(string));
}

foreach (var item in _FundamData)
{
DataRow row = dt.NewRow();
row["FORM NAME"] = item.FORMNAME;
row["As Reported Date"] = item.AsReported_Date;
row["StandardCode"] = item.StandardCode;
row["StandardName"] = item.StandardName;
row["AsReportedCode"] = item.AsReportedCode;
row["AsReportedName"] = item.AsReportedName;
row[dt.Columns[item.Period].Ordinal] = item.Value;

//row[row.Table.Columns["ColumnName"].Ordinal + 1] = someOtherValue;
dt.Rows.Add(row);
}
}
catch(Exception ex)
{

}
this line throwing error row[dt.Columns[item.Period].Ordinal] = item.Value;



how to fix it. thanks

Continue reading...
 
Back
Top