0

Hello I have tried to handle Null exception but I'm still receiving the exception warning: data is null this method or property cannot be called on null values.

This is my code:

 spojeni.Open();
        string sql_combobox = "SELECT column FROM tableO RDER BY nazev ASC";
        SqlCommand combobox = new SqlCommand(sql_combobox, spojeni);


        try
        {
            SqlDataReader dr = combobox.ExecuteReader();
            while (dr.Read())
            {
                if (!dr.IsDBNull(0))
                {
                    comboBox1.Items.Add(dr["column"]);
                }
            }

            dr.Close();
            dr.Dispose();
            spojeni.Close();
        }
4

2 回答 2

1

我从你的代码中得到什么

spojeni.Open();
//change your query string there was an error in it
string sql_combobox = "SELECT column FROM table ORDER BY nazev ASC";
SqlCommand combobox = new SqlCommand(sql_combobox, spojeni);


try
{
   SqlDataReader dr = combobox.ExecuteReader();
   while (dr.Read())
   {
       if (!dr.IsDBNull(0))
       {
          comboBox1.Items.Add(dr.GetString(0));
       }
   }

   dr.Close();
   dr.Dispose();
   spojeni.Close();
}
于 2013-08-26T17:09:08.583 回答
0

您需要指定要检查的列...尝试以下操作:

if (dr["column"] != DBNull.Value)
{
    comboBox1.Items.Add(dr["column"]);
}
于 2013-08-26T16:57:52.773 回答