protected void Bind(IQueryable<Fin_StudentLoan> items)
{
gvStuLoan.DataSource = from s in items
select new
{
ID = s.ID,
Number = s.StuNumber,
Name = s.Fin_CanLoanStu.Fin_Poverty.Sms_Student.Name,
Class = s.Fin_CanLoanStu.Fin_Poverty.Sms_Student.Inf_Class1.ClassName,
Major = s.Fin_CanLoanStu.Fin_Poverty.Sms_Student.Inf_Class1.Inf_Major.MajorName,
College = s.Fin_CanLoanStu.Fin_Poverty.Sms_Student.Inf_Class1.Inf_Major.Inf_College.CollegeName,
BankName = s.Fin_BankName.BankName,
BankCardNumber = s.BankCardNumber,
MPhone = s.Fin_CanLoanStu.Fin_Poverty.Sms_Student.MPhone,
AcaYeaName = s.Fin_AcacemicYear.AcaYeaName,
LoanAmount = s.LoanAmount,
ContractNum = s.ContractNum,
StartDate = s.StartDate.ToString() != "" ? Convert.ToDateTime(s.StartDate).ToShortDateString() : null,
ExpireDate = s.ExpireDate.ToString() != "" ? Convert.ToDateTime(s.ExpireDate).ToShortDateString() : null,
//IsRepayed = s.IsRepayed.ToString() != "" ? (bool)s.IsRepayed ? true : false : false,
//IsRepayed = s.IsRepayed.HasValue ? ((bool)s.IsRepayed ? true : false) : false,
IsRepayed = (bool)s.IsRepayed ? "已还清" : "未还清",
//IsRepayed = "4545"
};
gvStuLoan.DataBind();//这个位置抛出异常
}
语句抛出异常如下:
不能将 Null 值赋给类型为 System.Boolean (不可为 null 的值类型)的成员
我把IsRepayed 删除了还报错!以上方法都试过,总是报错!【运行时】
<asp:Label ID="lblIsRepayed" Text='<%#Eval("IsRepayed ")%>' runat="server" ></asp:Label>
错误信息提供不够详细,可能是 (bool)s.IsRepayed 这里出的错
s.IsRepayed 取出来是null值,(bool)s.IsRepayed 强制转换时出错。
楼上正解,你可以给这个字段设置默认值,或从数据库取数据时过滤掉为Null的记录。
IsRepayed被定义成了可空类型,bool? IsRepayed ,bool? 类型的变量时没办法直接赋值给bool型的