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

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 5 Years Ago by Momerath: n/a

That code looks so nice but does not work.

using (_conn = new MySqlConnection(ConnectionString))
            {
                _command = new MySqlCommand(query, _conn);
                _conn.Open();
                _NPC_TEXT.Clear();

                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.