Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

I have code that takes a file from a folder, and attaches it to a receipt header. It was working just fine, but then I got the above error for every file I try to save now. It won’t work anymore! Here is my code:
try{
//Creates a record attached to the part#
ReceiptAdapter adapterReceipt = new ReceiptAdapter(this.oTrans);
adapterReceipt.BOConnect();

	POAdapter adapterPO = new POAdapter(this.oTrans);
	adapterPO.BOConnect();

	string PONum = po;
	string PackNum = pack;

	adapterPO.GetByID(Convert.ToInt32(po));

	EpiDataView edvPA2 = new EpiDataView(); 
	edvPA2.dataView = new DataView(adapterPO.POData.POHeader);

	int vend = Convert.ToInt32(edvPA2.dataView[edvPA2.Row]["VendorNum"]); 
	string purch = Convert.ToString(edvPA2.dataView[edvPA2.Row]["PurPoint"]);

	try
	{
		adapterReceipt.GetNewRcvHeadAttch(vend,purch,pack);
	} catch (System.Exception ex)
	{
		errorlog("NewAttchR() " + newfile,ex.ToString());
		MessageBox.Show(Convert.ToString(ex));
		return;
	}

	DataRow newXAttchRow = adapterReceipt.ReceiptData.RcvHeadAttch[adapterReceipt.ReceiptData.RcvHeadAttch.Rows.Count - 1];
	newXAttchRow["Company"] = "NET";
	newXAttchRow["FileName"] = newfile;
	newXAttchRow["DrawDesc"] = Desc;
	newXAttchRow["DocTypeID"] ="PACK";
	adapterReceipt.Update();

	adapterPO.Dispose();
	adapterReceipt.Dispose();
	oTrans.Update();
	oTrans.Refresh();

All the variables are provided from a different function. PurPoint is always an empty value, but everything else exists as a record in Epicor. Here is the error I get for a document I am trying to save:

System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

Table: RcvHeadAttch
Company=‘NET’ VendorNum=‘356’ PurPoint=‘’ PackSlip=‘874220’ DrawingSeq=‘0’ SysRowID=‘00000000-0000-0000-0000-000000000000’: ForeignKeyConstraint RcvHeadToRcvHeadAttch requires the child key values (NET, 356, , 874220) to exist in the parent table. —> System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
at System.Data.DataSet.EnableConstraints()
at System.Data.DataSet.set_EnforceConstraints(Boolean value)
at System.Data.DataTable.EndLoadData()
at Ice.DatasetAdapter.CopyTSTableToDataTable[TIceRow,TDataTable](IceTable1 sourceTSTable, TDataTable destinationTable) --- End of inner exception stack trace --- at Ice.DatasetAdapter.CopyTSTableToDataTable[TIceRow,TDataTable](IceTable1 sourceTSTable, TDataTable destinationTable)
at Erp.Proxy.BO.ReceiptImpl.GetNewRcvHeadAttch(ReceiptDataSet ds, Int32 vendorNum, String purPoint, String packSlip)
at Erp.Adapters.ReceiptAdapter.GetNewRcvHeadAttch(Int32 vendorNum, String purPoint, String packSlip)
at Script.NewAttchR(String newfile, String Desc, String po, String pack)

If I can state the obvious, here is the issue -

With that said, it seems like I had a similar issue where the adapter itself has to have the head record in question. You need to use this:
public ReceiptDataSet GetByID(int vendorNum, string purPoint, string packSlip)

try this out

//assign your values here...
int vendornum;
string purpoint;
string packslip;

adapterReceipt.ClearData();
adapterReceipt.getByID(new object[]{vendornum, purpoint, packslip});

//now try your code here...

I also notice this method, that may be useful in the future:

public bool GetByIdChkContainerID(int piVendorNum, string pcPurPoint, string pcPackSlip, int poNum)
3 Likes

ugh. I feel like an idiot haha. Every other function I have the GetByID setup, but not for this one… What is strange to me is that it was working, then yesterday started crapping out. Works like a charm, Thank you Chris!

1 Like

Glad to help buddy!