public static IList<registerinfomodel> GetRegister(string transport_order)
        {
            string sql =string.Format("select * from registerinfo where log_transport_order='{0}'",transport_order);
            List<registerinfomodel> list = new List<registerinfomodel>();
            using (DataTable table = DBHelper.GetDataSet(sql).Tables[0])
            {
                foreach (DataRow row in table.Rows)
                {
                    registerinfomodel register = new registerinfomodel();
                    register.Log_id = (int)row["Log_id"];
                    //register.Log_ship_unit = (string)row["Log_ship_unit"];
                    register.Log_doc_date = (DateTime)row["Log_doc_date"];
                    register.Log_delivery_date = (DateTime)row["Log_delivery_date"];
                    register.Log_customers = (string)row["Log_customers"];
                    register.Log_user = (string)row["Log_user"];
                    register.Log_logistics_order = (string)row["Log_logistics_order"];
                    register.Log_outbound_number = (string)row["Log_outbound_number"];
                    register.Log_state = (string)row["Log_state"];
                    register.Log_insurance = (string)row["Log_insurance"];
                    register.Log_transport_unit = (string)row["Log_transport_unit"];
                    register.Log_transport_order = (string)row["Log_transport_order"];
                    register.Log_cost = (decimal)row["Log_cost"];
                    register.Log_settlement_way = (string)row["Log_settlement_way"];
                    register.Log_ship_people = (string)row["Log_ship_people"];
                    register.Log_expected_arrive = (DateTime)row["Log_expected_arrive"];
                    register.Log_served_time = (DateTime)row["Log_served_time"];
                    register.Log_track_cond_one = (string)row["Log_track_cond_one"];
                    register.Log_track_cond_two = (string)row["Log_track_cond_two"];
                    register.Log_track_cond_three = (string)row["Log_track_cond_three"];
                    register.Log_track_cond_four = (string)row["Log_track_cond_four"];
                    register.Log_track_cond_five = (string)row["Log_track_cond_five"];
                    list.Add(register);
                }
                return list;
            }
        }上面红色语句中提示:无法将类型为“System.DBNull”的对象强制转换为类型“System.String”。 
我知道是什么意思,数据库有空值,可是我不知道在上面方法中如何修改,修改成就算有空值也可以显示,代码主要是在外部控件显示作用。

解决方案 »

  1.   

    本帖最后由 net_lover 于 2012-08-22 14:30:57 编辑
      

  2.   

    Log_track_cond_three数据库中你这个字段是什么类型的?
      

  3.   

    row["Log_track_cond_three"] 内容为null,不能进行(string)null,所以要判断,如果是数据库null就不转换
      

  4.   


    3元运算符  如果不理解 那就这么写先判断该值是否为null或者"" 如果不是再转换
    如果是 你就直接赋值=""就行了
      

  5.   

    Convert.ToString(row["Log_track_cond_three"])  或者使用该方法转换:Convert.ToString
      

  6.   


    Convert.ToString 该方法如果转换的值为NULL 那么就会返回"" 也就是空字符
    这样就不会报错
      

  7.   


    我数据库中是空值,所以不能将空值装换为string
      

  8.   

    数据库里的null值不是对象的null,null值是不可以直接toString的,只有对象才可以
      

  9.   

    row["Log_track_cond_three"].ToString()不可以吗?
      

  10.   

    row["Log_track_cond_three"] 为空,你应该做一下转化
      

  11.   

    row["Log_track_cond_three"] 为空,你应该做一下转化