Record Not Found Error

I have code that I want to go and look and see if a UD110 record exists and if it does I don’t want it to create another UD110 but I want it to create a UD110A record. The code below is what checks to see if the record exists. It keeps throwing an error telling me the record isn’t found (guess it is working!) however, I don’t want it to throw the error I want it to do a different task based upon if it is found or not. Anyone know how I can rewrite this to prevent the error.

{
			UD110exists = false;
			UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans);
			adapterUD110.BOConnect();
			string Key1 = "PMOPERATIONS";
			string Key2 = tbTypeCode1.Value.ToString();
			string Key3 = tbFreqCode1.Value.ToString();
			string Key4 = tbEquipTypeID.Text;
			string Key5 = string.Empty;
			if(adapterUD110.GetByID(Key1, Key2, Key3, Key4, Key5))
			ERROR POPS UP HERE AND DOESN'T CONTINUE WITH CODE
MessageBox.Show("Get By ID Ran");
			{
				bool recSelected = adapterUD110.GetByID(Key1, Key2, Key3, Key4, Key5);
				UD110exists = true;
			}
			
}```


![image|690x452](upload://iEnbghhzqwVLxlZEwhwGKDVGdtu.png)
1 Like

I can’t re-write the code, but I think I can offer a suggestion based on what I am learning. I bet someone more versed will correct me, but it’s worth a try.

Can you set a variable (like var mydataset) = to the result of the adapterUD110.GetByID() and then check to see if there is a row in that variable (mydataset)?

If there are rows then do something, if there aren’t then do something else?

GetByID throws a Record not Found Exeception by design
Change it to GetRows.

2 Likes

There we go. Thanks Jose.

Okay can you help me with how to do that. I am not versed on the GetRows

If you’ve already installed BLTester…

Perfect. I have that. Now how would I say Key2 = ‘D’. Key3 = ‘22’. Do you separate with something?

Okay in BL Tester I got this to work:
image

Now how do I translate that into my code? This is not compiling

{
			MessageBox.Show("UD110 exists is running");
			UD110exists = false;
			UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans);
			adapterUD110.BOConnect();
			string whereClauseUD110 = "Key1 = PMOPERATIONS";
			string Key2 = tbTypeCode1.Value.ToString();
			string Key3 = tbFreqCode1.Value.ToString();
			string Key4 = tbEquipTypeID.Text;
			string Key5 = string.Empty;
			System.Data.DataSet dsUD110 = adapterUD110.GetRows(string whereClauseUD110, string whereClauseUD110Attch, string whereClauseUD110A, string whereClauseUD110AAttch, int pageSize, int absolutePage, out Boolean morePages);
			//if(adapterUD110.GetRows(Key1, Key2, Key3, Key4, Key5))
			if(dsUD110.Row > -1)
			MessageBox.Show("Get By ID Ran");
			{
				bool recSelected = adapterUD110.GetByID(Key1, Key2, Key3, Key4, Key5, "");
				MessageBox.Show("Record Found " + recSelected.ToString());
				UD110exists = true;
			}

Take the first string out of this:

System.Data.DataSet dsUD110 = adapterUD110.GetRows(string whereClauseUD110, string whereClauseUD110Attch, string whereClauseUD110A, string whereClauseUD110AAttch, int pageSize, int absolutePage, out Boolean morePages);

I don’t think you need it because you already gave it a type when you assigned something to it above:

string whereClauseUD110 = “Key1 = PMOPERATIONS”;

Also, what is the compiler saying?

{
			MessageBox.Show("UD110 exists is running");
			UD110exists = false;
			UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans);
			adapterUD110.BOConnect();
			string Key1 = "PMOPERATIONS";
			string Key2 = tbTypeCode1.Value.ToString();
			string Key3 = tbFreqCode1.Value.ToString();
			string Key4 = tbEquipTypeID.Text;
			string Key5 = string.Empty;
			string whereClauseUD110 = "Key1 = 'Key1'" + "Key2 = 'Key2'" + "Key3 = 'Key3'" + "Key4 = 'Key4" + "Key5 = 'Key4'";
			string whereClauseUD110Attch = "";
			string whereClauseUD110A = "";
			string whereClauseUD110AAttch = "";
			int pageSize = 0;
			int absolutePage = 1;
			bool morePages = false;
			System.Data.DataSet dsUD110 = adapterUD110.GetRows(whereClauseUD110, whereClauseUD110Attch, whereClauseUD110A, whereClauseUD110AAttch, pageSize, absolutePage, morePages);
			if(dsUD110.Row > -1)
			MessageBox.Show("Get By ID Ran");
			{
				bool recSelected = adapterUD110.GetByID(Key1, Key2, Key3, Key4, Key5, "");
				MessageBox.Show("Record Found " + recSelected.ToString());
				UD110exists = true;
			}
			
}

These are the errors:

Where do you download the BL tester? I recall it being on the server somewhere, is that right?

Yes I believe it is on the server

Shannon Kearney

1 Like

Your row issue I get, you have to select a table in that dataset. I can’t tell you the syntax cause I’m kinda new at this, but you need to do something like dsUD110.Table.Row… something like this maybe?

if (ud100Adapter.UD100Data.UD100A.Rows.Count > 0)
{
return ud100Adapter.UD100Data.UD100A[0][“Character01”].ToString();
}

The overload thing is confusing me because it is acting as if we aren’t passing it the correct amount of overloads when we are… I am trying it on my side too.

EDIT: What I am seeing @skearney is maybe that we need to use something called SearchOptions for the first parameter and then out more pages so that we have only 2 parameters total.

I think you assign all of the keys in search options so that the search returns your rows. Do some digging on SearchOptions.

Alright, so I don’t know what searchoptions are or how we are supposed to learn about them. I don’t see many posts on here about them or how to construct/add to them, but I borrowed some code from some other post that seems to work so that we can get our GetRows() method to work…

Firstly, your GetRows method only takes two parameters (not sure why the BL testers show so many more). The method takes a SearchOptions parameter and an out paremeter:

adapterUD110.GetRows(opts, out morePages);

So for your code you should do this (remember that I borrowed this code so I am not sure what I am telling you to do here with the search options object- full disclosure):

string wClause = "Key1 =  ' " + Key1 (this is your variable per your code) + " 'and Key2 = ' " + Key2 " AND SO ON AND SO FORTH FOR ALL YOUR KEYs. MAYBE TRY JUST USING ONE KEY AT FIRST;
		
		SearchOptions opts = new SearchOptions(SearchMode.AutoSearch);
		opts.NamedSearch.WhereClauses.Add("UD110",wClause);

Then once you have successfully created the search options you should do this like you kinda had already:

System.Data.DataSet dsUD110 = adapterUD110.GetRows(opts, out morePages);

But there is the change… you need to select the table first before you select the row

When you ran your BL tester you will see that you need to specify the table that you want in your dataset (there are mulitple). This can be seen when you run this in your BL tester as seen below… You can see that there are multiple tables UD110, UD110Attch,UD110A, etc…

So lets do this:

System.Data.DataSet dsUD110 = adapterUD110.GetRows(opts, morePages);

if(udSet.Tables["UD110"].Rows.Count > 0)
		{
			MessageBox.Show(udSet.Tables["UD110"].Rows[0]["Key1"].ToString());
		}

Does that work for you?

@skearney here is an example of something that will retrieve a row from UD110…

private void CallUD110AdapterGetRowsMethod(Ice.Lib.Searches.SearchOptions opts, out bool morePages)
{
	morePages = false;
	System.Data.DataSet dsUD110;
	try
	{
		UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans);
		adapterUD110.BOConnect();
		string wClause = "Key1 = 'MfgSys'";
		opts.NamedSearch.WhereClauses.Add("UD110",wClause);
		// Call Adapter method
		dsUD110 = adapterUD110.GetRows(opts, out morePages);
		// Cleanup Adapter Reference
		adapterUD110.Dispose();

	} catch (System.Exception ex)
	{
		ExceptionBox.Show(ex);
	}
	
	if(dsUD110.Tables["UD110"].Rows.Count > 0)
	{
		MessageBox.Show(dsUD110.Tables["UD110"].Rows[0]["Key1"].ToString());
	}
	
}

And here is full script for similar example
for form UD40 with button click to get a row from UD100.
Seemed OK but I didn’t test much… maybe if you add your extra keys.
// **************************************************
// Custom code for UD40Form
// Created: 5/27/2021 10:46:49 AM
// Wizard Added Assembly Reference - UD100
// **************************************************
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Ice.BO;
using Ice.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;

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 **
bool morePages;
Ice.BO.UD100DataSet dsUD100;
Ice.Lib.Searches.SearchOptions opts = new Ice.Lib.Searches.SearchOptions(SearchMode.AutoSearch);

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.btnUD100.Click += new System.EventHandler(this.btnUD100_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.btnUD100.Click -= new System.EventHandler(this.btnUD100_Click);
	// End Wizard Added Object Disposal

	// Begin Custom Code Disposal

	// End Custom Code Disposal
}

// Reference - Wizard Added UD100 GetRows 
private void CallUD100AdapterGetRowsMethod(Ice.Lib.Searches.SearchOptions opts, out bool morePages)
{
	morePages = false;
	System.Data.DataSet dsUD100;
try
{
	UD100Adapter adapterUD100 = new UD100Adapter(this.oTrans);

	adapterUD100.BOConnect();
	string wClause = "Key1 = 'A010'";
	opts.NamedSearch.WhereClauses.Add("UD100",wClause);
	// Call Adapter method
	dsUD100 = adapterUD100.GetRows(opts, out morePages);
	if(dsUD100.Tables["UD100"].Rows.Count > 0)
	{
		MessageBox.Show(dsUD100.Tables["UD100"].Rows[0]["Key1"].ToString());
	}
	else
	{
		MessageBox.Show("NOT");
	}
	adapterUD100.Dispose();
} 

catch (System.Exception ex)
{
	ExceptionBox.Show(ex);
}

}

private void btnUD100_Click(object sender, System.EventArgs args)
{
	// ** Place Event Handling Code Here **
	CallUD100AdapterGetRowsMethod(opts, out morePages);
}

// Reference - Wizard Added UD100 GetByID 
private void CallUD100AdapterGetByIDMethod()
{
	try
	{
		// Declare and Initialize EpiDataView Variables
		// Declare and create an instance of the Adapter.
		UD100Adapter adapterUD100 = new UD100Adapter(this.oTrans);
		adapterUD100.BOConnect();

		// Declare and Initialize Variables
		// TODO: You may need to replace the default initialization with valid values as required for the BL method call.
		string stringId = String.Empty;

		// Call Adapter method
		bool result = adapterUD100.GetByID(stringId);

		// Cleanup Adapter Reference
		adapterUD100.Dispose();

	} catch (System.Exception ex)
	{
		ExceptionBox.Show(ex);
	}
}

}
End*********************************

1 Like

Okay, for saying you don’t know too much you sure sent a lot of good information. From your info this is what I have so far:

{
			UD110exists = false;
			morePages = false;
			System.Data.DataSet dsUD110;
			try
			{
			UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans);
			adapterUD110.BOConnect();
			string Key1 = "PMOPERATIONS";
			string Key2 = tbTypeCode1.Value.ToString();
			string Key3 = tbFreqCode1.Value.ToString();
			string Key4 = tbEquipTypeID.Text;
			string Key5 = string.Empty;
			string wClause = "Key1 =  ' " + Key1 + " ' and Key2 = ' " + Key2  + " ' and Key3 = ' " + Key3 + " ' and Key4 = ' " + Key4 + " ' and Key5 = ' " + Key5 + "'";
			MessageBox.Show(wClause.ToString());
			opts.NamedSearch.WhereClauses.Add("UD110", wClause); ////Error Here
			MessageBox.Show("Before getting the Data Set");
			dsUD110 = adapterUD110.GetRows(opts, out morePages);
			MessageBox.Show("After getting the Data Set");
			if(dsUD110.Tables["UD110"].Rows.Count > 0)
			{
				UD110exists = true;
			}
				
				adapterUD110.Dispose();
			}
	catch (System.Exception ex)
	{
		ExceptionBox.Show(ex);
	}
			
}

That code works perfectly if a record exists and it sets the UD110 exists to true accordingly.

However, if a UD110 record doesn’t exists I am getting an error where I have shown it in the code. Attached is the error.
image

Any ideas?

@utaylor @josecgomez

If I am understanding this correctly you want to just handle the Record Not Found exception and proces some other code when it is not found.

First assign Key1 through Key5 as you are doing

then

try
{
	adapterUD110.GetByID(Key1,Key2,Key3,Key4,Key5);
	//Continue with code IF UD110 Record was found
}
catch(RecordNotFoundException)  //The GetByID Record Not Found Exception will fall to here skipping the rest of the code after the GetByID in the try block
{
	//process code when Record Not Found
}

Now you are handling the exception, in your case an expected exception, and no error will be thrown.

Scott

2 Likes

Another option would be to use the Try…Catch logic, and in the catch (meaning the UD110 is not there) create it.

try
{
GetByID
}
catch
{
GetANewUD110
set fields
Update
}
process child records

1 Like

I wish I knew it was going to be this easy! After many failures this is the code that works for me! Thank you everyone for your help.

	private void UD110Exists()
{
			UD110exists = true;
			try
			{
			UD110Adapter adapterUD110 = new UD110Adapter(this.oTrans);
			adapterUD110.BOConnect();
			string Key1 = "PMOPERATIONS";
			string Key2 = tbTypeCode1.Value.ToString();
			string Key3 = tbFreqCode1.Value.ToString();
			string Key4 = tbEquipTypeID.Text;
			string Key5 = string.Empty;
			adapterUD110.GetByID(Key1, Key2, Key3, Key4, Key5);
			}
			catch (System.Exception ex)
			{
				UD110exists = false;
			}
	}