chander
Apr 14th, 2003, 05:06 AM
i m making a web user control and have some server side control on it .. i hv 2 fields to get date but i couldnt find any DT Picker control in Server side control .. there is only Calendar control btu its too big to fit .. so i let the date entered in normal text box but before sending the values to Server i want to validate it .. like it shouldnt be > than Today Date... wat i did , i changed the string to Date by DateTime's Parse Method .. and Compare it with today's date with CompareTo method of DateTime class . well i think i m getting all right value in comparing when i see these in Quick watch in debug mode but when i go next step further by F11 it doesnt compare right value means if CompareTo method is giving -1 by comparing Date which i entered and Today's date and i hv checked it with 1 still it goesin if condition ... i give a small code of C# .. try to run in a simple Window's Form with one text box and one button....
//Declare dt of type DateTime .
protected DateTime dt = new DateTime();
private void cmdSubmit_Click_1(object sender, System.EventArgs e)
{
bool errVal;
errVal=validcheck();
MessageBox.Show(errVal.ToString());
}
// in this i called a fun which will return true if i entered wrong date otherwise false ..
private bool validcheck()
{
try
{
if (txtDOB.Text.Trim()!= "") // if txtDOB is not ""
{
dt=System.DateTime.Parse(txtDOB.Text);
// converted String date to DateTime type ...
if (DateTime.Compare(dt.Date,DateTime.Today) == 1 )
{
// 1 is returned if dt.Date is > Today date... but when i put for eg. 08-05-1977 which less than today's Date then it return -1 which i can see in quick watch ....
return true; //return true and get back to calling pos.
}
}
}
catch(Exception e)
{
MessageBox.Show("Exception Occured " + e.ToString());
return true;
}
return false; // if date condition is valid return false ..
}
as i wrote CompareTo method returns -1 in quick watch as per that it shouldnt enter if condition where it is return true .. but every time it is enterign in if condition executing return true but still it doesnt terminate the fun and get back it come down to return false and the get back to calling code ...
i think i m bit wrong with return statement or i m ot aware with its functionality in C# .. as far as i know it termiantes the execution and pass the control back to calling stage.. can any one throw some light on it ...
OR IF SOME ONE HAS BETTER IDEA OF VALIDATING THE DATE IN ASP.NET IN C# PLS TELL ME ....
//Declare dt of type DateTime .
protected DateTime dt = new DateTime();
private void cmdSubmit_Click_1(object sender, System.EventArgs e)
{
bool errVal;
errVal=validcheck();
MessageBox.Show(errVal.ToString());
}
// in this i called a fun which will return true if i entered wrong date otherwise false ..
private bool validcheck()
{
try
{
if (txtDOB.Text.Trim()!= "") // if txtDOB is not ""
{
dt=System.DateTime.Parse(txtDOB.Text);
// converted String date to DateTime type ...
if (DateTime.Compare(dt.Date,DateTime.Today) == 1 )
{
// 1 is returned if dt.Date is > Today date... but when i put for eg. 08-05-1977 which less than today's Date then it return -1 which i can see in quick watch ....
return true; //return true and get back to calling pos.
}
}
}
catch(Exception e)
{
MessageBox.Show("Exception Occured " + e.ToString());
return true;
}
return false; // if date condition is valid return false ..
}
as i wrote CompareTo method returns -1 in quick watch as per that it shouldnt enter if condition where it is return true .. but every time it is enterign in if condition executing return true but still it doesnt terminate the fun and get back it come down to return false and the get back to calling code ...
i think i m bit wrong with return statement or i m ot aware with its functionality in C# .. as far as i know it termiantes the execution and pass the control back to calling stage.. can any one throw some light on it ...
OR IF SOME ONE HAS BETTER IDEA OF VALIDATING THE DATE IN ASP.NET IN C# PLS TELL ME ....