Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have some code that imports rows from a .CSV file into my program. I have a validation method that checks whether a model has had data imported into it correctly:

C#
private bool ValidData(SupplierOrderImportModel DataToCheck)
        {
            bool ReturnValue = true;            

            if (ReturnValue)
            {
                if (string.IsNullOrWhiteSpace(DataToCheck.BoxType))
                {
                    ReturnValue = false;
                }
            }            

            if (ReturnValue)
            {                
                if (DataToCheck.BoxQuantity == 0) 
                {
                    ReturnValue = false;
                }
            }            

            return ReturnValue;
        }


BoxType is a string. So, if my importer tries to import a blank cell I can check it.

However, BoxQuantity is an int. When the cell is blank it defaults to 0. So my validation checks if it is 0. I think there must be a more secure way to write the code than "DataToCheck.BoxQuantity == 0"

I also need to check DataToCheck.HarvestDate which is a DateTime. I have no idea how to do the validation on that.

What I have tried:

For the DateTime validation i've tried:

C#
if (ReturnValue)
{
    if(string.IsNullOrWhiteSpace((DateTime)DataToCheck.HarvestDate))
        {
           ReturnValue = false;
        }
}


This won't work because you can't hard cast a DateTime to a string
Posted
Updated 31-Aug-22 5:12am
Comments
Richard MacCutchan 31-Aug-22 10:27am    
You can use the constructors and methods of the DateTime class to validate dates.
PIEBALDconsult 31-Aug-22 10:52am    
Yeah, TryParse might help.

1 solution

My solution that seems to work is this:

C#
if (ReturnValue)
            {                
                if (DataToCheck.BoxQuantity == 0) // If the cell in the .CSV is blank it defaults the value to 0 
                {
                    ReturnValue = false;
                }
            }

            if (ReturnValue)
            {
                if (DataToCheck.HarvestDate == DateTime.MinValue) // If the cell is blank it defaults to DateTime.MinValue
                {
                    ReturnValue = false;
                }
            }


Here's a single line way to solve the problem:

C#
if (ReturnValue)
            {               
                ReturnValue &= (DataToCheck.HarvestDate != 
                DateTime.MinValue);
            }

            return ReturnValue;
 
Share this answer
 
v3
Comments
PIEBALDconsult 31-Aug-22 11:26am    
Well I mean...ReturnValue &= (DataToCheck.HarvestDate != DateTime.MinValue) ;
Will Sewell 31-Aug-22 11:42am    
I'm not sure what you mean here. This sets ReturnValue to true if I try and import a blank cell.
PIEBALDconsult 31-Aug-22 15:39pm    
Your C# code looks VB-ish.
Will Sewell 1-Sep-22 4:15am    
Thanks mate that works. I'll edit the solution.

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