none
Data is null

    Question

  • My boss has a C# app which retrieves and inserts data to/from a MySQL database.  The app has been running fine for months but recently it started throwing an exception "Data is Null. This method or property cannot be called on Null values."  He asked me to fix it.  The problem is that I don't know C#.

    Here is the code:

                MySqlDataReader reader = null;

                MySqlCommand cmd = new MySqlCommand("<there is a MySQL query here...>", conn);

                try
                {
                    reader = cmd.ExecuteReader();
                    empList.Items.Clear();

                    while (reader.Read())
                    {
                        empList.Items.Add(reader.GetString(0)); // Line where the debugger stops. "Data is NULL."
                    }
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show("Failed to populate database list: " + ex.Message);
                }
                finally
                {
                    if (reader != null) reader.Close();
                }

    There is a PHP version that does the same thing that this app does, and it still works.  So there were no changes to the database.  So I really have no idea why this app spontainiously stopped working.

    What would cause "reader" to remain NULL?

    Thanks
    Monday, March 10, 2008 11:57 PM

Answers

  • Well the reader isnt null or it wouldnt have gone into the while statement.  Its more likely that the item contain empty data you could put in a check like this:

     

    Code Snippet

    while (reader.Read())
    {

    if (!reader.IsDBNull(0))

    {
         empList.Items.Add(reader.GetString(0)); // Line where the debugger stops. "Data is NULL."

    }

    }

     

     

     

    You might want to run the query manually and look at the data for some oddities.  My guess is that there is a null value in the first field though somewhere.  The Php code can probably handle it.  This code should take care of that.

    Tuesday, March 11, 2008 12:59 AM

All replies

  • Try replacing this:

     

    Code Snippet
    empList.Items.Add(reader.GetString(0));

     

     

     

    with this:

     

    Code Snippet

    string result = reader.GetString(0);

    if (result == null || result == DBNull)

    empList.Items.Add("Empty");

    else

    empList.Items.Add(result);

     

     

    Hope this helps

    Tuesday, March 11, 2008 12:55 AM
  • It appears that you have a null value in that field somewhere in the table. Make the field not nullable, or check for null before you call GetString(0);

     

    Code Snippet

    if (!reader.IsDBNull(0))

    {

    empList.Items.Add(reader.GetString(0));

    }

     

     

     

    Tuesday, March 11, 2008 12:56 AM
  • Well the reader isnt null or it wouldnt have gone into the while statement.  Its more likely that the item contain empty data you could put in a check like this:

     

    Code Snippet

    while (reader.Read())
    {

    if (!reader.IsDBNull(0))

    {
         empList.Items.Add(reader.GetString(0)); // Line where the debugger stops. "Data is NULL."

    }

    }

     

     

     

    You might want to run the query manually and look at the data for some oddities.  My guess is that there is a null value in the first field though somewhere.  The Php code can probably handle it.  This code should take care of that.

    Tuesday, March 11, 2008 12:59 AM
  • Awesome... thanks for the help.  I guess there were just a couple of null fields in the database.  It's up and running now.

    Thanks again.
    Tuesday, March 11, 2008 1:31 AM
  • Could mark these posts that helped you as helpful or answered so we can close this issue up?  Thanks!

    Tuesday, March 11, 2008 1:36 AM
  • Thanks ur short and sweet answer is very helpful for me....
    Monday, March 24, 2008 11:28 AM