Error while creating PIVOT TABLE (strName - string cannot be empty)

//Mycode

IWorksheet pivotSheet = workbook.Worksheets[0];
            //Create Pivot cache with the given data range
            IPivotCache cache = workbook.PivotCaches.Add(worksheet["A1:H50"]);
            //Create "PivotTable1" with the cache at the specified range
            IPivotTable pivotTable = pivotSheet.PivotTables.Add("PivotTable1", pivotSheet["A1"],cache);

            //Add Pivot table fields (Row and Column fields)
            pivotTable.Fields[2].Axis = PivotAxisTypes.Row;
            pivotTable.Fields[6].Axis = PivotAxisTypes.Row;
            pivotTable.Fields[3].Axis = PivotAxisTypes.Column;

            //Add data field
            IPivotField field = pivotTable.Fields[2];
            pivotTable.DataFields.Add(field, "Sum", PivotSubtotalTypes.Sum); 

//Output Error
Message: 
    System.ArgumentException : strName - string cannot be empty
  Stack Trace: 
    PivotCacheFieldsCollection.AddNewField(String strName)
    PivotCacheImpl.CreateField(IWorksheet sheet, Int32 row, Int32 lastRow, Int32 column)
    PivotCacheImpl.ctor(IApplication application, Object parent, IRange dataRange)
    PivotCacheCollection.Add(IRange range)


I have simply copied and pasted the code from documentation and it is showing me this error
Please tell me any solution 

3 Replies 1 reply marked as answer

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team July 6, 2020 09:45 AM UTC

Hi Firoz, 

Greetings from Syncfusion. 

We are unable to reproduce the reported issue. The sample which we have tried at our end can be downloaded from the following link. 


We were having an error in the code snippet in our documentation and we have changed this know.  

Old Code 
New Code 
//Add data field
IPivotField field = pivotTable.Fields[2];
pivotTable.DataFields.Add(field, "Sum", PivotSubtotalTypes.Sum); 
//Add data field
IPivotField field = pivotTable.Fields[5];
pivotTable.DataFields.Add(field, "Sum", PivotSubtotalTypes.Sum);

Note: But this is to confirm you that, even before the change, we were unable to reproduce the reported exception at our end. 

Kindly try this and let us know if it helps. Else, please share us the issue reproducing sample and confirm us the Syncfusion XlsIO version you are using at your end, which will be helpful for us in investigating the issue. 

Regards, 
Keerthi. 



FR Firoz Rangrez July 6, 2020 03:24 PM UTC

Thanks for Replying ,

Actually i have solved that issue 
I have tried to creating a new pivot table in a blank table so that's why the strName is always remained empty while execution.

Marked as answer

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team July 7, 2020 05:51 AM UTC

Hi Firoz, 

Thanks for the update. We are glad that the issue is resolved at your end.  

Kindly let us know if your need any further assistance. 

Regards, 
Keerthi.

Loader.
Up arrow icon