Leave empty fields of datatable to insert

0

How can I do it so that the Null or empty value does not become zero. I have this line:

int T150 = Convert.ToInt32(dt.Rows[j]["T150"] is DBNull ? null : dt.Rows[j]["T150"]);

and every time I execute it, it inserts zeros in the fields that should be empty, how can I do it so that the fields keep emptying them?

    
asked by Macx 25.10.2018 в 20:09
source

1 answer

0

The problem is that if you keep it in a variable int it will always be a number. In this case I think it would be best to save it in a variable Nullable , that is, T150 could be worth null or a numeric value. The code would be like this:

int? T150 = dt.Rows[j].IsNull("T150") ? null : new int?((int)dt.Rows[j]["T150"]);

If the value of the DataTable is not an int, for example it is a string with a number, you should do it like this:

int? T150 = dt.Rows[j].IsNull("T150") ? null : new int?(Convert.ToInt32(dt.Rows[j]["T150"]));

Then you could check if T150 has a value with the property "T150.HasValue".

I hope it serves you.

Greetings.

    
answered by 25.10.2018 / 20:20
source