[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

getting error in DateTime

Posted on 2007-03-21
5
Medium Priority
?
458 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

656 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