简体   繁体   中英

how to check for null in datarow row 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 the row["something"] is null?

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

I need to check if these is null.

I now that this is an stupid question, but my problem is that I am making ToString() so i thought that null becomes null or NULL or Null or empty?

thanks

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();
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM