I have added data from the database to the textbox but can't add a null value. I'd like to enter certain text if the field (not sure if thats what it's called) in the database is null.

if( (reader["Grey3"].ToString()) == DBNull.Value)

I've search online and read through a number of discussions but couldn't understand them too well.

Try it this way:

while (reader.Read())
     textBox1.Text = reader["Grey3"] != (object)DBNull.Value ? (string)reader["Grey3"] : String.Empty;

It didn't work, got the error "Invalid attempt to read when no data is present."

I modified the statement a tad also.

textBox1.Text = reader["Grey3"] != (object)DBNull.Value ? (string)reader["Grey3"] : String.Empty;

Would that change the results of what you wrote?

Tried using your way and it doesn't work as expected. If the column isn't null then I'll get a debugging error. If it is null then I am unable to change the text of the text box after substituting


with "applejacks" . I would like the text box to have text in it even if the column is null.

Edited 5 Years Ago by MagnetoM: n/a

I was wrong, it worked as you wrote it. I was testing for a database entry that wasn't there because I had recreated my database and forgot to add it. Thanks alot.

This question has already been answered. Start a new discussion instead.