I have a function that checks if a value already exists a database. It performs this check on every row in a grid:
private bool AlreadyExists(IDsForSupplierOrderImportModel DataToCheck)
{
var ReturnValue = false;
var ReturnMessage = "";
Controller.CheckIfSupplierProductExists(DataToCheck.SupplierProduct, ref
ReturnMessage);
if (string.IsNullOrWhiteSpace(Controller.LastError))
{
if (ReturnMessage != "")
{
SupplierProductExists = false;
cGeneralHelpers.ShowInfoMessage(ReturnMessage);
ReturnValue = true;
}
}
return ReturnValue;
}
The problem is the code will show this info message every time this method is hit. So if a mistake is made on multiple rows, the same info message will pop up every time.
What I have tried:
I've tried fixing it with a bool:
private bool AlreadyExists(IDsForSupplierOrderImportModel DataToCheck)
{
var ReturnValue = false;
var ReturnMessage = "";
bool MessageBoxDisplayed = false;
Controller.CheckIfSupplierProductExists(DataToCheck.SupplierProduct, ref ReturnMessage);
if (string.IsNullOrWhiteSpace(Controller.LastError))
{
if (ReturnMessage != "")
{
SupplierProductExists = false;
if (MessageBoxDisplayed)
{
cGeneralHelpers.ShowInfoMessage(ReturnMessage);
MessageBoxDisplayed = true;
ReturnValue = true;
}
}
}
return ReturnValue;
}
But the problem is the bool will revert to false everytime the method is hit.
TLDR: I need to stop this
cGeneralHelpers.ShowInfoMessage(ReturnMessage);
being hit if it has previously been hit.