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();
In my grid there is a column:
<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:
Unable to cast object of type 'System.DateTime' to type 'System.String'
Anyone know how I can fix that?
你需要在storeUsers.DataSource = result中赋值之前在linq结果对象中转换toString()你的日期值。
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
{
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
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.