C# 数据为空。无法对空值调用此方法或属性。(使用组合框)

C# 数据为空。无法对空值调用此方法或属性。(使用组合框),c#,mysql,box,C#,Mysql,Box,嗨,我在一个表中有空值,我将用它来填充一个组合框。我不知道该怎么做。当我运行下面的代码时,我得到了错误: 数据为空。无法对空值调用此方法或属性。 我需要帮助,我是mysql新手 守则: private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string constring = "datasource=localhost;port=3306;username=root;password=root"

嗨,我在一个表中有空值,我将用它来填充一个组合框。我不知道该怎么做。当我运行下面的代码时,我得到了错误:

数据为空。无法对空值调用此方法或属性。 我需要帮助,我是mysql新手

守则:

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

当一个或多个字段包含NULL(DBNull.Value)时,不能对其使用
GetString

您需要使用IsDBNull方法检查它们是否为null,并选择要在文本框中输入的值。通常它是一个空字符串

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;
                }
           }
      }
}

我还建议在一次性物品周围使用。这将确保在您不再需要它们时,以及在出现异常的情况下,正确地关闭和处理它们。…

在将值分配给textbox的Text属性之前,您可以添加一个Sytem.DBNull.Value复选框,请参见第一条,从何处获得异常,第二条,简单的null检查不会奏效吗?我试过了,但出现了一个错误:“MySql.Data.MySqlClient.MySqlDataReader”不包含“IsBNull”的定义,并且找不到接受第一个参数类型为“MySql.Data.MySqlClient.MySqlDataReader”的扩展方法“IsBNull”(是否缺少using指令或程序集引用?)那是个打字错误。方法名为IsDBNull。