locked
The name 'e' does not exist in the current context RRS feed

  • Question

  • I am currently trained as a Power User for InfoPath 2010 and SharePoint 2010 but am no way a developer, so please forgive me if I am missing something simple here.

    My current employer is on SharePoint 2007 and InfoPath 2007.  I am trying to create an InfoPath form to submit to a SharePoint list, something that was quite easy in the 2010 versions.  I have been following the Submitting Data from InfoPath 2007 to a SharePoint List instructions provided on this website.  Sorry I cannot post a link until my account can be verified...

    I am receiving the following error on four different lines in the C# code: The name 'e' does not exist in the current context. The Error List specifies there errors at: Line 44 Column 21; Line 67 Column 21; Line 71 Column 25; Line 73 Column 25.

    Here is my code:

    using Microsoft.Office.InfoPath;
    using System;
    using System.Windows.Forms;
    using System.Xml;
    using System.Xml.XPath;
    using mshtml;
    
    namespace MyContacts
    {
        public partial class FormCode
        {
            // Member variables are not supported in browser-enabled forms.
            // Instead, write and read these values from the FormState
            // dictionary using code such as the following:
            //
            // private object _memberVariable
            // {
            //     get
            //     {
            //         return FormState["_memberVariable"];
            //     }
            //     set
            //     {
            //         FormState["_memberVariable"] = value;
            //     }
            // }
    
            // NOTE: The following procedure is required by Microsoft Office InfoPath.
            // It can be modified using Microsoft Office InfoPath.
            public void InternalStartup()
            {
                EventManager.XmlEvents["/my:myFields/my:AddContacts"].Changed += new XmlChangedEventHandler(AddContacts_Changed);
                try
                {
                    // Get a reference to the form's XmlNamespaceManager object.
                    XmlNamespaceManager ns = this.NamespaceManager;
    
                    // Create an XPathNavigator object for the form's main data
                    // source.
                    XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
    
                    // If the check box is set to true, add a new row so the user 
                    // can add a new contact.
                    if (e.Site.Value == "true")
                    {
                        XPathNavigator xnTable =
                           xnDoc.SelectSingleNode("/my:myFields/my:gpContacts", ns);
                        xnTable.AppendChild("<my:gpContact><my:Title /><my:FirstName /><my:LastName /><my:Email /><my:Phone /></my:gpContact>");
                    }
                    else
                    {
                        // If the user clears the check box, remove the added row.
                        ClearEnteredValues(xnDoc);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("The following error occurred: " + ex.Message);
                    throw;
                }
                try
                {
                    // Get a reference to the form's XmlNamespaceManager object.
                    XmlNamespaceManager ns = this.NamespaceManager;
    
                    // See if values have been changed.
                    if (e.Operation == XmlOperation.ValueChange)
                    {
                        // Set the Changed field to "true" only if the Delete
                        // check box is not selected.
                        if (e.Site.SelectSingleNode("@Delete", ns).Value != "true")
                        {
                            e.Site.SelectSingleNode("@Changed", ns).SetValue("true");
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("The following error occurred: " + ex.Message);
                    throw;
                }
                EventManager.XmlEvents["/dfs:myFields/dfs:dataFields/dfs:MyContacts", "MyContacts"].Changed += new XmlChangedEventHandler(MyContacts__MyContacts_Changed);
                ((ButtonEvent)EventManager.ControlEvents["btnSendChanges"]).Clicked += new ClickedEventHandler(btnSendChanges_Clicked);
                try
                {
                    // Get a reference to the form's XmlNamespaceManager object.
                    XmlNamespaceManager ns = this.NamespaceManager;
    
                    // Create a DataSource object for the MyContacts 
                    // list data connection.
                    DataSource dsContacts = this.DataSources["MyContacts"];
    
                    // Create an XPathNavigator object for the MyContacts 
                    // data connection.
                    XPathNavigator xnContacts = dsContacts.CreateNavigator();
    
                    // Create an XPathNodeIterator object for the MyContacts 
                    // data connection to enumerate the existing contacts.
                    XPathNodeIterator xiContacts = xnContacts.Select(
                       "/dfs:myFields/dfs:dataFields/dfs:MyContacts", ns);
    
                    // Create a DataSource object for the Add List Item Template
                    // data connection.
                    DataSource dsCAML = this.DataSources["Add List Item Template"];
    
                    // Create an XPathNavigator object for the 
                    // Add List Item Template data connection.
                    // This is used to set the values in the CAML XML file.
                    XPathNavigator xnCAML = dsCAML.CreateNavigator();
    
                    // Create a WebServiceConnection object for submitting 
                    // to the Lists Web service data connection.
                    WebServiceConnection wsSubmit =
                       (WebServiceConnection)this.DataConnections[
                       "Web Service Submit"];
    
                    // Create an XPathNavigator object for the form's 
                    // main data source.
                    XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
    
                    // Create an XPathNodeIterator object for the new contacts.
                    XPathNodeIterator xiNewContacts = xnDoc.Select(
                       "/my:myFields/my:gpContacts/my:gpContact", ns);
    
                    // See if any new contacts have been added.
                    if (xiNewContacts.Count > 0)
                    {
                        while (xiNewContacts.MoveNext())
                        {
                            // Set the values in the Add List Item Template 
                            // XML file using the values in the new row.
                            xnCAML.SelectSingleNode(
                               "/Batch/Method/Field[@Name='Title']", ns)
                               .SetValue(xiNewContacts.Current.SelectSingleNode(
                               "my:Title", ns).Value);
                            xnCAML.SelectSingleNode(
                               "/Batch/Method/Field[@Name='FirstName']", ns)
                               .SetValue(xiNewContacts.Current.SelectSingleNode(
                               "my:FirstName", ns).Value);
                            xnCAML.SelectSingleNode(
                               "/Batch/Method/Field[@Name='LastName']", ns)
                               .SetValue(xiNewContacts.Current.SelectSingleNode(
                               "my:LastName", ns).Value);
                            xnCAML.SelectSingleNode(
                               "/Batch/Method/Field[@Name='Phone']", ns)
                               .SetValue(xiNewContacts.Current.SelectSingleNode(
                               "my:Phone", ns).Value);
                            xnCAML.SelectSingleNode(
                               "/Batch/Method/Field[@Name='Email']", ns)
                               .SetValue(xiNewContacts.Current.SelectSingleNode(
                               "my:Email", ns).Value);
    
                            // Set the values of the Changed and Delete columns to
                            // "FALSE".
                            xnCAML.SelectSingleNode(
                               "/Batch/Method/Field[@Name='Changed']", ns)
                               .SetValue("FALSE");
                            xnCAML.SelectSingleNode(
                               "/Batch/Method/Field[@Name='Delete']", ns)
                               .SetValue("FALSE");
    
                            // Set the value of Cmd attribute to "New".
                            xnCAML.SelectSingleNode("/Batch/Method/@Cmd", ns)
                               .SetValue("New");
    
                            // Submit the new row.
                            wsSubmit.Execute();
                        }
                    }
                    else
                    {
                        // Enumerate the existing contacts and see if 
                        // any items have been changed or set to be deleted.
                        while (xiContacts.MoveNext())
                        {
                            if (xiContacts.Current.SelectSingleNode(
                               "@Changed", ns).Value == "true")
                            {
                                // Set the values in the Add List Item Template XML file
                                // to the values in the updated row.
                                xnCAML.SelectSingleNode(
                                   "/Batch/Method/Field[@Name='Title']", ns)
                                   .SetValue(xiContacts.Current.SelectSingleNode(
                                   "@Title").Value);
                                xnCAML.SelectSingleNode(
                                   "/Batch/Method/Field[@Name='FirstName']", ns)
                                   .SetValue(xiContacts.Current.SelectSingleNode(
                                   "@FirstName").Value);
                                xnCAML.SelectSingleNode(
                                   "/Batch/Method/Field[@Name='LastName']", ns)
                                   .SetValue(xiContacts.Current.SelectSingleNode(
                                   "@LastName").Value);
                                xnCAML.SelectSingleNode(
                                   "/Batch/Method/Field[@Name='Phone']", ns)
                                   .SetValue(xiContacts.Current.SelectSingleNode(
                                   "@Phone").Value);
                                xnCAML.SelectSingleNode(
                                   "/Batch/Method/Field[@Name='Email']", ns)
                                   .SetValue(xiContacts.Current.SelectSingleNode(
                                   "@Email").Value);
                                xnCAML.SelectSingleNode(
                                   "/Batch/Method/Field[@Name='Changed']", ns)
                                   .SetValue("false");
                                xnCAML.SelectSingleNode(
                                   "/Batch/Method/Field[@Name='ID']", ns)
                                   .SetValue(xiContacts.Current.SelectSingleNode(
                                   "@ID").Value);
    
                                // Set the value of the Cmd attribute to "Update".
                                xnCAML.SelectSingleNode(
                                   "/Batch/Method/@Cmd", ns)
                                   .SetValue("Update");
    
                                // Submit the updates for the current row.
                                wsSubmit.Execute();
                            }
                            // If the Delete field is set to "true" for the current
                            // row, in the Batch fragment set the ID for the row
                            // and set the Cmd attribute to "Delete".
                            if (xiContacts.Current.SelectSingleNode(
                               "@Delete", ns).Value == "true")
                            {
                                xnCAML.SelectSingleNode(
                                   "/Batch/Method/Field[@Name='ID']", ns)
                                   .SetValue(xiContacts.Current.SelectSingleNode(
                                   "@ID").Value);
                                xnCAML.SelectSingleNode(
                                   "/Batch/Method/@Cmd", ns)
                                   .SetValue("Delete");
    
                                // Submit the delete operation for the current row.
                                wsSubmit.Execute();
                            }
                        }
                    }
    
                    // Create a DataConnection object for the MyContacts 
                    // data connection.
                    DataConnection dcContacts = this.DataConnections["MyContacts"];
    
                    // Execute the data connection to refresh the list of 
                    // contacts to reflect any additions, updates, or deletions.
                    dcContacts.Execute();
    
                    // Clear the values in the repeating table for 
                    // adding new contracts.
                    ClearEnteredValues(xnDoc);
                }
    
                catch (Exception ex)
                {
                    MessageBox.Show("The following error occurred: " + ex.Message);
                    throw;
                }
            }
    
            public void AddContacts_Changed(object sender, XmlEventArgs e)
            {
                // Write your code here to change the main data source.
                
            }
            private void ClearEnteredValues(XPathNavigator xnDoc)
            {
                try
                {
                    // Get a reference to the form's XmlNamespaceManager object.
                    XmlNamespaceManager ns = this.NamespaceManager;
    
                    // Create an XPathNodeIterator object to get a count of the 
                    // rows in the repeating table used to add new Contacts.
                    XPathNodeIterator xi = xnDoc.Select(
                       "/my:myFields/my:gpContacts/my:gpContact", ns);
                    int rowCount = xi.Count;
    
                    if (rowCount > 0)
                    {
                        // Get the first and last rows (nodes) in the 
                        // repeating table.
                        XPathNavigator firstNode = xnDoc.SelectSingleNode(
                           "/my:myFields/my:gpContacts/my:gpContact[1]", ns);
                        XPathNavigator lastNode = xnDoc.SelectSingleNode(
                           "/my:myFields/my:gpContacts/my:gpContact[" +
                           rowCount + "]", ns);
    
                        // Delete the existing nodes using the DeleteRange method.
                        firstNode.DeleteRange(lastNode);
                    }
    
                    // Clear the check box. 
                    xnDoc.SelectSingleNode(
                       "/my:myFields/my:AddContacts", ns).SetValue("false");
                }
    
                catch (Exception ex)
                {
                    MessageBox.Show("The following error occurred: " +
                       ex.Message);
                    throw;
                }
            }
    
            public void MyContacts__MyContacts_Changed(object sender, XmlEventArgs e)
            {
                // Write your code here to change the main data source.
            }
    
            public void btnSendChanges_Clicked(object sender, ClickedEventArgs e)
            {
                // Write your code here.
            }
        }
    }
    

    I appreciate any guidance you can give.  Thank you.

    • Moved by Amanda Zhu Tuesday, August 13, 2013 8:54 AM
    Monday, August 12, 2013 4:30 PM

Answers

  • Hi,

    the problem should start at the following snippet

     // If the check box is set to true, add a new row so the user 
    // can add a new contact.
      if (e.Site.Value == "true")
      {
        XPathNavigator xnTable =
    xnDoc.SelectSingleNode("/my:myFields/my:gpContacts", ns);
        xnTable.AppendChild("<my:gpContact><my:Title /><my:FirstName /><my:LastName /><my:Email /><my:Phone /></my:gpContact>");
      }
    

    Here you refer to a variable 'e' which is not declared before. It seems like you want to use a variable like in the event methods, but this one doesn't exist.

    Best Regards. When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer. This helps us build a healthy and positive community.

    @Horizon_Net | Blog

    • Proposed as answer by Sean Kornish Tuesday, August 13, 2013 9:03 AM
    • Marked as answer by Just Karl Thursday, August 22, 2013 6:57 PM
    Tuesday, August 13, 2013 12:30 AM

All replies

  • Hi,

    the problem should start at the following snippet

     // If the check box is set to true, add a new row so the user 
    // can add a new contact.
      if (e.Site.Value == "true")
      {
        XPathNavigator xnTable =
    xnDoc.SelectSingleNode("/my:myFields/my:gpContacts", ns);
        xnTable.AppendChild("<my:gpContact><my:Title /><my:FirstName /><my:LastName /><my:Email /><my:Phone /></my:gpContact>");
      }
    

    Here you refer to a variable 'e' which is not declared before. It seems like you want to use a variable like in the event methods, but this one doesn't exist.

    Best Regards. When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer. This helps us build a healthy and positive community.

    @Horizon_Net | Blog

    • Proposed as answer by Sean Kornish Tuesday, August 13, 2013 9:03 AM
    • Marked as answer by Just Karl Thursday, August 22, 2013 6:57 PM
    Tuesday, August 13, 2013 12:30 AM
  • Hello,

    Thank you for your post.

    I am afraid that the issue is out of support range of VS General Question forum which mainly discusses WPF & SL designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System and Visual Studio Editor.

    I am moving your question to the moderator forum ("Where is the forum for..?"). The owner of the forum will direct you to a right forum.

    Best regards,


    Amanda Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 13, 2013 8:54 AM