Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
(i'm using SQL server 2012). I changed the date format that in control panel-region-additional setting-date to dd/mm/yyyy but when i login to doctor page (asp.net page) which contains code to fill listbox with today's date on page load it shows this error.


C#
Server Error in '/Clinic' Application.

String was not recognized as a valid DateTime.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[FormatException: String was not recognized as a valid DateTime.]
   System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +13929766
   Doctors_DoctorBoard.Page_Load(Object sender, EventArgs e) +686
   System.Web.UI.Control.OnLoad(EventArgs e) +109
   System.Web.UI.Control.LoadRecursive() +68
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4498


Please can you give me solution to this error ,my regards

What I have tried:

I change different date formats so many times.
Posted
Updated 29-May-16 23:08pm

1 solution

Try DateTime.ParseExact Method (System)[^]
Custom Date and Time Format Strings[^]
e.g.
C#
DateTime result = DateTime.ParseExact("16/11/2016", "dd/MM/yyyy", CultureInfo.InvariantCulture);
 
Share this answer
 
v4
Comments
Abdalazeem Babiker 30-May-16 5:19am    
Sorry, i have got no thing from your post Peter leow.
Peter Leow 30-May-16 5:32am    
Since you have used a custom format, how do you expect the compiler to know which part of the format is day, month, or year? You have to state it explicitly. See example.
The links are supposed to help you discover and learn to formulate the solution yourself.
Abdalazeem Babiker 30-May-16 5:57am    
Thank you a lot, but i have no time, i want fast solution.
CHill60 30-May-16 6:05am    
If you want a fast solution then use the links in @PeterLeow's solution to find out how to change your date format to something explicit that the compiler will understand.
If you wanted a fast solution then including a code snippet would have helped.
In general use dates in format yyyy-MM-dd to avoid such problems (you don't have to display them like that - see the links)

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