score:-1

i think if you explain more precisely what do you want to do in your view we will be able to help you better.

when i see you code, i deduce that your table in your view will display just one record with his appropriate properties/columns. if you want to do that, its ok.

on the other hand, iterate over a table according to the number of columns is a bad idea because your are coupling two abstraction levels. what i mean is that your mvc method should not have knowledge of the number of columns and this is what your are doing with "for(int i = 1; i < 100; i++)". indeed, what happen if there is a table schema update and the number of columns changes ? the same for the name of the columns ? your code will be break !

i think you are taking the problem from the wrong side to me.

score:0

there is no reason to repeatedly take the first element from the same unchanging collection. simple take the first element from the collection, and then work with that retrieved object:

var element = db.datatable.firstordefault();

this already dramatically simplifies your code and improves performance (because you don't need to iterate over the collection for every property):

if(element.d1 != null)
{
    //do code
}

// and so on...

it is unclear exactly why you need to ascertain that each field contains a not-null value. you never posted any code that shows how you intend to use these values.

//here i would add the value (0 for null, 1 for data) to an array, which i would handle in the view

generally, it's better to simply ensure that your view can handle nulls, as this means you don't need to worry about your code executing when a certain value is null.

if that is not possible, then it's still easier to simply convert null values into a default not-null value. this means you still don't have to worry about nulls (after you've converted them) since you won't have any null values anymore.

a simple way to do this is by using the null coalescing operator, e.g.

var firstname = element.d1 ?? string.empty;

this ensures that null values get replaced by a default value (which you can specify, i just chose an empty string here but you can pick whatever you want).

also note that you don't even need to do this for non-nullable values such as int, as these will default to 0 automatically.

//here i would add the value to an array

why use an array instead of an object? it's unclear why you are deadset on manually handling every property (and its value) instead of passing the object by itself, which is far and away the better oop way of passing data.

it's contradictory to at the same time want to handle all data manually and also not want to have to handle all that data.


Related Query

More Query from same tag