How to check null value in datarow string in c #

I have this code

  foreach (DataRow row in DTgraph.Rows)
            {
                String UserName = row["UserName"].ToString();
                String LoggedState = row["LoggedState"].ToString();
                String InteractionId = row["InteractionId"].ToString();
                String InteractionType = row["InteractionType"].ToString();

            }

how to check if row["something"]null is value?

I tried to run the code, and null values ​​become "" (empty).

I need to check if they are null.

Now I think this is a stupid question, but my problem is what I am doing ToString(), so I thought that null becomes nulleither nullor nullor or empty?

thank

0
source share
1 answer

use DBNull.Value

 var UserName = row["UserName"].ToString();

to

 var UserName =  reader["UserName"] != DBNull.Value ? row["UserName"].ToString():"";

Update

 var UserName = "";
 if(reader["UserName"] != DBNull.Value)
 {
     UserName = row["UserName"].ToString();
 }
+1
source

Source: https://habr.com/ru/post/1535384/


All Articles