asp.net方法问题
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”。
我知道是什么意思,数据库有空值,可是我不知道在上面方法中如何修改,修改成就算有空值也可以显示,代码主要是在外部控件显示作用。 --------------------编程问答-------------------- --------------------编程问答--------------------
这句是什么意思,如果写了这句,我页面会出现数值吗
--------------------编程问答-------------------- Log_track_cond_three数据库中你这个字段是什么类型的? --------------------编程问答-------------------- row["Log_track_cond_three"] 内容为null,不能进行(string)null,所以要判断,如果是数据库null就不转换 --------------------编程问答--------------------
3元运算符
如果不理解 那就这么写
先判断该值是否为null或者"" 如果不是再转换
如果是 你就直接赋值=""就行了 --------------------编程问答-------------------- 我页面会出现数值吗
数据库里面就没有值,你想出现什么值? --------------------编程问答-------------------- Convert.ToString(row["Log_track_cond_three"]) 或者使用该方法转换:Convert.ToString --------------------编程问答--------------------
Convert.ToString 该方法如果转换的值为NULL 那么就会返回"" 也就是空字符
这样就不会报错 --------------------编程问答-------------------- 1L正解!
数据库有值就会显示值,没值也不报错。 --------------------编程问答--------------------
这方法很方便,不了解的话就去看下三元运算符 --------------------编程问答--------------------
我数据库中是空值,所以不能将空值装换为string --------------------编程问答-------------------- 数据库里的null值不是对象的null,null值是不可以直接toString的,只有对象才可以 --------------------编程问答-------------------- row["Log_track_cond_three"] --------------------编程问答-------------------- row["Log_track_cond_three"].ToString()不可以吗? --------------------编程问答-------------------- row["Log_track_cond_three"] 为空,你应该做一下转化 --------------------编程问答-------------------- row["Log_track_cond_three"] 为空,你应该做一下转化
补充:.NET技术 , ASP.NET