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

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

  • domingo, 06 de mayo de 2012 6:59
     
      Tiene código

    Hi 

    I have a one list in that i need to fetch latest date values. am using below query am getting error Unable to cast object of type 'System.DateTime' to type 'System.String'. on this line 

    Convert.ToDateTime( e.Field<string>("Date"))
    var query = from e in table.AsEnumerable()
                                              group e by e.Field<string>("S.No")                                         
                                             
                                            into groupedTable
                                            select new
                                            {
                                                MailCode = groupedTable.Key,
                                                LabelDate = groupedTable.Max(e => Convert.ToDateTime( e.Field<string>("Date"))),
                                                
                                                Count = groupedTable.Count()
                                            };
    
                               GridView1.DataSource = query;
    
                            GridView1.DataBind();


    Praveen

Todas las respuestas

  • domingo, 06 de mayo de 2012 7:06
    Usuario que responde
     
      Tiene código

    Hello,

    I'l not used to VB.net but I think you chould change 

    LabelDate = groupedTable.Max(e => Convert.ToDateTime( e.Field<string>("Date"))),

    to something like

    LabelDate = groupedTable.Max(e => Convert.ToDateTime( e.Field("Date").ToString())),

    Hope this help!


    Best regards, Christopher.
    Blog | Mail

  • domingo, 06 de mayo de 2012 7:09
     
      Tiene código

    Thanks for your quick reply

    LabelDate = groupedTable.Max(e => Convert.ToDateTime( e.Field("Date").ToString())),

    i used above line its showing error

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


    Praveen

  • domingo, 06 de mayo de 2012 7:20
    Usuario que responde
     
      Tiene código

    how is your date format?

    I've read that there are trouble with ('/') .

    it seems you need to use

    DateTime date;
    String chaine;
    chaine = "2006-11-16 / 12:55";
    chaine = chaine.Replace("/","");
    date = Convert.ToDateTime(chaine);
    



    Best regards, Christopher.
    Blog | Mail

  • domingo, 06 de mayo de 2012 7:41
     
     
    where can i place that code ?

    Praveen

  • domingo, 06 de mayo de 2012 8:07
    Usuario que responde
     
     Respondida Tiene código
    LabelDate = groupedTable.Max(e => Convert.ToDateTime( e.Field("Date").ToString().Replace("/",""))),
    I would say that but I'm not sure of the vb syntax

    Best regards, Christopher.
    Blog | Mail