relatively new to programming and have researched this but cannot resolve my problem. Feeling a little dumb already so go easy on me...
using (SqlDataAdapter da = new SqlDataAdapter(sqlcmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
var dataCell = worksheet.Cells["A3"];
var dataRange = dataCell.LoadFromDataTable(dt, true);
foreach (var sheets in package.Workbook.Worksheets)
{
foreach (var pivot in sheets.PivotTables)
{
if (pivot.Name == worksheet.Name){
Console.WriteLine("{0,-14}", pivot.Name);
Console.WriteLine("{0,-14}", dataRange.ToString());
pivot.CacheDefinition.SourceRange = dataRange;
Console.ReadKey();
}
}
}
}
so I get the dataRange from the datatable. That seems to work fine. In this case, I have "A3:W21622" as the value going into the foreach loops. I have it so that I want to update pivot table ranges that use the data from the dt.
pivot.CacheDefinition.SourceRange = dataRange;
This is where I get the NULLReference. Sorry, I am lost here and its the last piece to the puzzle of an otherwise enjoyable experience.