Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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; // SupplierProduct doesn't exist
			
			cGeneralHelpers.ShowInfoMessage(ReturnMessage); // Tell the user 

			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; // SupplierProduct doesn't exist

			if (MessageBoxDisplayed)
			{
				cGeneralHelpers.ShowInfoMessage(ReturnMessage); // Tell the user this

				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.
Posted
Updated 7-Sep-22 6:11am
Comments
Jo_vb.net 7-Sep-22 11:39am    
I would change
if (MessageBoxDisplayed)
to
if (MessageBoxDisplayed =false)

1 solution

Try changing this:
if (MessageBoxDisplayed)
To this:
if (!MessageBoxDisplayed)
 
Share this answer
 
Comments
Will Sewell 8-Sep-22 4:54am    
Thanks as always Griff!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900