I work on asp.net mvc application . i get error when publish application over iis but on local machine not get any error
i search for that i found that error on datetime on the line below
if (Session[SessionKeys.VisaExpiryDate] == null)
{
visaExpiryDate = 0;
}
else
{
DateTime visDateExpire = Convert.ToDateTime(Session[SessionKeys.VisaExpiryDate]);
visaExpiryDate = JulianDate.DateTimeToJulian(visDateExpire);
}
for details as below
if (Session[SessionKeys.VisaExpiryDate] == null)
{
visaExpiryDate = 0;
}
else
{
DateTime visDateExpire = Convert.ToDateTime(Session[SessionKeys.VisaExpiryDate]);
visaExpiryDate = JulianDate.DateTimeToJulian(visDateExpire);
}
public static int DateTimeToJulian(DateTime dateTime)
{
string szDate = string.Empty;
int nPrefix = 0;
int nSufix = 0;
nPrefix = (Convert.ToInt32(dateTime.Year.ToString().Substring(1, 1) + "00") + 100) + Convert.ToInt32(dateTime.Year.ToString().Substring(2, 2));
nSufix = dateTime.DayOfYear;
szDate = nPrefix.ToString() + nSufix.ToString("000");
if (szDate.Length > 6)
szDate = szDate.Substring(1, 6);
return Convert.ToInt32(szDate);
}
errror say input string not in correct formate with datetime
so How to solve this error to work on server ?
Jayraj ChhayaPosted Dec 22, 2023, 6:00 AM
The "Input string was not in a correct format" error typically occurs when there is an issue with parsing a string into a DateTime object. In your case, the error is happening when converting the value stored in the Session object to a DateTime object using the
Convert.ToDateTime()method.To solve this error and make your application work on the server, you can follow these steps:
Check the value stored in the
Session[SessionKeys.VisaExpiryDate]object. Make sure it is a valid DateTime string that can be parsed correctly.Use the
DateTime.TryParse()method instead ofConvert.ToDateTime()to handle the conversion. This method returns a boolean value indicating whether the conversion was successful or not, without throwing an exception.By using
DateTime.TryParse(), you can avoid the exception and handle the case when the conversion fails gracefully.Make sure the culture settings on the server are the same as on your local machine. DateTime parsing can be affected by different culture settings, such as date formats and separators. You can set the culture explicitly in your application's configuration or code to ensure consistent behavior.
By setting the culture explicitly, you can ensure that the DateTime parsing is consistent across different environments.
By following these steps, you should be able to solve the "Input string was not in a correct format" error and make your ASP.NET MVC application work correctly on the server.
Anandu G NathPosted Dec 22, 2023, 4:15 AM
Amit MohantyPosted Nov 8, 2023, 6:49 AM
Vishal JoshiPosted Nov 8, 2023, 5:56 AM
Hello Ahmed,
It can be due to different formats based on server and culture. there are two solutions that can help to resolve issue.
Solution 1:
Solution 2: Add globalization to web.config
Thanks
Vishal Joshi
Jignesh KumarPosted Nov 8, 2023, 5:05 AM
Hello,
You are assigning 0 to your datetime field. You should assign date to that field, Your VisaExpiryDate field should be nullable
public DateTime? VisaExpiryDate { get;set; }