2015年10月12日 星期一

[C#][MVC] 資料寫進DB時,檢查是否有Null

create 是要準被塞進DB裡的物件 當create裡面屬性有 null 時 , 就給空值 , 防止DBNull 錯誤發生
  1. IList properties = typeof(ProductSize).GetProperties().ToList();
  2. foreach (var p in properties)
  3. {
  4. if (p.GetValue(create) == null && p.PropertyType == typeof(string))
  5. {
  6. p.SetValue(create, "");
  7. }
  8. }

沒有留言:

張貼留言