2015年10月12日 星期一

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

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

沒有留言:

張貼留言