我有一个柱[WelcomeSent]与式SQL表:日期时间和空值存在于该列
我有这样的代码,从我的数据库获取值并将其存储在一个网格:
var result = (from i in dc.vw_Users where i.id == id select i).ToList(); //error line
storeUsers.DataSource = result;
storeUsers.DataBind();
在我的网格有一栏:
<ext:DateColumn runat="server" ID="ColUsersWelcomeSent" DataIndex="WelcomeSent" Text="WelcomeSent" Width="80" Format="dd/MM/yyyy" Hidden="true"></ext:DateColumn>
但是,当我运行它,我收到此错误:
无法投型“的System.DateTime”的对象为类型“System.String”
任何人都知道我该怎么解决呢?
你需要的toString()的日期值转换成LINQ结果对象storeUsers.DataSource =结果之前,分配;
通过分别获取每一列和解析日期列解决它:
var result = from i in dc.vw_Users
where i.id == id
select new
{
i.UserId,
i.id,
i.SiId,
i.FullName,
i.UserName,
i.RoId,
i.Ro,
i.Email,
WelcomeSent =
i.WelcomeSent != null && i.WelcomeSent.ToString().Length > 0
? DateTime.Parse(i.WelcomeSent.ToString())
: new DateTime(),
i.Signed,
i.IsPri,
i.IsApproved,
IsLocked = i.IsLockedOut,
i.LoginStatus,
i.LastLoginDate,
i.SignRights,
i.LastPasswordChangedDate,
i.FailedPasswordAttemptCount,
i.CompleteDate,
i.AccessDate,
i.CertificationDate,
i.CertificateId,
i.progress
};
文章来源: Datecolumn: Unable to cast object of type 'System.DateTime' to type 'System.String'