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
C#
if (Session[SessionKeys.VisaExpiryDate] == null)
{
visaExpiryDate = 0;
}
else
{
DateTime visDateExpire = Convert.ToDateTime(Session[SessionKeys.VisaExpiryDate]);
visaExpiryDate = JulianDate.DateTimeToJulian(visDateExpire);
}for details as below
C#
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 ?

Jignesh KumarRank #91 · 22.9K reputationPosted Nov 8, 2023, 12:05 AM EST
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; }
if (Session[SessionKeys.VisaExpiryDate] == null) { visaExpiryDate = (DateTime?) null; //Here you need to assign Date field. Due to this its giving error } else { DateTime visDateExpire = Convert.ToDateTime(Session[SessionKeys.VisaExpiryDate]); visaExpiryDate = JulianDate.DateTimeToJulian(visDateExpire); }Vishal JoshiRank #377 · 6.7K reputationPosted Nov 8, 2023, 12:56 AM EST
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:
DateTime visDateExpire = DateTime.ParseExact(TxtPolCreatedDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);Solution 2: Add globalization to web.config
Thanks
Vishal Joshi
Amit MohantyRank #112 · 20K reputationPosted Nov 8, 2023, 1:49 AM EST
string dateVisaExpiry = Session[SessionKeys.VisaExpiryDate].ToString(); if (DateTime.TryParseExact(dateVisaExpiry, "yyyy-MM-dd", CultureInfo.InvariantCulture, out DateTime visDateExpire)) { visaExpiryDate = JulianDate.DateTimeToJulian(visDateExpire); } else { // Handle invalid date format visaExpiryDate = 0; }Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 22, 2023, 1:00 AM EST
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.if (Session[SessionKeys.VisaExpiryDate] == null) { visaExpiryDate = 0; } else { DateTime visDateExpire; if (DateTime.TryParse(Session[SessionKeys.VisaExpiryDate].ToString(), out visDateExpire)) { visaExpiryDate = JulianDate.DateTimeToJulian(visDateExpire); } else { // Handle the case when the conversion fails // You can set a default value or show an error message } }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 NathRank #681 · 3.5K reputationPosted Dec 21, 2023, 11:15 PM EST
if (Session[SessionKeys.VisaExpiryDate] != null && Session[SessionKeys.VisaExpiryDate] is DateTime) { DateTime visDateExpire = (DateTime)Session[SessionKeys.VisaExpiryDate]; }