Solved

getting error in DateTime

Posted on 2007-03-21
5
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Amazon S3 .Net error 5 78
need to convert function to c# 5 54
c# ftp code 3 56
How do I "share" on social sites? 2 37
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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