[CLOSED] Datecolumn: Unable to cast object of type 'System.DateTime' to type 'System.String'

  1. #1

    [CLOSED] Datecolumn: Unable to cast object of type 'System.DateTime' to type 'System.String'

    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?
    Last edited by fabricio.murta; Mar 09, 2017 at 2:04 PM. Reason: [CLOSED]
  2. #2
    Please marked it as closed I found the solution:

    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 });
  3. #3
    Hello @atroul!

    The most likely reason you are having this problem is you are not mapping the model correctly between the database and grid store, and the field is getting to the wrong column. The DateColumn works just fine with DateTime as you can see on most of our GridPanel examples in Examples Explorer. Some use DateColumn as a string value but several others use DateColumn as a DateTime object.

    Here a couple examples:
    - GridPanel from an array with local paging + remote loading
    - Grid Panel cell editor with directMethod

    I hope this helps!
    Fabrício Murta
    Developer & Support Expert
  4. #4
    Well, thanks for the feedback! Guess that was really an issue with the mapping then.
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. Replies: 13
    Last Post: May 18, 2023, 5:29 PM
  2. Replies: 4
    Last Post: May 01, 2014, 7:06 AM
  3. Replies: 7
    Last Post: Oct 15, 2013, 11:35 AM
  4. Replies: 4
    Last Post: Feb 01, 2011, 11:54 AM
  5. Replies: 2
    Last Post: Aug 31, 2009, 6:03 PM

Posting Permissions