Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

i have a datetime field in sql server table - PAYMENTDATE.

i select this using a stored procedure and populate a dropdownlist in ASP.NET with the values that are returned.

 

this is the code that i use:

 

if(ds.Tables[0].Rows.Count!=0)

{

DropDownList1.DataSource = ds;

DropDownList1.DataMember = "CLAIMSPERD_EMF";

DropDownList1.DataTextField = "ACTIVE_PERIOD";

DropDownList1.DataBind();

}

 

this all works fine but the date appears in the dropdownlist with as a date and time field (the time is 00:00:00) so it might look something like this:

2001-09-30 00:00:00.000

 

i don't store the time initially in the field and i don't need it either.

 

is there a way to remove this 00:00:00 from the date in the dropdownlist.

 

in the table in sqlserver the date is stored as 2001-09-30 when i return all rows but if i use query analyser and run a query the date comes back ike 2001-09-30 00:00:00.000

 

any help would be much appreciated!

 

thanks!

Posted

DataTextFormatString will do the job.

 

{0:[YOUR FORMAT HERE]}

 

0 means the first element... since we have only one... 0 is the only correct value.

Format string...

 

D = Decimal

C = Currency

X = Hexadecimal... etc... etc...

 

Custom Date formating is much more interessting for us.

 

DAYS

d = 1..31

dd = 01 .. 31

ddd = mon. fri. sun.

dddd = day of the month (monday... friday...)

 

MONTH

M = 1..12

MM = 01..12

MMM = Jan. Feb. ........

MMMM = January , February

 

YEARS

y = 1 (2001) 2 (2002) ........

yy = 01 (2001) 02 (2002) ........

yyyy = 2001, 2002, 2003, 2004

 

TIME

m = minutes

h = hours

f = fraction of seconds

 

For the separator... you just have to add some "/" or "-" or "_" or any character you want... as long as it's not a formating character.

 

All formating character can be found here :

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcustomdatetimeformatstrings.asp

 

Have fun !

 

SOME EXEMPLE :

 

{0:yy-MM-dd} ==> 04-07-14

{0:yyyy MMMM dddd} ==> 2004 July Wednesday

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted

Correction bri189a ...

 

It will work if you go on with a single variable. But it's a binded DropDownList. You can't do that unless you hack the rendering of your control. But I must admit it must work if someone can't understand formatting expression :p

 

ToString("yy-MM-dd") will work also... :p

 

Time to change the tire... I'm on fire...

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted

thanks!

i used the DataTextFormatString like so:

DropDownList1.DataTextFormatString ="{0:d}";

this gives me the date in the format dd/mm/yyyy

Posted
Correction bri189a ...

 

It will work if you go on with a single variable. But it's a binded DropDownList. You can't do that unless you hack the rendering of your control. But I must admit it must work if someone can't understand formatting expression :p

 

ToString("yy-MM-dd") will work also... :p

 

Time to change the tire... I'm on fire...

That's true; didn't read fully and I'm use to custom generated strongly typed datasets that allow me to do things like what I said... my fault.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...