Baffled with Specified cast is not valid happening at .Update()

This is simple code that checks to see if a checkbox is set and flip it.

private bool ResetCycleCount()
	{
		bool answer = false;
		UD40Adapter adUD40 = new UD40Adapter(this.oTrans);
		adUD40.BOConnect();
		try
		{
			answer = adUD40.GetByID("COILID_CycleCountReset", string.Empty, string.Empty, string.Empty, string.Empty);
			if(!answer){return answer;}

			if(adUD40.UD40Data.UD40[0].CheckBox01 == true)
			{
	            adUD40.UD40Data.UD40[0].CheckBox01 = false;
			}
			else
			{
	        	adUD40.UD40Data.UD40[0].CheckBox01 = true;
			}

	        adUD40.UD40Data.UD40[0].RowMod = "U";
	        adUD40.Update();

		} 
		catch (System.Exception ex){ExceptionBox.Show(ex);answer = false;}
		finally{adUD40.Dispose();}
		return answer;
	}

The code is having problem at adUD40.Update();

The error below that I am getting is confusing me because what do I need to “Cast” when I’m only dealing with booleans and they are lowercase?

Server Side Exception

BPM runtime caught an unexpected exception of 'InvalidCastException' type.
See more info in the Inner Exception section of Exception Details.

Exception caught in: Epicor.ServiceModel

Error Detail 
============
Description:  BPM runtime caught an unexpected exception of 'InvalidCastException' type.
See more info in the Inner Exception section of Exception Details.
Program:  RefEmit_InMemoryManifestModule
Method:  lambda_method
Original Exception Type:  InvalidCastException
Framework Method:  SetValue
Framework Line Number:  126
Framework Column Number:  13
Framework Source:  SetValue<TValue> at offset 73 in file:line:column C:\_Releases\ICE\ICE3.2.100.6\Source\Shared\Framework\Epicor.ServiceModel\Ice\Tableset\PropertyIceColumn.cs:126:13

Server Trace Stack:     at lambda_method(Closure , Object , Object )
   at Ice.PropertyIceColumn.SetValue[TValue](Object row, TValue value) in C:\_Releases\ICE\ICE3.2.100.6\Source\Shared\Framework\Epicor.ServiceModel\Ice\Tableset\PropertyIceColumn.cs:line 126
   at Ice.DynamicColumnValues.SetIceColumnValueOrDynamicValue(IDynamicColumnValues row, String columnName, Object value) in C:\_Releases\ICE\ICE3.2.100.6\Source\Shared\Framework\Epicor.ServiceModel\Ice\Tableset\DynamicColumnValues.cs:line 235
   at Epicor.Data.TempRowBase.set_Item(String columnName, Object value) in C:\_Releases\ICE\ICE3.2.100.6\Source\Shared\Framework\Epicor.ServiceModel\Data\TempRowBase.cs:line 31
   at Epicor.Customization.Bpm.BOA011D23E27D442A1BB3678072A2A6C0D.UpdatePreProcessingDirective_COILID_CycleCountReset_FA3063C7C95E23B2E611F9A8F93610D8.A002_CustomCodeAction()
   at Epicor.Customization.Bpm.BOA011D23E27D442A1BB3678072A2A6C0D.UpdatePreProcessingDirective_COILID_CycleCountReset_FA3063C7C95E23B2E611F9A8F93610D8.ExecuteCore()
   at Epicor.Customization.Bpm.DirectiveBase`3.Execute(TParam parameters) in C:\_Releases\ICE\ICE3.2.100.6\Source\Server\Internal\Lib\Epicor.Customization.BPM\DirectiveBase.Generic.cs:line 131



Client Stack Trace 
==================
   at Epicor.ServiceModel.Channels.ImplBase`1.ShouldRethrowNonRetryableException(Exception ex, DataSet[] dataSets)
   at Ice.Proxy.BO.UD40Impl.Update(UD40DataSet ds)
   at Ice.Adapters.UD40Adapter.OnUpdate()
   at Ice.Lib.Framework.EpiBaseAdapter.Update()
   at Script.ResetCycleCount()

Inner Exception 
===============
Specified cast is not valid.

Any help/breadcrumbs will be welcomed.

I don’t have a dev machine handy, but adUD40.GetByID returns a UD40TableSet and the code below is trying to assign it to a variable “answer” which is declared as a bool.

2 Likes

Erik is spot on. Change your return type to DataSet or just return true instead if records exist.

protected override DataSet OnGetByID(params object[] parameters)
{
    if (parameters.Length != 5)
    {
        throw new TargetParameterCountException();
    }
    return this.uD40BO.GetByID((string) parameters[0], (string) parameters[1], (string) parameters[2], (string) parameters[3], (string) parameters[4]);
}

 

 

Thanks for your help.

I checked in to the return type for the GetByID method which is a boolean.
ReturnType

When I do a Visual Studio debug of the code the error takes place during the adUD40.Update(); not at the adUD40.GetByID();

Still, taking your advice I did change the code to use a return type of DataSet and the code still fails at answer = adUD40.Update();:

	private bool ResetCycleCount()
	//private void CallUD40AdapterGetRowsMethod()
	{
		bool answer = false;
		UD40Adapter adUD40 = new UD40Adapter(this.oTrans);
		adUD40.BOConnect();
		try
		{
			// Declare and Initialize Variables
			// TODO: You may need to replace the default initialization with valid values as required for the BL method call.
			SearchOptions opts = new SearchOptions(SearchMode.AutoSearch);
			string strWhereClause = "Company = 'xxxxxxxx' and ";
			strWhereClause += "key1 = 'COILID_CycleCountReset'";
			opts.NamedSearch.WhereClauses.Add("UD40", strWhereClause);
			bool morePages = false;

			// Call Adapter method
			System.Data.DataSet dsUD40 = adUD40.GetRows(opts, out morePages);

			foreach (DataRow drUD40 in dsUD40.Tables["UD40"].Rows) 
			{
				if(Convert.ToBoolean(drUD40["CheckBox01"]) == true)
				{
					drUD40["CheckBox01"] = false;
				}
				else
				{
					drUD40["CheckBox01"] = true;
				}

		        drUD40["RowMod"] = "U";
			}

			answer = adUD40.Update();
		} 
		catch (System.Exception ex)
		{
			ExceptionBox.Show(ex);answer = false;
		}
		finally{adUD40.Dispose();}
		return answer;
	}

Your initial code with the GetByID is fine. The bool is correct on the GetByID
This error is referencing a BPM. Do you have a BPM on UD40?

Yes, but I have disabled it.

Hmmm. What screen is this customization in and at what is the event driving it?

EDIT: And what version of E10?

Your Error is coming from a BPM, not from your code on the screen :slight_smile:
Look at the stack trace

 at Epicor.Customization.Bpm.BOA011D23E27D442A1BB3678072A2A6C0D.UpdatePreProcessingDirective_COILID_CycleCountReset_FA3063C7C95E23B2E611F9A8F93610D8.A002_CustomCodeAction()
   at Epicor.Customization.Bpm.BOA011D23E27D442A1BB3678072A2A6C0D.UpdatePreProcessingDirective_COILID_CycleCountReset_FA3063C7C95E23B2E611F9A8F93610D8.ExecuteCore()

Looks like you have a PreProcessing BPM called COILID_CycleCountReset which is casting something which I’m guessing you aren’t passing.

EPICOR version: ERP10.2.100.6

This program is a hijack of the HH Serial Tracker program where we overlay a frontend.
ResetCyclecount

I am firing the ResetCycleCount() during the btnReset_Click().

```private void btnReset_Click(object sender, System.EventArgs args)
{
	if(ResetCycleCount()){ConfigBubble(string.Empty,"Coils reset for a Cycle Count.",StatusTypes.OK);}
	else{ConfigBubble(string.Empty,"Did not work. Notify IT.",StatusTypes.Stop);}
}```

The entirety of the code:

/**************************************************
Custom code for HHSerialTrackerForm
Created: 8/24/2017 2:31:48 PM
Customization: HH_Coil_UDCOIL5(C)
BPMs:  
	Pre/Post-Processing  / BusinessObject.Method / DirectiveName
	(i.e.) Pre-Processing  / IssueReturn.OnChangeTranQty / SpareParts-AddReasonCode
Menu Location:
	Production Mgt/Data Collection/Handheld/Coils - HandHeld --> Reset Cycle Count button 
	Material Mgt/Data Collection/Handheld/Coils - HandHeld --> Reset Cycle Count button 
	Process -->(MenuID - UDCOIL5)    COIL5 - Match Receipts
Notes:
	
Developer:
	Will King
Date E10 Ready:
	__/__/_____
***************************************************/

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

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.btnReset.Click += new System.EventHandler(this.btnReset_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.btnReset.Click -= new System.EventHandler(this.btnReset_Click);
		// End Wizard Added Object Disposal
		// Begin Custom Code Disposal
		// End Custom Code Disposal
	}

	private void HHSerialTrackerForm_Load(object sender, EventArgs args)
	{
		// Add Event Handler Code
		txtNote.Text = "This resets the Cycle Count.\n\nEnsure that this is something that you want to do.";
	}

	private void btnReset_Click(object sender, System.EventArgs args)
	{
		if(ResetCycleCount()){ConfigBubble(string.Empty,"Coils reset for a Cycle Count.",StatusTypes.OK);}
		else{ConfigBubble(string.Empty,"Did not work. Notify IT.",StatusTypes.Stop);}
	}

	private void ConfigBubble(string Message,string Caption,Ice.Lib.Framework.StatusTypes status)
	{
		if(!string.IsNullOrEmpty(Message)){MessageBox.Show(Message);}

		if(!string.IsNullOrEmpty(Caption)){shpBubble.Visible = true;}
		else{shpBubble.Visible = false;}

        shpBubble.Status = status;
        shpBubble.EnabledCaption = Caption;
	}

	private bool ResetCycleCount()
	{
		bool answer = false;
		UD40Adapter adUD40 = new UD40Adapter(this.oTrans);
		adUD40.BOConnect();
		try
		{
			answer = adUD40.GetByID("COILID_CycleCountReset", string.Empty, string.Empty, string.Empty, string.Empty);
			if(!answer){return answer;}

			if(adUD40.UD40Data.UD40[0].CheckBox01 == true)
			{
	            adUD40.UD40Data.UD40[0].CheckBox01 = false;
			}
			else
			{
	        	adUD40.UD40Data.UD40[0].CheckBox01 = true;
			}

	        adUD40.UD40Data.UD40[0].RowMod = "U";
	        adUD40.Update();

		} 
		catch (System.Exception ex){ExceptionBox.Show(ex);answer = false;}
		finally{adUD40.Dispose();}
		return answer;
	}
}

This was on me. I disabled the BPM on UD40.GetById vice the UD40.Update().

Disabled it and the customization works. <heavysigh>Now to work on the broken BPM</heavysigh>

Thanks for all that helped. Glad to have another set of eyes.

I was just going to follow-up on my earlier post and ask you to double-check :slight_smile:

1 Like