Solved

getting error in DateTime

Posted on 2007-03-21
5
453 Views
Last Modified: 2010-08-05
Hello experts,

                   I have this line of code;
protected void Textbox1_Changed(object sender, EventArgs e)
     {
        string dt = ((TextBox)DetailsView1.FindControl("Textbox1")).Text;
        DateTime dtInput = DateTime.ParseExact(dt, "mmddyy", System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);
        ((TextBox)DetailsView1.FindControl("Textbox1")).Text = dtInput.ToString("mm/dd/yy");
     }

What it does is it allows the user to put the date like 010207 without putting the backslash. Once they leave the textbox, the backslash will be there. Now the problem is if there is a date that the user wants to modify for example 12/05/07 to 12/06/07, then there is an error "System.FormatException: String was not recognized as a valid DateTime. It will only allow them to update if they delete the whole date and enter the new date. But if they just want to change the month, then this error comes up.

Any help please.
0
Comment
Question by:siddhuoops
  • 3
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
Webstorm earned 500 total points
ID: 18764216
Hi siddhuoops,

Firstly, Your method should be in the DetailsView1 form class, so you don't need to find the control, but directly using the Textbox1 member.

Secondly, when you change user input from mmddyy to mm/dd/yy, the next time the method is called you get mm/dd/yy instead of mmddyy
-> you may accept both format


protected void Textbox1_Changed(object sender, EventArgs e)
     {
        try{
           string dt = Textbox1.Text;
           DateTime dtInput = null;
            try{
                dtInput = DateTime.ParseExact(dt, "mmddyy", System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);
            } catch(FormatException)
           {
                dtInput = DateTime.ParseExact(dt, "mm/dd/yy", System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);
           }
           Textbox1.Text = dtInput.ToString("mm/dd/yy");
        } catch(FormatException)
        {
        }
     }
0
 

Author Comment

by:siddhuoops
ID: 18764294
When I run this code, I get this error message. "Cannot convert null to 'System.DateTime' because it is a value type.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 18764363
Replace:
   DateTime dtInput = null;
By:
   DateTime dtInput = (DateTime)null;
0
 

Author Comment

by:siddhuoops
ID: 18764399
This doesn't work either....I tried doing
DateTime dtInput;

And then it works. Is there any reason behind declaring dtInput as null?

Without it, I think its working.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 18765596
I initialized it in order to avoid unitialized compilation error.
But I guess null initialization failed because it's a struct, not a class.

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question