Click here to Skip to main content
15,909,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear sir/ma'am

i am taking date from a text box while i am using webapplication.

but problem i found as
String was not recognized as a valid DateTime.


i am using as
VB
Dim dt1 As DateTime
dt1 = DateTime.ParseExact(Convert.ToDateTime(Me.txt_frmdate.Text), "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)



here txt_frmdate is a text box in which i choose the date from calender extender
Posted

In addition to Solution 1:

  1. Look at other method of the structure System.DateTime: Parse, ParseExact, TryParse and TryParseExact, all of them:
    http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

    The have different signatures and allow to choose either exact format string, culture, or both, the method started with "Try" don't throw exception, but return Boolean value in case of success. Each of the MSDN help pages on each method comes with a clear code sample.
  2. Learn format specifiers:
    http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].


Besides, it's not so good to use text input for time. I would advise to use DateTimePicker instead:
http://www.codeproject.com/KB/webforms/DateTimePicker.aspx[^].

—SA
 
Share this answer
 
Refer - DateTime.ParseExact Method (String, String, IFormatProvider)[^].
Quote:
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

But you have converted the string to date and then passed it inside this ParseExact Function as the first parameter.

So, try like below...
C#
Dim dt1 As DateTime
dt1 = DateTime.ParseExact(Me.txt_frmdate.Text, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Jun-13 0:05am    
It could be enough to solve the problem, but in most cases, specifying both culture and format would be redundant. The answer need a bit more detail. I voted 4 this time.

I added some detail, and, more importantly, a better alternative. Please see Solution 2.

—SA
Yes you are right... My +5 for you, but I can't see my +4. :P
Sergey Alexandrovich Kryukov 3-Jun-13 1:38am    
Thank you, Tadit.
Ah, sorry, I forgot, put it now. Please, don't hesitate to remind; such things happen.
—SA
No problem... Yeah I will remind... :D :P
ankur789 3-Jun-13 0:19am    
sir same problem is also find in the
Dim dt1 As DateTime
dt1 = DateTime.ParseExact(Me.txt_frmdate.Text, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)

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