Datagrid date column format

Hi,

I am using VB and ASP.Net and have a stored procedure that returns either a null or a date.  I have tried formatting the date as {0:yyyy-MM-dd h:mm tt} but when there is no date returned (as happens in some rows), it defaults to 1900-01-01....

How do I put conditional logic on this?  I am currently populating the grid as follows:
            DataGrid1.DataSource = sqlcmd.ExecuteReader
            DataGrid1.DataBind()

Appreciate the help.

DP
porter416Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmarinovCommented:
Hi porter416,

i suggest you to work in this case with ItemDataBound event
there you can determine if you will output a date or not depending on the value for the current record

here is an example ( it is based on datalist, BUT it is absolutely the same in your case with datagrid - just change
DataListItemEventArgs
to
DataGridItemEventArgs )
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatalistclassitemdataboundtopic.asp

Regards!
B..M
mmarinov
0
RejojohnyCommented:
try this ... u could use the isnull for the date field in the SQL statement that is used to retrieve the values from the database ..
select isnull(mydatefield, ' ' ) as mydatefield from mytable
this will ensure that an emplty value is returned instead of a null .. just check how the formatting behaves in this case ..


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
porter416Author Commented:
Both were great answers and the second 1 was simpler and ultimately the 1 I used.

Thank you both.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.