2

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

user2226785
  • 479
  • 8
  • 16

1 Answers1

4

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();
 }
pconnor88
  • 345
  • 3
  • 13
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
  • kindly update your answer without the `?` i don't know how to use it :) – user2226785 Apr 07 '14 at 15:42
  • 2
    +1 to you, but someone answered me at the commetns. thanks a lot – user2226785 Apr 07 '14 at 15:42
  • @user2226785: I answered this on your previous question as well!! updated with if-else – huMpty duMpty Apr 07 '14 at 15:44
  • I will accept your answer after 5 minutes, the guy who answered me allowed me to do that. thanks for your efforts – user2226785 Apr 07 '14 at 15:46
  • The ?: operator is called conditional operator and acts like an if. so humptys statement translates to: string username; if(reader["UserName"] != DBNull.Value) { username = row["UserName"].ToString(); } else { username = string.Empty; } – xeraphim Apr 07 '14 at 15:47