only first setting of field works; others get "Operation is not valid, because of the current state of the object." - InfoPath Dev
in

InfoPath Dev

only first setting of field works; others get "Operation is not valid, because of the current state of the object."

Last post 07-10-2009 01:29 PM by gerry lowry. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 07-09-2009 12:02 PM

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

    only first setting of field works; others get "Operation is not valid, because of the current state of the object."

    Hello, I'm new to InfoPath 2007.

    My InfoPath 2007 form is used by an instructor to grade various student skills.

    When a lesson starts, the instructor clicks a start lesson control.

    I have two read only fields:  a time data type "lessonStart" and a text data type "lessonElaspedTime".

    There is a button control "startLesson".

    public void startLesson_Clicked(object sender, ClickedEventArgs e)
    {
     
    XPathNavigator myNavigator = this.MainDataSource.CreateNavigator();
     
    XPathNavigator myStartTime = this.MainDataSource.CreateNavigator()
                        .SelectSingleNode(
    "/my:lessonData/my:lessonFields/my:lessonStart", NamespaceManager);
      myStartTime.SetValue(
    DateTime.Now.TimeOfDay.ToString());
     
    XPathNavigator myLessonStatus = this.MainDataSource.CreateNavigator()
                         .SelectSingleNode(
    "/my:lessonData/my:lessonFields/my:lessonStatus", NamespaceManager);
      myLessonStatus.SetValue(
    "not completed");
      setElaspedTime();
     
    return;
    }

    When a lesson starts, I save the start time and I set the initial value of the elapsed time; this code works the first time it is run:

    public void setElaspedTime()
    {
      
    XPathNavigator myNavigator = this.MainDataSource.CreateNavigator();
      
    XPathNavigator myStartTime = this.MainDataSource.CreateNavigator()
                                           .SelectSingleNode(
    "/my:lessonData/my:lessonFields/my:lessonStart", NamespaceManager);
      
    XPathNavigator myElapsedTime = this.MainDataSource.CreateNavigator()
                                           .SelectSingleNode(
    "/my:lessonData/my:lessonFields/my:lessonElapsedTime", NamespaceManager);
       
    TimeSpan currentElapsedTime;
       
    DateTime myDateTimeStarted = DateTime.Parse(myStartTime.ToString());
       currentElapsedTime =
    DateTime.Now.TimeOfDay - myDateTimeStarted.TimeOfDay;
       
    string workString = currentElapsedTime.ToString();
       
    myElapsedTime.SetValue(workString.Substring(0,8));
       
    return;
    }

    When the end user uses a control, for example, a drop down list, I call setElapsdTime() to update the lessonElapsedTime field.

    setElapsdTime() always works the first time that I call it; thereafter, it crashes on this statement in setElapsedTime():

       myElapsedTime.SetValue(workString.Substring(0,8));

    with this error:  "Operation is not valid, because of the current state of the object."

    Here's the full stack trace:

    System.InvalidOperationException was unhandled by user code
      Message="Operation is not valid, because of the current state of the object."
      Source="Microsoft.Office.InfoPath.Client.Internal.Host.Interop"
      StackTrace:
           at Microsoft.MsoOffice.InfoPath.MsxmlInterop.MsxmlDocument.ThrowExceptionFromMsxmlInteropError(MsxmlInteropError eError)
           at Microsoft.MsoOffice.InfoPath.MsxmlInterop.MsxmlDocument.ThrowExceptionFromHresult(Int32 hrError)
           at Microsoft.MsoOffice.InfoPath.MsxmlInterop.MsxmlNodeImpl.set_Text(String strText)
           at Microsoft.Office.InfoPath.MsxmlNavigator.SetValue(String value)
           at chetStudentProgressReport.FormCode.setElaspedTime()
           at chetStudentProgressReport.FormCode.circleCheckMark_Changing(Object sender, XmlChangingEventArgs e)
           at Microsoft.Office.InfoPath.Internal.XmlEventHost.GenericProxy(Object genericDelegate, DataDOMEvent dataDOMEvent, InfoPathEvents type)
           at Microsoft.Office.InfoPath.Internal.XmlEventHost.ChangingProxy(DataDOMEvent dataDOMEvent)
           at Microsoft.Office.Interop.InfoPath.SemiTrust._DataDOMEventSink_SinkHelper.OnBeforeChange(DataDOMEvent pDataDOMEvent)

    What have I failed to do to cause this?

    Please advise.

    Thank you.

    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-10-2009 12:49 AM In reply to

    Re: only first setting of field works; others get "Operation is not valid, because of the current state of the object."

    Hi Gerry,

    My hunch is that you have some reentrancy in your event handler which is causing problems. Another idea is that you are trying to edit a value (myElapsedTime) in a Before event handler or perhaps there is some restriction on myElapsedTime... but, these are just guesses... I need more info. Can you please attach your XSN and your complete code? We will need to debug.

    You know this is a coding question. Not really an InfoPath question. We typically don't debug people's code since it doesn't benefit others in the forum and we don't make any money doing it. We're here to provide InfoPath tips and tricks and quick answers to questions FOR EVERYONE. We're not here to debug someone's specific code. We will take a quick look at your code and see what's going on, but if we can't figure it out in 5-10 minutes, we will kindly decline. You can hire one of our consultants to debug it, and we can most surely do that in an hour, but if you want something for free, ask a more general InfoPath question (after searching the site and not finding an answer).

    Cheers!

    Patrick Halstead
    Project Manager at Qdabra
  • 07-10-2009 01:29 PM In reply to

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

    Re: only first setting of field works; others get "Operation is not valid, because of the current state of the object."

    Hi Patrick,

    sorry, I contribute heavily by answering questions and asking questions at forums.asp.net, especially the ASP.NET MVC forum.

    I am used to posting code there and helping others with code that they've posted.

    Thank you so much for your reply.  You gave me the clue that I needed.

    Solution:  move the code from Changing to Changed:

       public void circleCheckMark_Changed(object sender, XmlEventArgs e)
       {
         setElapsedTime();
        
    return;
       }

    There's analogous behaviour in Access ... somethings one can do only in certain places.

    When I get past the "newbie" status in InfoPath 2007, I'll try to "pay it forward" by answering questions of others.

    Patrick, I appreciate your help.

    Thank you, again.

    Regards,
    Gerry (Lowry)

    P.S.:  for other "newbies":  I did not use the Validating Event because it gets fired before Form Loading, thus the uninitialized myStartTime variable in my example would cause the following exception:

                       System.FormatException was unhandled by user code
                         Message="String was not recognized as a valid DateTime."

    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]
Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.