25
Views
4
Comments
Solved
How to validate imported excel fields
Application Type
Traditional Web

Hey,

I have a problem with validation my excel. 

I get the excel file correctly (as a List of  Structure), however I need to check types of all my fields, if they were filled in correctly. To be more specific, I need to check if all the inputs are either empty or ints. Any other case should break the function. The easiest would be to loop and check, however I can't loop over a structure. 

I'll be more precise. All the fields are text at the import. Right after the import I want to check all the values. The check will be:
TextToInteger(value) = 1 or TextToInteger(value) = 0 

Any idea how to achieve that? There is a number of fields, so checking it manually is not an option. 

mvp_badge
MVP
Rank: #2
Solution

Hi Adam,

If you have a Structure with 50 Attributes, you'll need to write 50 checks. There's no way to check them all in a generic way, unless you write an Extension for that, using reflection to get the individual Attributes. This is possible, but you'll need some C# knowledge to do that.

Rank: #69

Hi Adam,

If you need to go through all the fields of a structure dynamically, you can transform your structure into a JSON and treat the JSON text.

Use JSON Serialize to transform your structure into JSON.

Note: 

If you set Serialize Default Values to Yes, and you leave the default values empty, the resulting JSON has the JSON equivalents of the default platform values. For example:

  • Empty Boolean converts to false
  • Empty Integer converts to 0
  • Empty DateTime converts to 1900-01-01T00:00:00
Rank: #33816

Well, I can't say I'm happy with the solution. That's an amazing limitation!
Thank you very much for your answers. I decided to do the check manually. 

mvp_badge
MVP
Rank: #2

Well, it's a limitation for your very specific use case for sure, but it's not a very common use case I think. In general you'd have columns with different data types, and also, you'd want to check more than just the column's convertability to an integer, but also the integer's range etc., necessitating "manual" code anyway.