web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Specified cast is not valid

(0) ShareShare
ReportReport
Posted on by 1,965

Hi,

I'm getting this error when trying package.save()

When should I try save or saveAs(this.FileStream)

It doesn't work either way 

protected void preProcessCatalogMaintenanceRequestXML()
    {
        System.Exception ex;
        try
        {
            CatVendorDateTime uploadDateTime = CatVendorCatalogMaintenanceRequest::findRecId(catVendorCatalogMaintenanceRecId).UploadDateTime;

            using(ExcelPackage package = new ExcelPackage(this.fileStream))
            {

                int rowCount,columnCount, rowIterator, columnIterator;

                package.Load(this.fileStream);

                ExcelWorksheet worksheet = package.get_workbook().get_worksheets().get_Item(1);

                OfficeOpenXml.ExcelRange range = worksheet.Cells;

                rowCount = worksheet.Dimension.End.Row - worksheet.Dimension.Start.Row   1;
                columnCount = worksheet.Dimension.End.Column - worksheet.Dimension.Start.Column   1;

           

                    package.Save();
                    fileId = this.uploadFileToTempStorage(this.fileStream, fullFileName);
                }

            }
        }
        catch(ex)
        {
            info(ex.Message);
        }
    }

I have the same question (0)
  • Martin Dráb Profile Picture
    237,681 Most Valuable Professional on at
    RE: Specified cast is not valid

    What object do you have in fileStream variable?

    Is it the full message? Doesn't it mention it the actual types? Isn't it actually related to your utcDateTime field? Won't the problem disappear if you remove the assignment at line 31? If so, you're now discussion the same thing in four threads under two user accounts...

    Also, the whole idea to using SaveAs() to save to the original package looks suspicious at least. If you want to use SaveAs(), let me remind you once more (I already did it in another thread) that I already showed you sample code. Please look at it, try it and let us know if it works for you. It saves the result to a MemoryStream object.

    By the way, you claimed in another thread that the value should be unique, but you're trying to set the same value to all rows.

  • .. Profile Picture
    1,965 on at
    RE: Specified cast is not valid

    Hi Martin,

    This.FileStream is of type System.IO.Stream

    And when I debug it, it's system.IO.Memory

    And the capacity, Length and Position are filled (not zero)

    I tried to replace package.Save()

    With those lines:

    Using(System.IO.MemoryStream output stream = new System.IO.MemoryStream())

    {

    Package.SaveAs(outputStream);

    FileId=this.uploadFileToTempStorage(this.filestream, fullFileName);

    }

    But same error...may I know why I should use saveAs and use new stream?

    Note I only have one row after the header in my example

  • .. Profile Picture
    1,965 on at
    RE: Specified cast is not valid

    Hi Martin,

    I replaced line 31 with setValue(datetime2str(uploadDateTime)) and it stopped erroring at save package. It seems it needs to be string.

    However the import is failing on the execution details of the entity. Saying "input column specified was not found in the entity mapping(input column UPLOADATETIME was not found in the input column collection)".. Does that mean the code to save to the excel file is not working? What's wrong with it?

  • Martin Dráb Profile Picture
    237,681 Most Valuable Professional on at
    RE: Specified cast is not valid

    Good, it's great th problem is resolved. It's just a pity that you didn't do it already when I suggested it (in another thread).

    Your problem about entity mapping doesn't belong to this thread (which is about "Specified cast is not valid"). Create a new thread with an appropriate title title and explain your problem there. Tell us how you created or updated the entity (something wasn't done correctly there) and if you're able to import the same file through data management.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 683 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 563 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 398 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans