2014年7月13日 星期日

[RESOLVED] String was not recognized as a valid DateTime.


when executing my code im getting :


String was not recognized as a valid DateTime.


even thought this code is working correctly on another page , below is my code :


DateTime expiration = DateTime.ParseExact(txtEndofTrial.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);


if (txtEndofTrial.Text == string.Empty)
{

string NAFendDate = "End Date Cant be Left Blank !!!";
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + NAFendDate + "');", true);
return;
}

any hint please ???!!!????



Hi,


use this:






       


if (txtEndofTrial.Text
== string.Empty)

       
{


             
string NAFendDate
= "End Date Cant be Left Blank !!!";

           
ClientScript.RegisterStartupScript(this.GetType(),
"Alert",
"alert('" +
NAFendDate +
"');",
true);

           
return;

       
}else{


         DateTime expiration =
DateTime.ParseExact(txtEndofTrial.Text,
"dd/MM/yyyy",
CultureInfo.InvariantCulture);

}



Modify your code to following


try
{
if (txtEndofTrial.Text == string.Empty)
{
string NAFendDate = "End Date Cant be Left Blank !!!";
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + NAFendDate + "');", true);
return;
}

DateTime expiration = DateTime.ParseExact(txtEndofTrial.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
}
catch(Exception ex)
{
lblError.Text = string.Format("{0} is not in the correct format.", txtEndofTrial.Text);
}

Regards.




沒有留言:

張貼留言