Reputation: 3469
I have a date column in my database, it's not date time so the values have no time attached.
However, when I drag and drop a SQLDataSource on my page and bind a dropdownlist
to it to display the date, it automatically adds a time of 12:00:00 AM
.
I know why it does this, but I don't know how to get rid of it. Do I need to bind it from codebehind?
Is there an easy fix to this?
Upvotes: 1
Views: 588
Reputation: 517
C# doesn't have date datatype and though values from sql(date) datatype is convert to datetime field in c#. So better convert date as nvarchar in sql using convert(varchar,date ,103) though C# treats it as string.
Upvotes: 0
Reputation: 813
Set DropDownList.DataTextFormatString.
<asp:DropDownList
id="datesList"
runat="server"
dataTextFormatString="{0:dd/MM/yyyy}"/>
Then after DataBinding your control will apply the correct format:
Upvotes: 2
Reputation: 301
Can you show your sql query it may be useful .Have you use convert(varchar,date ,103) method to your query
Upvotes: 1
Reputation: 3412
Add format to field or column definition. For example Gridview:
<asp:boundfield datafield="Your_Date_Column" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />
BTW, other method is to use ViewModel - class which is needed only for displaying data. In this class there is no need to use DateTime or other data types: only string. For example, you can define some displaying defaults here: if value null, show text "UNDEFINED" or what you need. In some cases this approach makes centralized point where you make preparation of data for display.
Upvotes: 1