1

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?

1
  • I think you can still a format to it in the generated code Commented Jul 19, 2013 at 7:18

4 Answers 4

2

Set DropDownList.DataTextFormatString.

<asp:DropDownList
id="datesList"
runat="server"
dataTextFormatString="{0:dd/MM/yyyy}"/>

Then after DataBinding your control will apply the correct format:

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

Can you show your sql query it may be useful .Have you use convert(varchar,date ,103) method to your query

1 Comment

there is no time in the database so I haven't used convert in my sql statement, it's a .net runtime issue.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.