Click here to Skip to main content
15,888,240 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

In my page i have a date with string format

C#
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

C#
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 .

C#
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
Comments
Sergey Alexandrovich Kryukov 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
[no name] 18-Jan-13 23:56pm    
In all conditions i'm defining the date in string format only and that string information to be stored in datatable based on that datatable i fetch the information by using datarow.

compilation error is on this line "string Schedule_Date_For_GFC =dr["Schedule_Date_For_GFC"].ToString("dd-MMM-yyyy");"

Sergey Alexandrovich Kryukov 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
[no name] 19-Jan-13 0:03am    
foreach (DataRow dr in dtTotal.Rows)
{
string Schedule_Date_For_GFC =dr["Schedule_Date_For_GFC"].ToString()

}
Sergey Alexandrovich Kryukov 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

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:
C#
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
 
Share this answer
 
v2
hey,


try this

SQL
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
 
Share this answer
 
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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 19-Jan-13 0:25am    
Ah, I guess I read too quickly and assumed too much.
Sergey Alexandrovich Kryukov 19-Jan-13 0:34am    
I dug it out finally, please see...
—SA
PIEBALDconsult 19-Jan-13 0:39am    
Yes, seems so. Good work -- 5d.
Sergey Alexandrovich Kryukov 19-Jan-13 0:46am    
Thank you very much. I tried hard :-)
—SA
Try this... :-)

C#
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...
 
Share this answer
 
Comments
[no name] 19-Jan-13 0:30am    
when i try to convert like this it's showing the error
"The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
"
Mitchell J. 19-Jan-13 0:32am    
Can you give an example value of Schedule_Date_For_GFC ?
[no name] 19-Jan-13 0:35am    
1/26/2012
Mitchell J. 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 19-Jan-13 0:41am    
This is exactly why not to use strings to store dates. Or, when interchanging, to use ISO 8601.
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
 
Share this answer
 
Hi Try like this

C#
DateTime Schedule_Date_For_GFC =convert.toDatetime(dr["Schedule_Date_For_GFC"].ToString("dd-MMM-yyyy"));
 
Share this answer
 
Comments
[no name] 19-Jan-13 0:30am    
when i try to convert like this it's showing the error
"The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
"
Try This, It will help you...
string Schedule_Date_For_GFC = Convert.ToDateTime(dr["Schedule_Date_For_GFC"].ToString()).ToString("dd-MMM-yyyy");
 
Share this answer
 
Comments
[no name] 19-Jan-13 0:29am    
when i try to convert like this it's showing the error
"The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
"
Ronjon1 19-Jan-13 0:36am    
the value is null or not in date time format please debug and see what is the value.
Ronjon1 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900