Click here to Skip to main content
15,885,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using a textbox in displaying the date picked by the user. When the user didn't fill the textbox it will automatically use the date and time today; but when I tried filling the textbox with date I'm having error on the same of code.

Here's my code:
XML
DateTime dateT;
                if (txtDateRequired.Text != "") 
                {
                    dateT = Convert.ToDateTime(txtDateRequired.Text); 
                }
                else
                {
                    dateT = DateTime.Today; 
                }

I'm having problem with this line,
XML
dateT = Convert.ToDateTime(txtDateRequired.Text);</pre>

All thanks in advance to all your responses.
Posted
Updated 26-Aug-13 6:45am
v3
Comments
Thomas Daniels 26-Aug-13 12:56pm    
Can you show the content of the Text property please?

Reason: text entered in your textbox is not a proper datetime.
Why not used the datetimepicker? you can have set its default to DateTime.Today on load, then you wont need the code you have written. With text boxes, the user might fill some text in your textbox and you will still have the same error.

I saw some people doing something similay to the the below - I dont don't encourage it as the DateTimePicker can handle this properly.

DateTime dateT;
string sError = string.Empty;
C#
if (txtDateRequired.Text != "")
  {
   try
    {
     dateT = Convert.ToDateTime(txtDateRequired.Text);
    }
   catch
   {
    sError += "- Invalid Date Entered\\n";
   }
 }
else
  {
   dateT = DateTime.Today;
  }
 
Share this answer
 
v3
Comments
timJosh 26-Aug-13 13:18pm    
Thanks for your suggestions.
This occurs due to the invalid value of the date time format expected. For example, I have the code below in vb.net.


VB
Dim dateT As DateTime
       ' Sets the CurrentCulture property to U.S. English.
       Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-US")

       ' Creates a CultureInfo for German in Germany.
       Thread.CurrentThread.CurrentUICulture = New CultureInfo("de-DE")


       If Not IsNothing(txtDate.Text) Then
           dateT = Convert.ToDateTime(txtDate.Text)
       Else
           dateT = DateTime.Today
       End If
       MessageBox.Show(String.Format("The current cultrue is : {0} and entered date time is: {1} ", Thread.CurrentThread.CurrentUICulture.ToString(), dateT.ToString()))



This code will be working fine for the value:
8/26/2013 10:0:37 AM
This follows mm/dd/yyyy format.

But, it will throw an error if the value becomes:
26/8/2013 10:0:37 AM
This follows dd/mm/yyyy format.

So, Please check the current culture and pass the value accordingly.
 
Share this answer
 
I like to use the Calendar Class[^] so that my users can just click on the date rather than typing in a date.
 
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