Code Error

Ask general questions here.
NewLearner
Posts: 29
Joined: Wed Jan 15, 2014 3:32 pm

Code Error

Post by NewLearner » Tue Jan 13, 2015 12:14 pm

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Code Error

Post by Support Team » Wed Jan 14, 2015 10:00 am

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