Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi,
i have my function BindGrid(), i have attribute dsm in my database i have 2010-01-01 00:00:00.000 and i want to display just 2010-01-01 i have add q.DATE_MS.Value.ToShortDateString() but it dosen't work

pzz help mee

C#
void BindGrid()
       {
           using (QTEL_Entities qtl = new QTEL_Entities())
           {
               var rslt = from q in qtl.STATIONs
                          select new
                          {
                              id=q.ID_STATION,
                              lib=q.LIBELE_STATION,
                              typ=q.TYPE_STATION,
                              dsm=q.DATE_MS.ToString(), // 
                             
                          }
                          ;
               gridSample.DataSource = rslt.ToList();
               gridSample.DataBind();
           }
       }
Posted
Updated 15-Jun-16 21:05pm
Comments
Kornfeld Eliyahu Peter 7-Jul-15 8:53am    
"it dosen't work" - what do you mean by that? Error? Unexpected result?
Suvendu Shekhar Giri 7-Jul-15 9:00am    
Try-
DATE_MS.ToString("yyyy-MM-dd");
Member 11573837 7-Jul-15 9:08am    
hi Kornfeld Eliyahu Peter,
thank you to answering me i have exception (LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this method cannot be translated into a store expression.)
Member 11573837 7-Jul-15 9:11am    
hi Suvendu Shekhar Giri,
you solution not work i have red underling
(no overloading for methde tostring)

Hello,
Try this way
void BindGrid()
       {
           using (QTEL_Entities qtl = new QTEL_Entities())
           {
               var rslt = (from q in qtl.STATIONs
                          select new
                          {
                              id=q.ID_STATION,
                              lib=q.LIBELE_STATION,
                              typ=q.TYPE_STATION,
                              dsm=q.DATE_MS 
                          }).ToList().Select(x => new
                         {
                              id=x.id,
                              lib=x.lib,
                              typ=x.typ,
                              dsm=string.Format("{0:yyyy-MM-dd}", x.dsm), 
                         });
                          
               gridSample.DataSource = rslt.ToList();
               gridSample.DataBind();
           }
       }


thanks
 
Share this answer
 
v3
Comments
Member 11573837 7-Jul-15 11:41am    
hi Animesh Datta,
thank you for answering me i try you solution and i get this error
(LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object)' method, and this method cannot be translated into a store expression.)
Animesh Datta 7-Jul-15 11:51am    
I modified my answer . check
Member 11573837 7-Jul-15 11:58am    
hi Animesh Datta,

plz check my pic plzz

http://www.mediafire.com/view/x6w10dpob92r94v/code2e.png
Animesh Datta 7-Jul-15 12:06pm    
Now check.
Member 11573837 7-Jul-15 12:15pm    
Tahnk you looooooot Animesh Datta :D it works now
You cannot put a method call into the LINQ query that the provider cannot translate into SQL. So, you cannot use .ToString().

The quick'n'dirty solution is to retrieve the objects, convert it to a list so the query executes against the database and then create your view objects from the list, putting the .ToString() call in that code instead.
C#
void BindGrid()
{
    using (QTEL_Entities qtl = new QTEL_Entities())
    {
        // Grab the data from the database.
        var dataItems = qtl.STATIONs.ToList();

        // Create view model objects from the data.
        var rslt = from item in dataItems
                   select new
                   {
                       id=q.ID_STATION,
                       lib=q.LIBELE_STATION,
                       typ=q.TYPE_STATION,
                       dsm=q.DATE_MS.ToString("yyyy-MM-dd")
                   };
               gridSample.DataSource = rslt.ToList();
               gridSample.DataBind();
           }
}
 
Share this answer
 
Comments
Richard Deeming 7-Jul-15 11:14am    
You can avoid creating two copies of the data by replacing the first .ToList() call with .AsEnumerable().
Dave Kreskowiak 7-Jul-15 11:16am    
Forgot about that one. I did say quick'n'dirty, didn't I? :)
Member 11573837 7-Jul-15 11:18am    
hi Dave Kreskowiak,
i have use your solution but it dosen't work for mee i i swear
Dave Kreskowiak 7-Jul-15 11:24am    
"Doesn't work" is not descriptive of the problem. What DOES happen?
Member 11573837 7-Jul-15 11:19am    
hi, Richard Deeming
plz how can i do can you give me example plz
void BindGrid()
       {
           using (QTEL_Entities qtl = new QTEL_Entities())
           {
               var rslt = (from q in qtl.STATIONs
                          select new
                          {
                              id=q.ID_STATION,
                              lib=q.LIBELE_STATION,
                              typ=q.TYPE_STATION,
                              dsm=q.DATE_MS 
                          }).ToList().Select(x => new
                         {
                              id=x.id,
                              lib=x.lib,
                              typ=x.typ,
                              dsm=string.Format("{0:yyyy-MM-dd}", x.dsm), 
                         });
               gridSample.DataSource = rslt.ToList();
               gridSample.DataBind();
           }
       }
 
Share this answer
 
v3
Comments
Member 11573837 7-Jul-15 9:18am    
hi Anil Vaghasiya, i try it and it dosen't work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900