'New Record' in C# - InfoPath Dev

InfoPath Dev

Having trouble finding a blog or post that answers your question? Check out our Custom Search Page

'New Record' in C#

Last post 09-16-2015 12:43 PM by ES44AC SD70MAC. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-18-2011 11:32 AM

    'New Record' in C#


    I've been scouring the internet for the latter half of the day and trying various things but I can't seem to find the solution to my problem.  I've read many a post asking a similar question but unfortunately most went unanswered and those that were answered - didn't help.

    My question is basically:  What does the button action 'New Record' actually do in terms of coding (i.e. underlying xml document, etc)?  As a follow up to that question - how can you recreate the 'New Record' action programmatically in C#?

    The basis of my problem is that I have an InfoPath 2007/2010 form that submits to an SQL table.  After the form is submitted, it needs to be cleared, ready for more data entry and submission (rinse and repeat as many times as required).  I don't want the user to have to click a 'Submit' button and then a 'New Record' button - I figure that it should be doable in C#.  There are other reasons why I need to recreate the 'New Record' functionality too but "the why" is not important so I'll focus on the scenario I've just defined.

     Two of the main suggestions that I've come across are summarised below with the reason why they don't help:

    1. After the form has submitted, query the database with an incorrect ID (i.e. -1) so that no data is returned.  Although this clears the form, it also makes it read only so it's useless in the context of submitting the form again.
    2. Create a load event that records the initial OuterXml as a private string value and then simply replace the OuterXml with the private string value when you need to create a new record (i.e. after submission).  This seems to work but on inspection of the SQL table I realised that every time the OuterXml is replaced and a submission is made, the previous submission is deleted/overwritten.  So if you submit 10 forms without closing the form itself down, the only submission that would remain at the end would be the last.  It's more of a delete than an overwrite as the SQL key still increments by one each submission rather than just overwriting a particular row.

    I think that InfoPath points to a row in the database and even if you change various properties such as the OuterXml (or set all elements and attributes to zero, etc), it still remains pointing to the same row.  The only way it seems to move to a new row is if you add a 'New Record' button to the form and click that. 

    So in summary, I need to recreate what 'New Record' does without having to dedicate a button/mouse click to it.  Any ideas?

    Filed under: , , , ,
  • 01-21-2011 02:33 AM In reply to

    I've figured it out!  Yay, I'm happy now!  Thanks if anyone else attempted to solve the problem and I'll post my solution to hopefully help anyone else that may come across this in the future:

    Summary Steps (InfoPath 2007 C# model)

    1. Create a private string variable called 'initialInnerXml' and set it to ""
    2. Create a 'Form Loading' event and within this set 'initalInnerXml' equal to 'myFields' innerXml
    3. Create a private function/method called 'newRecord' that sets 'myFields' innerXml equal to 'initialInnerXml'
    4. Call this function whenever you need to mimic the 'New Record' button functionality.

    N.B.  I've found that if you have multiple views and use the InfoPath 'New Record' button, whenever you click it, the view resets to the default view.  Using this code solution, the view does not change.

    Here's a code example:

    public partial class FormCode

           //This string records the initial myFields InnerXml to revert to 
           private string initialInnerXml = "";

           //Call this whenever you want to mimic the 'New Record' InfoPath button 
           private void newRecord() 

                  //Simply sets the myFields InnerXml back to what it originally was 
                  MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields", NamespaceManager).InnerXml = initialInnerXml;


           // NOTE: The following procedure is required by Microsoft InfoPath. 
           // It can be modified using Microsoft InfoPath. public void InternalStartup() 

                  //Let InfoPath create these bits for you 
                  EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading); 
                  ((ButtonEvent)EventManager.ControlEvents["Example_Button"]).Clicked += new ClickedEventHandler(Example_Button_Clicked);


           //This is the Form Loading event. Let infopath create it for you 
           public void FormEvents_Loading(object sender, LoadingEventArgs e) 

                  //Simply records what myFields InnerXml is originally as the string initialInnerXml 
                  initialInnerXml = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields", NamespaceManager).InnerXml;


           //This is an example button on the form that creates a new Record 
           public void Example_Button_Clicked(object sender, ClickedEventArgs e) 

                  //Call the function/method 



  • 09-16-2015 12:43 PM In reply to

     well i took the time to create an account...  because i do want to let know that this is a great tip and the method presented does provide an answer to the matter presented...

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