Solved

getting error in DateTime

Posted on 2007-03-21
5
449 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now