Page 1 of 1

Code Error

Posted: Tue Jan 13, 2015 12:14 pm
by NewLearner
public void funcDateManipulation(string todaydate, string adddate, string subdate, string addmonth, string submonth, string spare1, string spare2)
{

string strYear = System.DateTime.Now.Year.ToString();
string strMonth = System.DateTime.Now.Month.ToString();
string strDay = System.DateTime.Now.Day.ToString();
double newdate;
int month;
// string output;
bool output;
output = string.IsNullOrEmpty(adddate);

if (!output )

{
newdate = Convert.ToDouble(adddate);
System.DateTime newcheckdate = System.DateTime.Now.AddDays(newdate);
strDay = newcheckdate.Day.ToString();
}

output = string.IsNullOrEmpty(addmonth);

if (!output )

{
month = Convert.ToInt32(addmonth);
System.DateTime newcheckdate = System.DateTime.Now.AddMonths(month);
strMonth = newcheckdate.Month.ToString();
}
//strDay = newdate.ToString();
pmanipulateddate = strDay + "/" + strMonth + "/" + strYear;


}

I'm getting error "string not in correct formate" I don't know whats the mistake inthis code... pmanipulateddate is variable defined in the module variable....pls let me know how to check not null in if statement....

Re: Code Error

Posted: Wed Jan 14, 2015 10:00 am
by Support Team
Hello NewLearner,

The issue is that there is no query for invalid characters (everything beside digits). Therefore the “Convert.ToDouble()” fails if the string contains, e.g., a letter.

You could use following method in order to check if the string is in a correct format:
private	bool CheckString(string s)
		{
			if(!String.IsNullOrEmpty(s))
			{
				foreach (char c in s)
				{
					if (!Char.IsDigit(c))
						return false;
				}
				return true;
			}
			return false;
		}
Regards,
Robert