Customization to Toggle CallContextBpmData Cast Error

Hello,
I’ve added a button to the RFQ supplier screen and I’m trying to use it to toggle CallContextBpmData [“Checkbox01”], the 2 examples of code below compile fine but I’m getting the error “Specified cast is not valid” when I click the button, (I’ve restarted and cleared client cache)

Any help would be much appreciated

Example 1:

// **************************************************
// Custom code for RFQForm
// Created: 7/09/2020 11:22:30 PM
// **************************************************

extern alias Erp_Contracts_BO_Part;
extern alias Erp_Contracts_BO_PartRevSearch;
extern alias Erp_Contracts_BO_Company;

using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Erp.Adapters;
using Erp.UI;
using Ice.Lib;
using Ice.Adapters;
using Ice.Lib.Customization;
using Ice.Lib.ExtendedProps;
using Ice.Lib.Framework;
using Ice.Lib.Searches;
using Ice.UI.FormFunctions;
using Ice.BO;

public class Script
{
	// ** Wizard Insert Location - Do Not Remove 'Begin/End Wizard Added Module Level Variables' Comments! **
	// Begin Wizard Added Module Level Variables **

	// End Wizard Added Module Level Variables **

	// Add Custom Module Level Variables Here **

	public void InitializeCustomCode()
	{
		// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Variable Initialization' lines **
		// Begin Wizard Added Variable Initialization

		// End Wizard Added Variable Initialization

		// Begin Wizard Added Custom Method Calls

		this.epiButtonC1_Add.Click += new System.EventHandler(this.epiButtonC1_Add_Click);
		// End Wizard Added Custom Method Calls
	}

	public void DestroyCustomCode()
	{
		// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
		// Begin Wizard Added Object Disposal

		this.epiButtonC1_Add.Click -= new System.EventHandler(this.epiButtonC1_Add_Click);
		// End Wizard Added Object Disposal

		// Begin Custom Code Disposal

		// End Custom Code Disposal
	}

	private void epiButtonC1_Add_Click(object sender, System.EventArgs args)
	{
		// ** Place Event Handling Code Here **

	EpiDataView edvCallContextBpmData = ((EpiDataView)(this.oTrans.EpiDataViews["CallContextBpmData"]));
			System.Data.DataRow edvCallContextBpmDataRow = edvCallContextBpmData.CurrentDataRow;
			if ((edvCallContextBpmDataRow != null))
			{
				
				edvCallContextBpmDataRow.BeginEdit();

				if ((bool)edvCallContextBpmDataRow["Checkbox01"] == true)
				{
					edvCallContextBpmDataRow["Checkbox01"] = false;
				}
				else
				{
					edvCallContextBpmDataRow["Checkbox01"] = true;
				}
			
				//edvCallContextBpmDataRow.RowMod = "U";
				//edvCallContextBpmDataRow.Update();
					
				//edvCallContextBpmDataRow["Checkbox01"] = !edvCallContextBpmDataRow["Checkbox01"];
				edvCallContextBpmDataRow.EndEdit();
				oTrans.Update();

			}

	}
}

Example 2:

// **************************************************
// Custom code for RFQForm
// Created: 7/09/2020 11:22:30 PM
// **************************************************

extern alias Erp_Contracts_BO_Part;
extern alias Erp_Contracts_BO_PartRevSearch;
extern alias Erp_Contracts_BO_Company;

using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Erp.Adapters;
using Erp.UI;
using Ice.Lib;
using Ice.Adapters;
using Ice.Lib.Customization;
using Ice.Lib.ExtendedProps;
using Ice.Lib.Framework;
using Ice.Lib.Searches;
using Ice.UI.FormFunctions;
using Ice.BO;

public class Script
{
	// ** Wizard Insert Location - Do Not Remove 'Begin/End Wizard Added Module Level Variables' Comments! **
	// Begin Wizard Added Module Level Variables **

	// End Wizard Added Module Level Variables **

	// Add Custom Module Level Variables Here **

	public void InitializeCustomCode()
	{
		// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Variable Initialization' lines **
		// Begin Wizard Added Variable Initialization

		// End Wizard Added Variable Initialization

		// Begin Wizard Added Custom Method Calls

		this.epiButtonC1_Add.Click += new System.EventHandler(this.epiButtonC1_Add_Click);
		// End Wizard Added Custom Method Calls
	}

	public void DestroyCustomCode()
	{
		// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
		// Begin Wizard Added Object Disposal

		this.epiButtonC1_Add.Click -= new System.EventHandler(this.epiButtonC1_Add_Click);
		// End Wizard Added Object Disposal

		// Begin Custom Code Disposal

		// End Custom Code Disposal
	}

	private void epiButtonC1_Add_Click(object sender, System.EventArgs args)
	{
		// ** Place Event Handling Code Here **

	EpiDataView edvCallContextBpmData = ((EpiDataView)(this.oTrans.EpiDataViews["CallContextBpmData"]));
			System.Data.DataRow edvCallContextBpmDataRow = edvCallContextBpmData.CurrentDataRow;
			if ((edvCallContextBpmDataRow != null))
			{
				
					
				edvCallContextBpmDataRow.BeginEdit();
				edvCallContextBpmDataRow["Checkbox01"] = !(bool)edvCallContextBpmDataRow["Checkbox01"];
				edvCallContextBpmDataRow.EndEdit();
				oTrans.Update();

			}

	}
}

The full error message:

Application Error

Exception caught in: App.RFQEntry.RFQForm.EP.Customization.RFQEntry_TWG.CustomCode.0

Error Detail

Message: Specified cast is not valid.
Program: App.RFQEntry.RFQForm.EP.Customization.RFQEntry_TWG.CustomCode.0.dll
Method: epiButtonC1_Add_Click

Client Stack Trace

at Script.epiButtonC1_Add_Click(Object sender, EventArgs args)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButtonBase.OnClick(EventArgs e)
at Ice.Lib.Framework.EpiButton.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButton.OnMouseUp(MouseEventArgs e)
at Ice.Lib.Framework.EpiButton.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

It’s late, so I don’t see anything offhand. have you tried running it through Visual Studio to debug it?

EDIT: Actually I think the problem is with your conditional statement:
Example 1:

if ((bool)edvCallContextBpmDataRow["Checkbox01"] == true)

and Example 2:

edvCallContextBpmDataRow["Checkbox01"] = ! (bool)edvCallContextBpmDataRow["Checkbox01"];

I think the Checkbox01 can be nullable. So it won’t allow a casting of (bool)null.

ok, I got my code from here

I’ve since added a UD field and have now got that working as needed

thanks

I fixed the code on the original post. @Doug.C was right it needed a null check.

			EpiDataView edvCallContextBpmData = ((EpiDataView)(this.oTrans.EpiDataViews["CallContextBpmData"]));
			System.Data.DataRow edvCallContextBpmDataRow = edvCallContextBpmData.CurrentDataRow;
			if ((edvCallContextBpmDataRow != null))
			{
				edvCallContextBpmDataRow.BeginEdit();
				if(edvCallContextBpmDataRow["Checkbox01"] == DBNull.Value)
				{
					edvCallContextBpmDataRow["Checkbox01"] = true;
				}
				else
				{
					edvCallContextBpmDataRow["Checkbox01"] = !(bool)edvCallContextBpmDataRow["Checkbox01"];
				}
				edvCallContextBpmDataRow.EndEdit();
				oTrans.Update();
			}
2 Likes

Thank you @Carson and @Doug.C
If I understand correctly, before the button is clicked for the first time the field value will be null, so when it is clicked for the first time the null check will set the field to true?? and subsequent clicks will change it between true and false

That’s correct.