Good Morning,
I am creating an InfoPath form for my supervisors to submit their direct reports' production and hours on a weekly basis. It seems so simple but I have tried now for 2 weeks to find a codeless solution and cannot seem to find any detailed instructions on how to make my form work.
I have approximately 30 work units/departments. I want each supervisor to be able to pull up the form, select their department code from a drop-down list, click a button ("Populate Unit"), and based on that selection in the drop-down field (my:selectedUnit) each employee name within that department(along with other information) will appear in data fields within a repeating table. There will be other data fields next to their names so the supervisor can input the weekly production number and work hours for each employee. Once they fill it out they will submit the form to the main data source, an access table, "Weekly_Hours". The drop-down list items and employee information are retrieved from a secondary data source: an access table, "Workforce Details".
After trying for days I found some code that Hillary Stoupa posted a few years ago titled "How Do I Copy Repeating Data From an External Source into My Form?" and gave it a try but it didn't work so I exported my secondary source from a table to an XML ("Workforce") so it could mirror Hillary's example. SO CLOSE but there was one error that I am praying you can help me fix. I receive a *NullReferenceException error was unhandled by user code* error. Object reference not set to an instance of an object.
Tips given:
Use the "new" keyword to create an object instance (you may have tried to use an object without using the New keyword first, or tried to use an object whose value is set to null).
Check to determine if the object is null before calling the method.
I am using Infopath 2010. Should my code change somewhat since she used a different year? I tried to provide as much information below as I could :-? Thank you for any help you can provide. This project deadline is quickly approaching.
Schema
MAIN
myFields
queryFields
dataFields
d:Weekly_Hours
my:selectedUnit
XML clip
<?xml version="1.0" encoding="UTF-8"
?>
- <Workforce_x0020_Details>
<L_x0020_Name>Stackhouse</L_x0020_Name>
<F_x0020_Name>Sookie</F_x0020_Name>
<Supervisor>A SMITH</Supervisor>
</Workforce_x0020_Details>
UNDERLINED CODE IS WHERE NULL REFERENCE EXCEPTION ERROR IS POINTING TO
using Microsoft.Office.InfoPath;
using System;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using mshtml;
namespace test
{
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 InfoPath.
// It can be modified using Microsoft InfoPath.
public void InternalStartup()
{
((ButtonEvent)EventManager.ControlEvents["cmdPopulateUnit"]).Clicked += new ClickedEventHandler(cmdPopulateUnit_Clicked);
}
public void cmdPopulateUnit_Clicked(object sender, ClickedEventArgs e)
{
// Get the selected unit.
string selectedUnit = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:selectedUnit", NamespaceManager).Value;
// If no Unit was selected notify the user.
if (selectedUnit.Equals(""))
{
MessageBox.Show("Please select a Unit first.");
return;
}
// Get the list of workers per the selected unit.
XPathNodeIterator Workforce = this.DataSources["Workforce"].CreateNavigator().Select("/Workforce/Workforce_x0020_Details[Unit = '" + selectedUnit + "']", NamespaceManager);
// If nothing was found, then return; there will be no items to insert.
if (Workforce.Count == 0)
{
return;
}
//Get the worker insertion point.
XPathNavigator workforceList = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Weekly_Hours", NamespaceManager);
//Add the rows to the main DOM.
foreach (XPathNavigator Workforce_x0020_Details in Workforce)
{
try
{
// Get this unit's values from the secondary DOM.
string lastname = Workforce_x0020_Details.SelectSingleNode("L_x0020_Name", NamespaceManager).Value;
string firstname = Workforce_x0020_Details.SelectSingleNode("F_x0020_Name", NamespaceManager).Value;
string position = Workforce_x0020_Details.SelectSingleNode("Position", NamespaceManager).Value;
string status = Workforce_x0020_Details.SelectSingleNode("Status", NamespaceManager).Value;
// Build a string representing a main DOM vehicle nodeset using the values from the secondary DOM.
string workforceXml = "<d:Weekly_Hours xmlns:my='http://schemas.microsoft.com/office/infopath/2003/ado/dataFields'>"
+ "<my:Last_Name>" + lastname + "</@Last_Name>"
+ "<my:First_Name>" + firstname + "</@First_Name>"
+ "<my:Position>" + position + "</@Position>"
+ "<my:Status>" + status + "</@Status>";
//Load new workforce string into a temporary XML document
XmlDocument tempDoc = new XmlDocument();
tempDoc.LoadXml(workforceXml);
// Insert the new record into the main DOM.
XPathNavigator newWorker = tempDoc.CreateNavigator().SelectSingleNode("d:Weekly_Hours", NamespaceManager);
workforceList.AppendChild(newWorker);
}
catch (Exception ex)
{
MessageBox.Show("Could not insert other individual.\n\nError message:\n" + ex.Message);
}
}
}
}
}