Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
Hi,
 
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 18:47pm
Comments
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.
—SA
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".
—SA
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.
—SA
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...
 
—SA
Sergey Alexandrovich Kryukov at 19-Jan-13 0:35am
   
Of course. Hold on, please see my solution. The problem is solved...
—SA
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
good
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:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx[^],
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx[^],
http://msdn.microsoft.com/en-us/library/332de853.aspx[^],
http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx[^],
http://msdn.microsoft.com/en-us/library/ms131044.aspx[^],
http://msdn.microsoft.com/en-us/library/h9b85w22.aspx[^].
 
That solves the problem.
 
Good luck,
—SA
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

hey,
 

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
  Permalink  
Rate this: bad
good
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.
  Permalink  
Comments
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...)
—SA
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...
—SA
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 :-)
—SA
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.
—SA
Rate this: bad
good
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");
    }
    else
    {
        //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...
  Permalink  
Comments
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
good
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.
 

--Amit
  Permalink  
Rate this: bad
good
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"));
  Permalink  
Rate this: bad
good
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");
  Permalink  
Comments
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");
MessageBox.Show(Schedule_Date_For_GFC);
 
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 OriginalGriff 400
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 95
4 Maciej Los 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2014
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