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)
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?
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…
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);
}
}
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.