1

I am reading data from MySQL in C#. If the reader["A"].ToString is a NULL value in the MySQL table, I want the following command: int A = 0. Else (if reader["A"].ToString is not NULL), int A = 1.

How can I do this? I tried if(Convert.IsDbNull(reader["A"])) but my C# Express isn't recognising IsDbNULL.

(I am able to get normal Varchar or Int data from the table)

drspuv
  • 7
  • 2
  • 4
    It is `Convert.IsDBNull`, with an upper case `B` – Habib Jun 01 '15 at 15:15
  • 1
    do it in the query directly? `select coalesce(field_with_null`, 0)` – Marc B Jun 01 '15 at 15:15
  • @Habib, thank you! Please send your comment as an answer so I can accept it. Sorry for the inattention, I found the wrong code here: http://stackoverflow.com/questions/4739641/how-to-check-for-null-in-mysqldatareader-by-the-columns-name – drspuv Jun 01 '15 at 15:21
  • @drspuv, thanks, but this is a typo, and there is a close reason for that, I have already voted to close this question. – Habib Jun 01 '15 at 15:24
  • side note -> use the ternary operator here. `int A = Convert.IsDBNull(reader["A"]) ? 0 : 1` – Jonesopolis Jun 01 '15 at 15:36
  • Thank you @Jonesy, we learn something every day! – drspuv Jun 01 '15 at 15:40

1 Answers1

0

As @Habib corrected the typo, there is the code for the question if anybody would need it:

if (Convert.IsDBNull(reader[A])) A = 0;
else A = 1;

Thank you @Jonesy for upgrading the code:

int A = Convert.IsDBNull(reader["A"]) ? 0 : 1
drspuv
  • 7
  • 2