Click here to Skip to main content
11,705,212 members (49,721 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0

In my page i have a date with string format

string Schedule_Date_For_GFC =dr["Schedule_Date_For_GFC"].ToString()

I enter date in any format like 17-6-2012, 17-jun-2012,6-17-2012...

i want to be convert that into 17-jun-2012.

for this i wrote like this

string Schedule_Date_For_GFC =dr["Schedule_Date_For_GFC"].ToString("dd-MMM-yyyy");
but here it shows the error "no overload for method 'tostring' takes '1' arguments".

this is the error message i got. for this i found solution in google first i need to convert that date into datetime format and then use this .

string Schedule_Date_For_GFC =((DateTime)dr["Schedule_Date_For_GFC"]).ToString("dd-MMM-yyyy");

here it's showing the error related to casting....

How to overcome this issue....

Any one suggest me regarding this...
Posted 18-Jan-13 17:47pm
Sergey Alexandrovich Kryukov at 18-Jan-13 23:50pm
You need to show how your "dr" is defined. Also, clearly indicate where is the compilation error, and where is exception.
Sergey Alexandrovich Kryukov at 18-Jan-13 23:59pm
OK, if you don't want to answer my question, why do you expect us to help you?
I asked you to show the definition of "dr".
Sergey Alexandrovich Kryukov at 19-Jan-13 0:18am
Check the run-time type of dr["Schedule_Date_For_GFC"]. I bet it's not DateTime.
Please see Solution 2 and my comment to it.
Sergey Alexandrovich Kryukov at 19-Jan-13 0:25am
I only suspected it. If you knew that (to me, it was not obvious from you question), why did you try to use DateTime formatting? It was pointless.

It was a bad idea to work with string representation of data instead of data itself. You should better change your database schema. However, if this beyond your reach, the solution is quite simple...

Sergey Alexandrovich Kryukov at 19-Jan-13 0:35am
Of course. Hold on, please see my solution. The problem is solved...
PIEBALDconsult at 19-Jan-13 0:31am
You need to know what type the value is before you try casting it.

I fear it's a string -- which it shouldn't be; dates/times should never be stored as strings.
But if it is then it needs to be converted to a Date or DateTime and the earlier the better.
Can it be converted before it's stored?
Can it be converted when it's queried?
If not, you'll have to do it in code -- look into System.DateTime.TryParse and .ParseExact.
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

First of all, try to work with System.DateTime as much as possible, not with strings.

In your situation (please see my last comments to the question), you can use System.DateTime.Parse:
System.DateTime time = System.DateTime.Parse(dr["Schedule_Date_For_GFC"]).ToString());

As the possibility exists that the string was not in a date-time format in first place, you can avoid throwing exception by using System.DateTime.TryParse instead.

And finally, if you know expected format exactly, and parsing by default is not suitable to this format, you should better use System.DateTime.ParseExact or System.DateTime.TryParseExact.

Please see:[^],[^],[^],[^],[^],[^],[^].

That solves the problem.

Good luck,
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


try this

string Schedule_Date_For_GFC =dr["Schedule_Date_For_GFC"]).ToString("dd MMMM yyyy");  
your answer will be look like below:
19 Jan 2013

hope this will help you
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

First the requisite-- Please use an ISO 8601 compliant format YYYY-MM-DD.

Now that that's over... Yes, you must cast the value to its proper type in order to use the override of ToString that DateTime provides.

Or you could use string Schedule_Date_For_GFC = System.String.Format("{0:dd-MMM-yyyy}" , dr["Schedule_Date_For_GFC"] ) ;

Also, both of these could fail if the value isn't a DateTime so you may want to protect against that.
Sergey Alexandrovich Kryukov at 19-Jan-13 0:16am
You are right, but you see, from the last line of OP's code it is obvious that the object dr["Schedule_Date_For_GFC"] fails your assumption for being of the type DateTime, otherwise the type cast would not fail, so it's not "could fail", it's "failed". And this is the problem -- OP needs to have appropriate data in first place.
(I did not vote this time...)
PIEBALDconsult at 19-Jan-13 0:25am
Ah, I guess I read too quickly and assumed too much.
Sergey Alexandrovich Kryukov at 19-Jan-13 0:34am
I dug it out finally, please see...
PIEBALDconsult at 19-Jan-13 0:39am
Yes, seems so. Good work -- 5d.
Sergey Alexandrovich Kryukov at 19-Jan-13 0:46am
Thank you very much. I tried hard :-)
Sergey Alexandrovich Kryukov at 19-Jan-13 0:34am
Finally, after next OP's clarification, I found out what it is. This is not DateTime but string, so some Parse/ParseExact method will solve it. Please see my answer.
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

Try this... Smile | :)

foreach (DataRow dr in dtTotal.Rows)
    string Schedule_Date_For_GFC = dr["Schedule_Date_For_GFC"].ToString();
    DateTime d;
    if (DateTime.TryParse(Schedule_Date_For_GFC, out d))
        Schedule_Date_For_GFC = d.ToString("yy-MMM-yyyy");
        //Some formats will problably be unrecognized by .NET
        //You'll need to figure out a workaround for these formats. :-)

You see this basically parses the current value of your string into a DateTime, which is then used to change your string into the format you want.

Hope this helps...
CraigDJ at 19-Jan-13 0:32am
Can you give an example value of Schedule_Date_For_GFC ?
CraigDJ at 19-Jan-13 0:38am
use code to change the slashes to dashes and .NET will recognize it...
Schedule_Date_For_GFC = Schedule_Date_For_GFC.Replace('/', '-');
PIEBALDconsult at 19-Jan-13 0:41am
This is exactly why not to use strings to store dates. Or, when interchanging, to use ISO 8601.
Rate this: bad
Please Sign up or sign in to vote.

Solution 7

It's very very clear that dr["Schedule_Date_For_GFC"] doesn't contains the valid datetime. Try putting a breakpoint over here and then step through the code to check whether the datetime coming from database is valid or not. If it is valid, then try any one of the solution given for this question over here. I am sure it'll work.

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi Try like this

DateTime Schedule_Date_For_GFC =convert.toDatetime(dr["Schedule_Date_For_GFC"].ToString("dd-MMM-yyyy"));
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Try This, It will help you...
string Schedule_Date_For_GFC = Convert.ToDateTime(dr["Schedule_Date_For_GFC"].ToString()).ToString("dd-MMM-yyyy");
Ronjon1 at 19-Jan-13 0:36am
the value is null or not in date time format please debug and see what is the value.
Ronjon1 at 19-Jan-13 0:40am
Or can you provide codes ??
Because this is working good

string Schedule_Date_For_GFC = Convert.ToDateTime(DateTime.Now).ToString("dd-MMM-yyyy");

I think something is missing in your code.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 284
2 ppolymorphe 126
3 Andy Lanng 125
4 F-ES Sitecore 110
0 OriginalGriff 8,958
1 Sergey Alexandrovich Kryukov 8,276
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606

Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100