然后当读到某个字段是NULL值时候,系统提示错误“制定转换无效”该怎么修改?
源代码如下
C# code
private Yddlinf GetYddlByRow(DataRow row)
{
Yddlinf yddl = new Yddlinf();
yddl.PatientInfoId = (int)row["PatientInfoId"];
yddl.SeatHoursPerday = (int)row["SeatHoursPerday"];
yddl.DislikeSport = (string)row["DislikeSport"];
yddl.FavoriteSportId = (int)row["FavoriteSportId"];
yddl.SportTimeId = (int)row["SportTimeId"];
yddl.SportFrequencyPerWeekId = (int)row["SportFrequencyPerWeekId"];
return yddl;
}
读取代码如下:
C# code
if (IsPostBack == false)
{
Id = Int32.Parse(Request.QueryString["id"]);
YddlMgmt mgmt = new YddlMgmt();
Yddlinf yddl = mgmt.GetYddlById(Id);
SeatHoursPerday.Text = yddl.SeatHoursPerday.ToString();
DislikeSport.Text = yddl.DislikeSport.ToString();
FavoriteSportId.Text = yddl.FavoriteSportId.ToString();
SportTimeId.Text = yddl.SportTimeId.ToString();
SportFrequencyPerWeekId.Text = yddl.SportFrequencyPerWeekId.ToString();
}
几种解决方案:
1. sql解决, isnull(xxx, 0) as xxx
2. C#中判断 为null时如何处理
3. 将字段改为 int? 类型;