5

Hi I have nulls in a table that I will use to populate a combo box. I am not sure how to do this. When I run the below code I get the error:

Data is Null. This method or property cannot be called on null values.

I need help and I'm new to mysql

the code :

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string constring = "datasource=localhost;port=3306;username=root;password=root";
    string Query = "SELECT * from database.check WHERE patientname IS NOT NULL";
    MySqlConnection conDataBase = new MySqlConnection(constring);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
    MySqlDataReader myReader;

    try
    {
        conDataBase.Open();
        myReader = cmdDataBase.ExecuteReader();

        while (myReader.Read())
        {
            string namethestore = myReader.GetString("namethestore");
            string checkername = myReader.GetString("checkername");
            this.textBox65.Text = namethestore;
            this.textBox66.Text = checkername;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
user3806048
  • 73
  • 1
  • 1
  • 6
  • You could add a check of Sytem.DBNull.Value before you assign the value to the Text property of your textbox, see http://stackoverflow.com/questions/10431835/dbnull-if-statement – VMai Jul 04 '14 at 22:32
  • First, where exactly do you get the exception, and secondly, won't simple null check make the trick? – PiotrWolkowski Jul 04 '14 at 22:37

1 Answers1

9

When one or more of your fields contains a NULL (DBNull.Value) you cannot use GetString on them.
You need to check if they are null using the IsDBNull method and choose what value you want to put in the textbox instead. Usually it is an empty string

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string constring = "datasource=localhost;port=3306;username=root;password=root";
    string Query = "SELECT * from database.check WHERE patientname IS NOT NULL";
    using(MySqlConnection conDataBase = new MySqlConnection(constring))
    using(MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase))
    {
        try
        {
            conDataBase.Open();
            using(MySqlDataReader myReader = cmdDataBase.ExecuteReader())
            {
                int namePos = myReader.GetOrdinal("namethestore");
                int checkerPos = myReader.GetOrdinal("checkername");
                while (myReader.Read())
                {
                    string namethestore = myReader.IsDBNull(namePos) 
                                          ? string.Empty 
                                          : myReader.GetString("namethestore");
                    string checkername = myReader.IsDBNull(checkerPos) 
                                          ? string.Empty
                                          : myReader.GetString("checkername");
                    this.textBox65.Text = namethestore;
                    this.textBox66.Text = checkername;
                }
           }
      }
}

I suggest also to use the using statement around the disposable objects. This will ensure a proper closing and disposing when you don't need them anymore, also in case of exceptions.....

Steve
  • 213,761
  • 22
  • 232
  • 286
  • i tried but i got an error : 'MySql.Data.MySqlClient.MySqlDataReader' does not contain a definition for 'IsBNull' and no extension method 'IsBNull' accepting a first argument of type 'MySql.Data.MySqlClient.MySqlDataReader' could be found (are you missing a using directive or an assembly reference?) – user3806048 Jul 04 '14 at 23:07
  • It was a typo. The method name is IsDBNull. – Steve Jul 04 '14 at 23:16