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

Recommended Answers

All 3 Replies

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();

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

Sorry about that stupid class bug:( and not reader

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.