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”。
我知道是什么意思,数据库有空值,可是我不知道在上面方法中如何修改,修改成就算有空值也可以显示,代码主要是在外部控件显示作用。
{
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”。
我知道是什么意思,数据库有空值,可是我不知道在上面方法中如何修改,修改成就算有空值也可以显示,代码主要是在外部控件显示作用。
3元运算符 如果不理解 那就这么写先判断该值是否为null或者"" 如果不是再转换
如果是 你就直接赋值=""就行了
Convert.ToString 该方法如果转换的值为NULL 那么就会返回"" 也就是空字符
这样就不会报错
我数据库中是空值,所以不能将空值装换为string