Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert Date Time from a User Control to a Date Time variable to send it to the Database and getting the error as " Input String was not in a correct date time format "
Please check the code below,

condObj.DiagnosisDate = Convert.ToDateTime(txtDateTimeDiag.ToString()); where txtDateTimeDiag is my user control.

Any help on conversion is appreciated.

The code is not going beyond the line where I am trying to convert the date.
The date is stored in the database as 2011-12-06 11:56:52.777
Posted
Updated 6-Dec-11 20:44pm
v2
Comments
coded007 7-Dec-11 1:18am    
can you show me database table description to solve it

Also to add to previous solution try using DateTime.TryParse[^] instead. In case the text field contains illegal format you can catch the error more easily.

Also remember to use parameters in the SQL statement where you save the date unless you already do so.
 
Share this answer
 
v2
I see.

You forgot the .Text property of the textbox. Do this:
C#
condObj.DiagnosisDate = Convert.ToDateTime(txtDateTimeDiag.Text.ToString());


Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard
 
Share this answer
 
v2
Comments
gpthakur 7-Dec-11 1:55am    
Dear Eduard, I surely could not miss that but txtDateTimeDiag is the ID of my UserControl. I have built a user control for Date time picker and using a textbox to capture date and then transfer it to the database
In your own UserControl, store the selected Date and Time from the user in a DateTime struct.

When you need to update SQL then call DateTime.ToString("s")[^] method.
The Sortable ("s") Format Specifier will work in a SQL INSERT/UPDATE statement.

A better way is to use SqlParamenter, this way data as DateTime will always be correct.
http://www.dotnetperls.com/sqlparameter[^]
 
Share this answer
 
v2
C#
DateTime.ParseExact(txtDateTimeDiag.Text,"dd/MM/yyyy",null);
This is correct format for datetime try this then tell
 
Share this answer
 
Comments
gpthakur 7-Dec-11 5:07am    
Prashant, txtDateTimeDiag is the ID of the UserControl which I am using in the page to select dates. It does not have a .Text property
You say "I have built a user control for Date time picker and using a textbox to capture date and then transfer it to the database". Can you not get the value as a DateTime from the user control and thus avoid the need for converting back and forth to strings?
 
Share this answer
 
Comments
gpthakur 8-Dec-11 5:06am    
Hi Richard, I already solved the problem myself. Thanks for your comment though. Please have a look at the code below

Convert.ToDateTime(txtDateTimeDiag.CalendarDate.ToString());
Convert.ToDateTime(txtDateTimeDiag.CalendarDate.ToString());
 
Share this answer
 

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