Solved

getting error in DateTime

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 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