I have this expression
while (rdr.Read())
{
decimal[] yValues =
{
decimal.Parse(rdr["Result1"].ToString()),
decimal.Parse(rdr["Result2"].ToString()),
decimal.Parse(rdr["Result3"].ToString()),
decimal.Parse(rdr["Result4"].ToString()),
decimal.Parse(rdr["Result5"].ToString()),
decimal.Parse(rdr["Result6"].ToString()),
decimal.Parse(rdr["Result7"].ToString()),
decimal.Parse(rdr["Result8"].ToString()),
decimal.Parse(rdr["Result9"].ToString()),
decimal.Parse(rdr["Result10"].ToString()),
decimal.Parse(rdr["Result11"].ToString()),
decimal.Parse(rdr["Result12"].ToString())
};
which is working well if all Result are not null. If any of it is null I have error Input string was not in a correct format. How do I handle null values so there is no error?

Amit MohantyPosted Oct 23, 2024, 9:15 AM
The List type does not allow null values because decimal is a value type, and value types cannot be null. To allow null values, you need to use a nullable type.
Manoj KallaPosted Oct 24, 2024, 5:48 AM
Hi Varius,
Basic engineering rule is start from basic if you got any problem or issue.
Here also we have to start from basic that is always use Convert.ToString, Convert.Todecimal this will handle the null value by default.
My Suggetion don't use direct Data reader value, create model,DTO or class and transfer data the required data. This will help you not only use the data but also for further process.
Regards,
Marius VasilePosted Oct 23, 2024, 9:21 AM
Thank you Amit, that has done it!
Marius VasilePosted Oct 23, 2024, 8:59 AM
So the question is how do I add null in the list
I tried with adding else condition but doesn't allow null
Marius VasilePosted Oct 23, 2024, 6:10 AM
The reason I want to ignore null values is because I use yValues in a Chart. If I assign 0 to null then the Chart looks like
What I want is for the line to stop where the value is not null i.e August in my chart
Marius VasilePosted Oct 23, 2024, 6:04 AM
Thank you Amit, I tried first solution proposed by you and I have the following error
Enumeration has either not started or has already finished.Brahma Prakash ShuklaPosted Oct 23, 2024, 5:22 AM
To handle
nullvalues safely, you can usedecimal.TryParseinstead ofdecimal.Parseto avoid errors if any of theResultvalues arenullor not in a correct format. Here's how you can modify your code:Amit MohantyPosted Oct 23, 2024, 5:18 AM
If you'd like to ignore null values entirely and not add them to the array, you can dynamically build a list of values and only include those that are not null. For example:
If a value is null and you want to provide a default value (e.g., 0):
Marius VasilePosted Oct 22, 2024, 6:29 PM
Well, I will have null values for sure but I want them handled. I tried with
and it makes value equal to 0. I rather want to ignore this value, to not be visible in the list if possible not to make it 0
Ayush GuptaPosted Oct 22, 2024, 6:26 PM
Hi Marius,
Better to check null first before parse.