I found a small issue in one of my app's.

I'm reading data from a database to a array(so to say)

npc_text.text1[0]   = reader["text0_1"].ToString();

Now I'm having the problem that "text0_1" can be a NULL value. The to string complains about it. If I use a variable it works fine. Is there some other method that I can use?

I know I write a more complex query to change the Null value to

string = "";

But don't like doing that

6 Years
Discussion Span
Last Post by Singlem

Since null isn't anything you can't call ToString() on it. You'll have to check for it:

npc_text.text1[0] = reader["text0_1"] == null ? String.Empty : reader["text0_1"].ToString();

Edited by Momerath: n/a


That code looks so nice but does not work.

using (_conn = new MySqlConnection(ConnectionString))
                _command = new MySqlCommand(query, _conn);

                using (var reader = _command.ExecuteReader())
                    while (reader.Read())
                        NPC_TEXT npc_text = new NPC_TEXT();
                        npc_text.ID         = reader["ID"].ToInt32();
                        npc_text.text1[0]   = reader["text0_1"] == null ? String.Empty : reader["text0_1"].ToString();

Getting a system.nullreferenceexception error

But thanks, think I'll write a method to do the string convertion

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.