Solved

Display Date on a GridView

Posted on 2009-05-18
11
347 Views
Last Modified: 2012-05-07
Hello,

I have a gridview that display records from a store procedure. One of the fields is Date.
I need to display the date as: mm/dd/yyyy right now it displays like this:
1/22/2009 12:00:00 AM
..
The date is coming from the stored procedure. How can I select the date as mm/dd/yyyy?
I have this:
Select


e.EvaluationVisitID as [EvaluationVisitID], e.DateEvaluation as [DateEvaluation]
from tbl_EvaluationVisit e
where e.participantID=@participantID
0
Comment
Question by:Romacali
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24415955
select CONVERT( varchar(10),EvaluationVisitID ,101)
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24415964
I would URGE you to leave your formatting to the application, and not do it in the select.  Doing it in your select statement will change it from a DATETIME to a VARCHAR.

If you must...



Select e.EvaluationVisitID as [EvaluationVisitID], convert(varchar(10), e.DateEvaluation,101) as [DateEvaluation]

from tbl_EvaluationVisit e

where e.participantID=@participantID

Open in new window

0
 

Author Comment

by:Romacali
ID: 24415981
some of the dates got the year as 1900
10      01/01/1900                          
11      05/05/2009                          
12      06/27/2009                                 
16      01/01/1900                          
17      01/01/1900                          
18      01/01/1900                           
19      01/01/1900                          
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24416005
those dates, u enter as '' , so sql represent them as the default date.
you can either update them as NULL  or filter them in you select query adding a where condition

where DateEvaluation  > '1901-01-01'
0
 
LVL 4

Accepted Solution

by:
dublingills earned 500 total points
ID: 24416078
I take it your gridview is an ASP.NET gridview control.

If so, you need to set the DataFormatString property of the column in the gridview to {0:"mm/dd/yyyy"}.  If the gridview is databound and you are not explicitly defining boundfields in your markup you need to set this in code.

Take a look at this for more:
http://msdn.microsoft.com/en-us/library/aa479342.aspx
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Romacali
ID: 24416146
I got this error:
 Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Literal content ('<asp:BoundField  DataField="DateEvaluation" HeaderText="Date of Evaluation" DataFormatString="{0:"mm/dd/yyyy"} />') is not allowed within a 'System.Web.UI.WebControls.DataControlFieldCollection'.
0
 

Author Closing Comment

by:Romacali
ID: 31582759
Thanks!
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24416224
Romacali:

I have to disagree with your allocation of points here.  

aneeshattingal provided you with what you requested.  "How can I select the date as mm/dd/yyyy?"
I provided you with the same statement, wrapped inside of your select, after explaining to you the better way to do it.
dublingills provided the C# solution that I proposed.  Which is correct.

But I think that a 3-way split is in order because we all three helped you.
0
 
LVL 4

Expert Comment

by:dublingills
ID: 24416246
You need to remove the quotation marks from the DataFormatString:

<asp:BoundField  DataField="DateEvaluation" HeaderText="Date of Evaluation" DataFormatString="{0:mm/dd/yyyy} />

My fault, should have made it clearer...

0
 
LVL 4

Expert Comment

by:dublingills
ID: 24416410
Guys,

I think the issue here is one of interpretation; on the one hand Romacali's question clearly states:

'I need to display the date as: mm/dd/yyyy'

Which, to me defines the requirement.  However another part of the question is:

'How can I select the date as mm/dd/yyyy?'

Which has a different solution.

The bottom line here is that the required solution is simply a change of display format.  A change of data type in the datasource potentally introduces other issues but I'm not going to discuss the pros and cons of each approach.

I do this to help others and the time I get to spend on it is limited so I'm not big on the points thing.  If you feel your solution has been unfairly ignored please refer this to a moderator as I can't, and would happily reallocate points.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24416477
I couldn't agree more dublingills that you provided the proper solution.  That's what I was referring them to in my post.  I do feel it is important though to acknowledge the other two experts, myself being one of them.  But not enough to the point where I want to get a MOD involved.  I don't typically do the "request attention" myself.  Rather, I just ask that the asker review my comments and make their own decision.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now