Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I am getting an issue while converting an object into DateTime. I am getting the following problem :

The getStartDateOfEvent is my stored procedure.

Business Logic :

Object exist = events.getStartDateOfEvent(EventID, OrganizerID);
DateTime dt1 = (DateTime)exist;

The value i get while debugging in exist is : 7/9/2012 12:34:45 PM
and in dt1 i get the value 1/1/0001 12:00:00 AM

So what will be the exact solution. I have tried in many ways but not getting any solution.
Posted

The way to do it would be

C#
string MyString = exist.tostring();
DateTime dt1 = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt",
                                  null);


you may also find this useful

Easy String to DateTime, DateTime to String and Formatting[^]
 
Share this answer
 
v2
Comments
rp786 6-Jul-12 1:28am    
Thanks a lot.....It worked.
Rahul Rajat Singh 6-Jul-12 1:30am    
You are most welcome. always ready and glad to help a fellow developer.
rp786 6-Jul-12 1:30am    
:)
If the object recieved was of datatype datetime internally then it will be converted into Datetime by explicit assignment.

Looks like, internally it is not of datetime type. You need to convert it explicitly.
Here:
C#
Object exist = events.getStartDateOfEvent(EventID, OrganizerID);
DateTime dt1 = Convert.ToDateTime(exist);
 
Share this answer
 
Comments
rp786 6-Jul-12 1:30am    
I have tried this also but the same issue is coming.
Sandeep Mewara 6-Jul-12 1:40am    
Then it means, your object returned was null and not a proper date. You should have checked for that easily.

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