admin

Datecolumn:无法将类型为“ System.DateTime”的对象转换为类型为“ System.String”的对象

sql

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

我有以下代码可从数据库中获取值并将它们存储在网格中:

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”的对象

有人知道我该如何解决吗?


阅读 250

收藏
2021-07-01

共1个答案

admin

通过分别获取各列并解析日期列来解决此问题:

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
    };
2021-07-01