Problem with SaveAs(string URI) method - InfoPath Dev
in

InfoPath Dev

Problem with SaveAs(string URI) method

Last post 07-12-2010 01:49 PM by BartH. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 07-19-2009 04:37 AM

    Problem with SaveAs(string URI) method

    Hi,

     

    I am sitting now for some time trying to discover why am I getting an exception when performing custom Save operation. I think I set everything right:

    Form is fully trusted and signed.

    I set save with custom code.

    Method that is triggered on saveEvent is:

    public void FormEvents_Save(object sender, SaveEventArgs e)  {

    if (this.New)
    {
        this.SaveAs("c:\\form1.xml");
    }
    else
    {
    this.Save();
    }
     e.PerformSaveOperation();

     e.CancelableArgs.Cancel = false;

    }

     

    The problem is that I keep getting following Exception every time I try to save new form:

    InfoPath cannot save the form with the following name: c:\test.xml
    Invalid context for OM call.
       bei Microsoft.Office.Interop.InfoPath.SemiTrust._XDocument.SaveAs(String bstrFileUrl)
       bei Microsoft.Office.Interop.InfoPath.SemiTrust.XDocumentWrapper.SaveAs(String bstrFileUrl)
       bei Microsoft.Office.InfoPath.Internal.XmlFormHost.SaveAs(String fileUrl)
       bei Microsoft.Office.InfoPath.XmlFormHostItem.SaveAs(String fileUrl)
       bei questionnaire1.FormCode.FormEvents_Save(Object sender, SaveEventArgs e) in C:\Users\Edin Zvizdic\Documents\InfoPath Projects\q1_deploy3\FormCode.cs:Zeile 62.
       bei Microsoft.Office.InfoPath.Internal.FormEventsHost.OnSaveRequest(SaveEvent pEvent)
       bei Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnSaveRequest(SaveEvent pEvent)

    I would really appriciate some help. Thanks!

     

    regards, easy

  • 07-20-2009 08:23 AM In reply to

    • gerry lowry
    • Not Ranked
      Male
    • Joined on 07-09-2009
    • alliston ontario canada
    • Posts 21

    Re: Problem with SaveAs(string URI) method

    I am fighting similar problems myself.

    Grab this chapter:  http://ptgmedia.pearsoncmg.com/images/9780321410597/samplechapter/roberts_ch15.pdf

    The code below is from the last few pages of the above .pdf.

    The code probably will not give you 100% of your solution but it may get you closer.

    Are you using InfoPath 2007?   2007 has differences form 2003.

    Regards,
    Gerry (Lowry)

    public void FormEvents_Submit(object sender, SubmitEventArgs e)
    {
    // NOTE: There are no errors if InfoPath allows submit to happen.
    // If this is recovered data, ask the user if we should continue.
    DialogResult result = DialogResult.Yes;
    if (this.Recovered)
    result = MessageBox.Show("This data is recovered. "
    + "Still continue?", "Submitting Recovered Data",
    MessageBoxButtons.YesNo);
    if (result == DialogResult.Yes)
    {
    // Save the form
    string tempFileName = System.IO.Path.GetTempFileName()
    + ".xml";
    this.SaveAs(tempFileName);
    // Fills a link to see where it saved
    XPathNavigator root = MainDataSource.CreateNavigator();
    XPathNavigator savedFileLocation = root.SelectSingleNode(
    "/my:Request/@my:SavedFileLocation", NamespaceManager);
    savedFileLocation.SetValue(tempFileName);
    // Go to the Thank You view
    ViewInfos.SwitchView("ThankYou");
    }
    else
    {
    e.CancelableArgs.Cancel = true;
    e.CancelableArgs.Message = "Submit was canceled.";
    e.CancelableArgs.MessageDetails =
    "Submit again whenever you are ready.";
    }
    }

    Programming InfoPath . . . in Action! 831

    Gerry Lowry, Principal
    Ability Business Computer Services ~~ Because it's your Business, our Experience Counts!
    68 John W. Taylor Avenue
    Alliston · Ontario · Canada · L9R 0E1 · [email protected]
  • 07-20-2009 05:30 PM In reply to

    • gerry lowry
    • Not Ranked
      Male
    • Joined on 07-09-2009
    • alliston ontario canada
    • Posts 21

    Re: Problem with SaveAs(string URI) method

    good news / bad news .............  using code based on the .pdf in my previous reply, I could    SaveAs.

    PROBLEM:  it's a     temporary   file in my temp directory.

    I tried to rename the temporary   file    with   very unhappy results:

    // Save the form
    string tempFileName = System.IO.Path.GetTempFileName() + ".xml";
    this.SaveAs(tempFileName); // ~~ do not know why ... we need temp file name first ?????

    //============= can not rename ================
    File.Move(tempFileName, newFileSaveAsFilename); // ~~~~~~~~~~~~~~~~~~~~~~~rename the file to reflect the student and the course.

    // System.IO.IOException was unhandled by user code
    //  Message="The process cannot access the file because it is being used by another process."
    //  Source="mscorlib"
    //  StackTrace:
    //       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    //       at System.IO.__Error.WinIOError()
    //       at System.IO.File.Move(String sourceFileName, String destFileName)
    //       at StudentProgressReport.FormCode.lessonEnd_Clicked(Object sender, ClickedEventArgs e) in C:\Drivers\driverManagement\infoPathManagedCode\StudentProgressReport2\FormCode.cs:line 735
    //       at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
    //       at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

    Please note:  in my case, I have removed the ability for the form user to manually Save/SaveAs ...

    this is because I need full control over the final file names and can not allow end users to make  filename typos.

     

    ???? is it just me, or is the InfoPath 2007 documentation substantially incomplete with regards to crafting managed code?

    I wish Ken Getz et al would do for InfoPath 2007 what they did with there fine series of books for MS Access.

     

    Regards,
    Gerry (Lowry)

    Gerry Lowry, Principal
    Ability Business Computer Services ~~ Because it's your Business, our Experience Counts!
    68 John W. Taylor Avenue
    Alliston · Ontario · Canada · L9R 0E1 · [email protected]
  • 07-12-2010 01:49 PM In reply to

    • BartH
    • Not Ranked
    • Joined on 07-12-2010
    • Posts 1

    Re: Problem with SaveAs(string URI) method

    Hi Gerry (and Edin),

    Probably a bit late, but I hope you read this - I had the same problem and found the solution in saving the file not to C:\\, but to a sub directory.

    Where "C:\\demo.xml" didn't work, "C:\\demo\\demo.xml" (for C# demo\\demo.xml - two back slashes) works fine!

    Good luck, BartH

Page 1 of 1 (4 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.