?
Solved

Display Date on a GridView

Posted on 2009-05-18
11
Medium Priority
?
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 2000 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
 

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 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