FutuToad
FutuToad

Reputation: 2850

If condition to check for DBNull.Value

I'm trying to check for DBNull.Value but in this case reader["Preferences"] returns {} in the immediate window (why?)

so when (string)reader["Preferences"]; executes I get a type casting error

if (reader["Preferences"] == System.DBNull.Value)
{
     preferences = (string)reader["Preferences"];
}

Upvotes: 0

Views: 908

Answers (1)

Oded
Oded

Reputation: 499002

You are trying to cast only when the value is DBNull.Value.

You should invert your if:

if (reader["Preferences"] != System.DBNull.Value)
{
     preferences = (string)reader["Preferences"];
}

Upvotes: 1

Related Questions