Update UD110A Child Table

I want to update the UD110A child that matches the UD110 selected. I have this code but it isn’t compiling:

private void CallUD110AdapterGetByIDMethodDelete()
	{
		try
		{
			UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans);
			adapterUD110.BOConnect();
			string Key1 = "PMOPERATIONS";
			string Key2 = tbFreqCodeBase.Text;
			string Key3 = tbTypeCodeBase.Text;
			string Key4 = tbEquipTypeID.Text;
			string Key5 = string.Empty;
			string childKey1 = "OPERINSTRUCT";
			string childKey2 = tbInstNum.Text;
			string childKey3 = string.Empty;
			string childKey4 = string.Empty;
			string childKey5 = string.Empty;
			bool result = adapterUD110.GetByID(Key1, Key2, Key3, Key4, Key5);
			if (result)
			{
			edvUD110A = new EpiDataView();
			edvUD110A.dataView = new DataView(UD110Adapter.UD110Data.UD110A);
			edvUD110A.dataView.RowFilter = "ChildKey 1 = '" + childKey1 + "' and ChildKey2 ='" + childKey2;
			adapterUD110.UD110Data.UD110A[0].CheckBox01 = true;
	    	adapterUD110.UD110Data.UD110A[0].RowMod = "U";
			adapterUD110.Update();
			adapterUD110.Dispose();
			}

		} catch (System.Exception ex)
		{
			ExceptionBox.Show(ex);
			MessageBox.Show("PMS Instruction wasn't updated");
		}
			}

Error is
Error: CS0120 - line 340 (6052) - An object reference is required for the non-static field, method, or property ‘Ice.Adapters.UD110Adapter.UD110Data.get’

I got it figured out.
Here is the code:

private void CallUD110AdapterGetByIDMethodDelete()
	{
		try
		{
			UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans);
			adapterUD110.BOConnect();
			string Key1 = "PMOPERATIONS";
			string Key2 = tbFreqCodeBase.Text;
			string Key3 = tbTypeCodeBase.Text;
			string Key4 = tbEquipTypeID.Text;
			string Key5 = string.Empty;
			string childKey1 = "OPERINSTRUCT";
			string childKey2 = tbInstNum.Text;
			string childKey3 = string.Empty;
			string childKey4 = string.Empty;
			string childKey5 = string.Empty;
			bool result = adapterUD110.GetByID(Key1, Key2, Key3, Key4, Key5);
			foreach(DataRow dr in adapterUD110.UD110Data.UD110A.Rows)
			if(dr["ChildKey1"].ToString() == childKey1 && dr["ChildKey2"].ToString() == childKey2 && dr["ChildKey3"].ToString() == childKey3 && dr["ChildKey4"].ToString() == childKey4)
			{
			adapterUD110.UD110Data.UD110A[0].CheckBox01 = true;
	    	adapterUD110.UD110Data.UD110A[0].RowMod = "U";
			adapterUD110.Update();
			adapterUD110.Dispose();
			}

		} catch (System.Exception ex)
		{
			ExceptionBox.Show(ex);
			MessageBox.Show("PMS Instruction wasn't updated");
		}
			}

What was the issue?

I had to get ahold of the correct child keys:

foreach(DataRow dr in adapterUD110.UD110Data.UD110A.Rows)

if(dr[“ChildKey1”].ToString() == childKey1 && dr[“ChildKey2”].ToString() == childKey2 && dr[“ChildKey3”].ToString() == childKey3 && dr[“ChildKey4”].ToString() == childKey4)

Spoke too soon darn it.
It is updating the correct UD110 but still not the correct UD110A. Somehow I am not telling it to update that record. So frustrating. Anyone know what I’m doing wrong?

Are you writing this on the UD110.dll form as a form customization?

I don’t quite understand the adapterUD110.UD110Data.UD110A.Rows object… Are those all of the rows that are currently in the session since you used UD110Adapter(this.oTrans); ?

If that is the case, okay cool, I learned something, but it really confuses me too. Are these rows the same rows that are in the UD110A dataview?

In any case, say that does return all of the rows linked to your current oTrans session… I see you looping through each one of those rows until you find a match.

What I don’t see you doing is finding the row number that matches and then updating that row number specifically. I see you using the value 0 in this statement:

adapterUD110.UD110Data.UD110A[0].CheckBox01 = true;

I thought that 0 meant the first row in the table. If it isn’t the first row in the table that matches your criteria, what happens… maybe that is what is happening here. Maybe it isn’t the first child row that matches your criteria yet you are updating the first row anyways.

No I have added UD110 as a DataView to the Equipment form customization.

Once it finds the appropriate UD110 record then it is looping thru all of the UD110A child records to see if any meet the criteria I am looking for in the ChildKey’s.

You are exactly correct. I am not finding the row number that matches and then updating that row number because I don’t know how to do that. How do I get the number and then update just that row.

You can always add an increment variable.

int i = 0;

foreach(DataRow dr in adapterUD110.UD110Data.UD110A.Rows)
{
if(dr[“ChildKey1”].ToString() == childKey1 && dr[“ChildKey2”].ToString() == childKey2 && dr[“ChildKey3”].ToString() == childKey3 && dr[“ChildKey4”].ToString() == childKey4)
{
adapterUD110.UD110Data.UD110A[i].CheckBox01 = true;
adapterUD110.UD110Data.UD110A[i].RowMod = “U”;
adapterUD110.Update();
adapterUD110.Dispose();
}
else
{
i++;
}
}

I think this would work?

Are you sure it is even finding rows? Is that how you are supposed to loop through rows in UD110A?

Okay, I think you are on to something here. I added the part about GetRowEnumerator on the for each. It is still not updating the correct row so there is something I need to still get correct in that part.

	private void CallUD110AdapterGetByIDMethodDelete()
	{
		try
		{
			UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans);
			adapterUD110.BOConnect();
			string Key1 = "PMOPERATIONS";
			string Key2 = tbFreqCodeBase.Text;
			string Key3 = tbTypeCodeBase.Text;
			string Key4 = tbEquipTypeID.Text;
			string Key5 = string.Empty;
			string childKey1 = "OPERINSTRUCT";
			string childKey2 = tbInstNum.Text;
			string childKey3 = string.Empty;
			string childKey4 = string.Empty;
			string childKey5 = string.Empty;
			bool result = adapterUD110.GetByID(Key1, Key2, Key3, Key4, Key5);
			//foreach(DataRow dr in adapterUD110.UD110Data.UD110A.Rows)
			foreach(UltraGridRow dr in this.gridPMInstructions.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
			if(dr.GetCellValue("UD110A_ChildKey1").ToString() == childKey1 && dr.GetCellValue("UD110A_ChildKey2").ToString() == childKey2 && dr.GetCellValue("UD110A_ChildKey3").ToString() == childKey3 && dr.GetCellValue("UD110A_ChildKey4").ToString() == childKey4)
			{
			bool checkstatus = true;	
			adapterUD110.UD110Data.UD110A[0].CheckBox01 = checkstatus;
	    	adapterUD110.UD110Data.UD110A[0].RowMod = "U";
			adapterUD110.Update();
			adapterUD110.Dispose();
			}
		} catch (System.Exception ex)
		{
			ExceptionBox.Show(ex);
			MessageBox.Show("PMS Instruction wasn't updated");
		}
			}

Yeah I tried it myself and it didn’t update anything.

Try putting a message box in your foreach loop. Do you even see that message box pop up?

In other words, are you getting any rows to begin with?

I agree, first verify you are getting in the ultra grid row loop like you expect. Put a message box in your for each, right before

bool checkstatus = true;	

Once in the loop over the ultra grid rows, you still to loop over the UD100A rows to find the right one, as @utaylor showed above(break out of the for each loop once you find your match).

1 Like

@skearney I don’t know if this will work for you because I don’t know where you are calling this from or if you are trying to update this using a business logic method call, but if you have the UD110A dataview in your form at the time this method is ran, I believe you can do this:

private void Testing()
{
	
	EpiDataView ThisUD110A;

	ThisUD110A = (EpiDataView)(this.oTrans.EpiDataViews["UD110A"]);

	string childKey1 = "test";
	string childKey2 = string.Empty;
	string childKey3 = string.Empty;
	string childKey4 = string.Empty;
	string childKey5 = string.Empty;
	
	
	foreach(System.Data.DataRow dr in ThisUD110A.dataView.Table.Rows)
	{
		//MessageBox.Show(dr["Key1"].ToString());
		if(dr["Key1"].ToString() == "test")
		{
			dr.BeginEdit();
			dr["Checkbox01"] = true;
			dr.EndEdit();
		}
		
	}

}

@skearney

I borrowed some code from @hkeric.wci to create a table that only has the rows you want in it. That way you don’t have to loop through the whole UD110A child table if only SOME of the rows are matching your criteria. For example, if you had 100 child rows, but only two matched, the foreach loop you wrote would have to go through all of them. I think this way that I copied from @hkeric.wci may be more efficient (I don’t really know though). It creates a table that you can then loop through and change.

> 
	EpiDataView ThisUD110A;
	ThisUD110A = (EpiDataView)(this.oTrans.EpiDataViews["UD110A"]);

> 		System.Data.DataRow[] Test;
> 		string whereClause = string.Empty;
> 		whereClause = String.Format("Key1 = '{0}'", "test");
> 		Test = ThisUD110A.dataView.Table.Select(whereClause);
> 		
> 
> 		foreach(System.Data.DataRow dr in Test)
> 		{
> 			//MessageBox.Show(dr["ChildKey1"].ToString());
> 			dr.BeginEdit();
> 			dr["Checkbox01"] = true;
> 			dr.EndEdit();
> 		}

I added the message box and it is finding a record that matches. It is updating the first keys it finds for the UD110 table but it isn’t picking the correct UD110A record. I just need to figure out how to tell it which record to update.
@utaylor

I’m about to give up on this. I feel like I’m just spinning in circles.

Can you use the code I provided where you call the dataview instead of using the adapter?

Or do you have to use the adapter?

private void Testing()
{
	
	EpiDataView ThisUD110A;

	ThisUD110A = (EpiDataView)(this.oTrans.EpiDataViews["UD110A"]);

	string childKey1 = "test";
	string childKey2 = string.Empty;
	string childKey3 = string.Empty;
	string childKey4 = string.Empty;
	string childKey5 = string.Empty;
	
	
	foreach(System.Data.DataRow dr in ThisUD110A.dataView.Table.Rows)
	{
		//MessageBox.Show(dr["Key1"].ToString());
		if(dr["Key1"].ToString() == "test")
		{
			dr.BeginEdit();
			dr["Checkbox01"] = true;
			dr.EndEdit();
		}
		
	}

}

Hey Shannon,
Can you provide the latest state of your customization?
Don’t give up, this is possible.

1 Like

Here is my latest code.

	{
		try
		{
			UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans);
			adapterUD110.BOConnect();
			string Key1 = "PMOPERATIONS";
			string Key2 = tbFreqCodeBase.Text;
			string Key3 = tbTypeCodeBase.Text;
			string Key4 = tbEquipTypeID.Text;
			string Key5 = string.Empty;
			string childKey1 = "OPERINSTRUCT";
			string childKey2 = tbInstNum.Text;
			string childKey3 = string.Empty;
			string childKey4 = string.Empty;
			string childKey5 = string.Empty;
			bool result = adapterUD110.GetByID(Key1, Key2, Key3, Key4, Key5);
			//foreach(DataRow dr in adapterUD110.UD110Data.UD110A.Rows)
			foreach(UltraGridRow dr in this.gridPMInstructions.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
			if(dr.GetCellValue("UD110A_ChildKey1").ToString() == childKey1 && dr.GetCellValue("UD110A_ChildKey2").ToString() == childKey2 && dr.GetCellValue("UD110A_ChildKey3").ToString() == childKey3 && dr.GetCellValue("UD110A_ChildKey4").ToString() == childKey4)
			{
			MessageBox.Show("Record exists");
			bool checkstatus = true;	
			adapterUD110.UD110Data.UD110A[0].CheckBox01 = checkstatus;
	    	adapterUD110.UD110Data.UD110A[0].RowMod = "U";
			adapterUD110.Update();
			adapterUD110.Dispose();
			
			}
		} catch (System.Exception ex)
		{
			ExceptionBox.Show(ex);
			MessageBox.Show("PMS Instruction wasn't updated");
		}
			}

Hi Shannon,

Try updating your loop to this:
Also, I think you may have been disposing the adapter to soon(if there may be more than row), i wrapped it in a using statement and opened up the scope a bit.

using(UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans))
{

...
...
...

 foreach(UltraGridRow dr in this.gridPMInstructions.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
            {
                bool shouldBreak = false;
                for(int i = 0; i < adapterUD110.UD110Data.UD110A.Rows.Count; i++)
                {
                    if(dr.GetCellValue("UD110A_ChildKey1").ToString() == childKey1 && dr.GetCellValue("UD110A_ChildKey2").ToString() == childKey2 && dr.GetCellValue("UD110A_ChildKey3").ToString() == childKey3 && dr.GetCellValue("UD110A_ChildKey4").ToString() == childKey4 && dr.GetCellValue("UD110A_ChildKey5").ToString() == childKey5)
                    {
                        MessageBox.Show("Record exists");
                        bool checkstatus = true;	
                        adapterUD110.UD110Data.UD110A[i].CheckBox01 = checkstatus;
                        adapterUD110.UD110Data.UD110A[i].RowMod = "U";
                        adapterUD110.Update();
                        shouldBreak = true;
                    }
                    if(shouldBreak)
                        break;
                }
			}
}