Datecolumn:无法将类型为“System.DateTime”的对象转换为键入“System.String” [英] Datecolumn: Unable to cast object of type 'System.DateTime' to type 'System.String'

查看:346
本文介绍了Datecolumn:无法将类型为“System.DateTime”的对象转换为键入“System.String”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有[WelcomeSent]的列的SQL表,其类型为:Datetime ,此列中存在空值

I have a sql table with a column [WelcomeSent] with type: Datetime and null values existing in this column

我有这个代码从我的数据库获取值并将它们存储在一个网格中:

I have this code to get the values from my DB and store them in a grid:

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>

但是当我运行它时,我收到此错误:

But when I run it I receive this error:


无法将类型为System.DateTime的对象转换为键入
'System.String'

Unable to cast object of type 'System.DateTime' to type 'System.String'

任何人都知道如何解决这个问题?

Anyone know how I can fix that?

推荐答案

通过单独获取每个列并解析日期列:

Solved it by getting each column separately and parsing Date Column:

var result = (from i in dc.vw_Users where i.id == id select new { UserId = i.UserId, id = i.id, SiId = i.SiId, FullName = i.FullName, UserName = i.UserName, RoId = i.RoId, Ro = i.Ro, Email = i.Email, WelcomeSent = (i.WelcomeSent != null && i.WelcomeSent.ToString().Length > 0 ? DateTime.Parse(i.WelcomeSent.ToString()) : new DateTime()), Signed = i.Signed, IsPri = i.IsPri, IsApproved = i.IsApproved, IsLocked = i.IsLockedOut, LoginStatus = i.LoginStatus, LastLoginDate = i.LastLoginDate, SignRights = i.SignRights, LastPasswordChangedDate = i.LastPasswordChangedDate, FailedPasswordAttemptCount = i.FailedPasswordAttemptCount, CompleteDate = i.CompleteDate, AccessDate = i.AccessDate, CertificationDate = i.CertificationDate, CertificateId = i.CertificateId, progress = i.progress });

这篇关于Datecolumn:无法将类型为“System.DateTime”的对象转换为键入“System.String”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆