Format date value in item.ToString('MM/dd/yyyy') not working

R

Rich P

I am having a problem running an updaet sql string which contains the
following piece of sql

sql += "dateFld='" + item["dateFld"].ToString() + "' ";

the date is being formatted as Wed 2/1/2009 00:00:00 PDT

I hardcoded a date value like '2/1/2009' and this ran fine. So I am
thinking I need to format the date string in ToString. I tried

item["dateFld"].ToString("MM/dd/yyyy") + "'...

but C# complained. How to format the date here?

Thanks

Rich
 
P

Pavel Minaev

I am having a problem running an updaet sql string which contains the
following piece of sql

sql += "dateFld='" + item["dateFld"].ToString() + "' ";

the date is being formatted as Wed 2/1/2009 00:00:00 PDT

I hardcoded a date value like '2/1/2009' and this ran fine.  So I am
thinking I need to format the date string in ToString.  I tried

item["dateFld"].ToString("MM/dd/yyyy") + "'...

but C# complained.  How to format the date here?

That's because item["dateFld"] has a return type of Object, even if
you actually get a boxed DateTime there. You need to cast the return
value to DateTime first to get access to the latter's ToString()
method which takes a format string,
 
A

Arne Vajhøj

Rich said:
I am having a problem running an updaet sql string which contains the
following piece of sql

sql += "dateFld='" + item["dateFld"].ToString() + "' ";

the date is being formatted as Wed 2/1/2009 00:00:00 PDT

I hardcoded a date value like '2/1/2009' and this ran fine. So I am
thinking I need to format the date string in ToString. I tried

item["dateFld"].ToString("MM/dd/yyyy") + "'...

but C# complained. How to format the date here?

Besides the answer to your question given by other (that
you need to cast/convert to DateTime before you have the
ToString overloads), then you should seriously consider
using parameters in your SQL.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top