Forum Community,
I'm using managed code and InfoPath's EventManager to call a method that's attached to one of two date/time picker controls (DTPC). The method attached to one of the DTPCs calls two methods that calculate the elapsed time in hours/days. Unfortunately, I have another form button that copies information between form controls such that pressing the button clears the contents of one of the DTPCs; changes to this DTPC automatically invokes my C# method (Calculate_Total_Hours/Calculate_Total_Days) which requires that both of the DTPCs on which it depends possess a valid date time. Unfortunately, after the button is pressed, one of the DTPCs is cleared which immediately causes a null exception to be thrown (as it should) as a result of invoking my C# methods. Both of my C# methods and the code attached to the offending DTPC are listed below:
public void CRD_ReturnDateTime_Changed(object sender, XmlEventArgs e)
{
DateTime DepartureTime = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:P2p_LI_CommittedDateTime", NamespaceManager).ValueAsDateTime;
DateTime ReturnTime = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:CrewRotationPreview/my:CRD_ReturnDateTime", NamespaceManager).ValueAsDateTime;
XPathNavigator reimbursable_hours = this.MainDataSource.CreateNavigator().SelectSingleNode("//my:myFields/my:CrewRotationPreview/my:P_ReimbursableHours", this.NamespaceManager);
XPathNavigator reimbursable_days = this.MainDataSource.CreateNavigator().SelectSingleNode("//my:myFields/my:CrewRotationPreview/my:P_ReimbursableDays", this.NamespaceManager);
reimbursable_hours.SetValue(Calculate_Total_Hours(DepartureTime, ReturnTime));
reimbursable_days.SetValue(Calculate_Total_Days(DepartureTime, ReturnTime));
}
private string Calculate_Total_Hours(DateTime DepartureTime, DateTime ReturnTime)
{
TimeSpan elapsedTime = ReturnTime.Subtract(DepartureTime);
int TotalElapsedTime;
TotalElapsedTime = (Convert.ToInt32(elapsedTime.TotalDays) * 16); // Represents multiples of 16-hour time blocks
TotalElapsedTime += Convert.ToInt32(elapsedTime.TotalHours) % 24; // Represents one-hour increments up to 15 hours
return TotalElapsedTime.ToString();
}
private string Calculate_Total_Days(DateTime DepartureTime, DateTime ReturnTime)
{
TimeSpan elapsedTime = ReturnTime.Subtract(DepartureTime);
int TotalElapsedTime;
TotalElapsedTime = Convert.ToInt32(elapsedTime.TotalDays);
return TotalElapsedTime.ToString();
}
Ideally, I would like to wrap some type of conditional logic around calls to "Calculate_Total_Hours/Calculate_Total_Days" inside the CRD_ReturnDateTime_Changed method by using the methods' "object sender, XmlEventArgs e" parameters. Is there a way I can use the stated parameters to only call the "Calculate_Total_Hours/Calculate_Total_Days" methods if a date/time was selected by a user using the DTPC control?
Thanks,
~W