Skip to main content
Post a question

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Specified cast is not valid

Like (0) ShareShare
ReportReport
Posted on 14 Jan 2023 22:02:43 by 1,487

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);
        }
    }

  • Martin Dráb Profile Picture
    231,760 Most Valuable Professional on 15 Jan 2023 at 06:16:00
    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.

  • .. Profile Picture
    1,487 on 15 Jan 2023 at 00:18:17
    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?

  • .. Profile Picture
    1,487 on 14 Jan 2023 at 22:23:56
    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

  • Martin Dráb Profile Picture
    231,760 Most Valuable Professional on 14 Jan 2023 at 22:12:06
    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.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,884 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,760 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans
Loading started