Issue converting string to int in BPM

Hello,

I am attempting to store all ShipToRows in a post-proc method directive for SalesOrder.ChangeCustomer. This isn’t my end game, but it’s a means to the end I seek.

When the ChangeCustomer method fires, I want to query all ShipTos via linq that match the CustNum of the current context. Currently, I’m fetching just the first record off of the shipTos variable for debugging purposes. When I traced, I saw that the value I want is in the SalesOrderDataSet, which I’ve tried to access using ds.OrderHed["CustNum"]

I’m experiencing an issue where no matter what I do, I cannot convert the CustNum from the ds to an Int. I’m guessing this is because I’m doing something wrong and I’m not getting a value that I can convert when I reference ds.OrderHed["CustNum"].

Attached is my code:

int ipCustNum = Int32.Parse(ds.OrderHed["CustNum"]);
var shipTos = (from shipto_row in Db.ShipTo.With(LockHint.NoLock)
               where shipto_row.CustNum == ipCustNum
               select shipto_row).FirstOrDefault();

if (shipTos != null) {
  return true;
  }
  
return false;

The error I receive is: Argument 1: cannot convert from 'string' to 'int'

Try… Convert.ToInt32(yourcol) or Erp.ErpEFFunctions.ConvertToInt(yourcol)

1 Like

Thank you for your reply. I also tried your approach with the same results.

I think your missing some code…

ds.OrderHed["CustNum"]

I am going to assume you are in some kind of Order BPM so yes you can use ds.TableName or ttTableName (alias).

I think you are missing a

var OrderHedRow = ttOrderHed.FirstOrDefault();

Then you could use

OrderHedRow.CustNum

You could even expiriment with JOINs, an example Code

// Initialize Vars
string existingPO = string.Empty;

var shipDtlRows =
	from tsd in ttShipDtl
	where tsd.Added() || tsd.Updated()
	select tsd;

foreach (var ttShipDtlRow in shipDtlRows)
{
	existingPO =
		(from sd in Db.ShipDtl
			join oh in Db.OrderHed.With(LockHint.NoLock)
				on new { sd.Company, sd.OrderNum } equals new { oh.Company, oh.OrderNum }

			join c in Db.Customer.With(LockHint.NoLock)
				on new { oh.Company, oh.CustNum, CheckBox01 = true } equals new { c.Company, c.CustNum, c.CheckBox01 }
		where
			sd.Company == ttShipDtlRow.Company &&
			sd.PackNum == ttShipDtlRow.PackNum &&
			sd.OrderNum != ttShipDtlRow.OrderNum

		select oh.PONum).DefaultIfEmpty("").First();

	if (existingPO != string.Empty)
	{
		// What we are processing now, does it match our discovery
		bool doesNotMatch =
			(from oh in Db.OrderHed.With(LockHint.NoLock)
			where oh.Company == ttShipDtlRow.Company && oh.OrderNum == ttShipDtlRow.OrderNum && oh.PONum != existingPO
			select oh).Any();

		if (doesNotMatch)
		{
			throw new Ice.BLException("Current Lines PO Number does not match previous Lines PO Number");
		}
	}
}
1 Like

Oh Haso is right, you cant do this above. Perhaps you only want to look at the first record? First(). But you’ll need to access a row before you try to pull a field.

1 Like

@hkeric.wci @Chris_Conn thank you both for taking the time to shed some light on best practices with the dataset. I’m going to take a crack at the suggestion and will followup on how I make out.

As well… CustNum is already a type integer… no need to convert it

Thank you all for your input. I indeed needed to select a single row from the ttOrderHed ds.

This checked out OK:

var OrderHed_row = ttOrderHed.FirstOrDefault();
int ipCustNum = OrderHed_row.CustNum;

var shipTos = (from shipto_row in Db.ShipTo.With(LockHint.NoLock)
               where shipto_row.CustNum == ipCustNum
               select shipto_row).FirstOrDefault();

if (shipTos != null) {
  return true;
  }
  
return false;