我有這樣一個資料庫物體
classs a
[Display(Name = "姓名")]
public string name { get; set; }
[Display(Name = "工號")]
public string id_card { get; set; }
[Required(ErrorMessage = "請輸入資產編號")]
[Display(Name = "資產編號")]
public string pc_code { get; set; }
[Display(Name = "創建時間")]
public DateTime? create_time { get; set; }
加了[Required(ErrorMessage = "請輸入資產編號")] 這一行
那庫里面讀出來的資料,里面如果這個欄位有空的就會報錯
我現在的需求就是,新寫進去的,這個欄位不能為空
但之前庫里面有空的,可以正常讀出來
An unhandled exception occurred while processing the request.
SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
uj5u.com熱心網友回復:
兩條路要么用update陳述句把資料庫里面的Null值改成空字串,例如 update TableName set ColName='' where ColName is null
要么從資料庫讀取的時候判斷下是不是system.dbnull.value,是的話用空字串替代
uj5u.com熱心網友回復:
"庫里面讀出來的資料"是怎么寫的?從 ado.net 讀取資料庫值需要判斷 DBNull,例如
if (value == DBNull.Value)
xd = string.Empty;
else
xd = (string)res;
uj5u.com熱心網友回復:
你“新寫進去的”怎么樣應該自己測驗,你從資料庫里邊讀出來的就必須處理 DBNull。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/237831.html
標籤:ASP.NET
