Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I Convert Windows forms Textbox to Datetime, I am Getting this(String was not recognized as a valid DateTime.) error please solve thanks in advance...happy coding
Posted
Updated 24-Mar-15 21:52pm
v3
Comments
Sinisa Hajnal 25-Mar-15 3:52am    
Why didn't you search for the solution instead of asking? Use DateTime.TryParse method...my vote of 1.
[no name] 25-Mar-15 3:56am    
this method giving no evrload for method tryparse, please suggest working example i tried but facing bit of problem.....thanksss

my code is
objbel.DtEntry = DateTime.TryParse(txtEntrydate.Text.ToString());
objbel.Dtfrom = DateTime.TryParse(txtDateFrom.Text));
ArunRajendra 25-Mar-15 4:06am    
You will get the error becasue dd/mm/yyyy and mm/dd/yyyy. I guess there is a option to specify the format.
[no name] 25-Mar-15 4:41am    
Try this..
objbel.DtEntry = DateTime.ParseExact(txtEntrydate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
[no name] 25-Mar-15 5:50am    
Sql Exception caught date range exception..
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

First off, don't use a textbox for Date / Time entry unless you have no alternative: they are a PITA as the user can enter what he wants, in whatever format he wants, and it may not be correct. For WinForms, use a DateTime picker instead - it's more friendly for the user, and it can't generate a "bad" DateTime value.

But if you must, then look at DateTime.TryParse[^] which lets you check (and convert if possible) a string to a DateTime value.

Once you have the DateTime, you can pass that directly to SQL via a parametrized query.
 
Share this answer
 
Try using DateTime.TryParse function.

Here are some examples -
DateTime.TryParse Method (String, DateTime)[^]
TryParse examples[^]
 
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