c# - 数据为空。不能对空值调用此方法或属性。(使用组合框)

标签 c# mysql box

您好,我将用于填充组合框的表中有空值。我不知道该怎么做。当我运行以下代码时出现错误:

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

我需要帮助,我是 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;
                }
           }
      }
}

我还建议使用 using statement一次性元素周围。这将确保在您不再需要它们时正确关闭和处置它们,在出现异常情况时也是如此......

关于c# - 数据为空。不能对空值调用此方法或属性。(使用组合框),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24581305/

相关文章:

c# - 如何在 Visual Studio 2013 中打开 SSRS (.rptproj) 文件?

c# - Azure DefaultAzureCredential 正在尝试在开发环境中从 169.254.169.254 获取 oauth token

c# - 向数据库中添加sql语句

c# - 如何为我的 JWT 创建签名?

c# - 来自数据库的数据未返回,但正常列表是

mysql - MySql 中的考勤报表

mysql - mysql中max函数的问题

php - 在使用准备好的语句执行插入查询后获取自动增量 ID

coldfusion - 通过 Box API 2.0 下载文件给出 200 作为响应而不是 302 找到

c - 通过三点A、B、C的坐标定义一个盒子