Answered by:
ExecuteReader how to handle NULL?

Question
-
User-1104215994 posted
Hi,
Is there a practical way to handle NULL and write <g class="gr_ gr_70 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="70" data-gr-id="70">lets</g> say "---" instead of NULL for <g class="gr_ gr_94 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="94" data-gr-id="94">reader</g>[4<g class="gr_ gr_104 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="104" data-gr-id="104">] ?</g>
using (var reader = command.ExecuteReader()) { while (reader.Read()) { if (reader.GetInt32(10) == 0) { status = "Uncertain"; } else if (reader.GetInt32(10) == 1) { status = "Confirm"; } else { status = "Cancel"; } using (StreamWriter outputFile = new StreamWriter(myFullPath, true)) { outputFile.WriteLine( $"{reader[0]};{reader[1]};{reader[2]};{reader[3]};{reader[4]};{reader[5]};{reader[6]};{reader[7]};{reader[8]};{reader[9]}" + ";" +status); } } //No Purchase if (!reader.HasRows) { using (StreamWriter outputFile = new StreamWriter(myFullPath, false)) { } } }
Saturday, September 28, 2019 6:00 PM
Answers
-
User475983607 posted
can you elaborate please?
Sure, click the link in my first post and read. Write code based on the null-coalescing operator and test. If you run into trouble share your code.
{reader[1] as string ?? "---" }
{reader[0] as int? ?? default}
There's also static and extension methods which you'll need to implement if you are trying to convert data types; int -> string. This is basic logic for a developer with your experience, IMHO.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 29, 2019 2:07 PM -
User665608656 posted
Hi cenk,
According to your description, I recommend you can use IsDBNull to determine ExecuteReader whether it is null.
For more details, you could refer to following code:
string res4 = string.Empty; if (!reader.IsDBNull(4)) res4 = reader.GetString(4); else res4 = "---";
outputFile.WriteLine(
$"{reader[0]};{reader[1]};{reader[2]};{reader[3]};{res4};{reader[5]};{reader[6]};{reader[7]};{reader[8]};{reader[9]}" + ";" +status);Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 30, 2019 6:57 AM
All replies
-
User475983607 posted
Use the null-coalescing operator ??; https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
Saturday, September 28, 2019 6:28 PM -
User-1104215994 posted
can you elaborate please?
Saturday, September 28, 2019 7:11 PM -
User475983607 posted
can you elaborate please?
Sure, click the link in my first post and read. Write code based on the null-coalescing operator and test. If you run into trouble share your code.
{reader[1] as string ?? "---" }
{reader[0] as int? ?? default}
There's also static and extension methods which you'll need to implement if you are trying to convert data types; int -> string. This is basic logic for a developer with your experience, IMHO.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 29, 2019 2:07 PM -
User665608656 posted
Hi cenk,
According to your description, I recommend you can use IsDBNull to determine ExecuteReader whether it is null.
For more details, you could refer to following code:
string res4 = string.Empty; if (!reader.IsDBNull(4)) res4 = reader.GetString(4); else res4 = "---";
outputFile.WriteLine(
$"{reader[0]};{reader[1]};{reader[2]};{reader[3]};{res4};{reader[5]};{reader[6]};{reader[7]};{reader[8]};{reader[9]}" + ";" +status);Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 30, 2019 6:57 AM