Avatar of siddhuoops
siddhuoops

asked on 

getting error in DateTime

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.
C#

Avatar of undefined
Last Comment
Webstorm
ASKER CERTIFIED SOLUTION
Avatar of Webstorm
Webstorm

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of siddhuoops
siddhuoops

ASKER

When I run this code, I get this error message. "Cannot convert null to 'System.DateTime' because it is a value type.
Avatar of Webstorm
Webstorm

Replace:
   DateTime dtInput = null;
By:
   DateTime dtInput = (DateTime)null;
Avatar of siddhuoops
siddhuoops

ASKER

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.
Avatar of Webstorm
Webstorm

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

C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo