user2226785
user2226785

Reputation: 489

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

Upvotes: 2

Views: 10209

Answers (1)

huMpty duMpty
huMpty duMpty

Reputation: 14460

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

Upvotes: 4

Related Questions