M
Mike P
I'm calling this procedure from a custom validator OnServerValidate
event. I keep getting the error :
Use of unassigned local variable 'DateFrom'
I'm new to c# so I'm not really sure about the syntax I'm using. But it
seems the problem is that the DateFrom and DateTo values get lost
somewhere between being initialised and the line if (DateFrom > DateTo).
Can anybody help me out?
public void ValidateDates(object sender,
System.Web.UI.WebControls.ServerValidateEventArgs value)
{
bool blnValid = false;
DateTime DateFrom, DateTo;
int intErrorCount = 0;
if (SelectLog.Items[0].Selected)
{
blnValid = true;
}
else
{
try
{
DateFrom = Convert.ToDateTime(DayFrom.SelectedItem.Value
+ "/" + MonthFrom.SelectedItem.Value + "/" +
YearFrom.SelectedItem.Value);
}
catch
{
intErrorCount += 1;
}
try
{
DateTo = Convert.ToDateTime(DayTo.SelectedItem.Value +
"/" + MonthTo.SelectedItem.Value + "/" + YearTo.SelectedItem.Value);
}
catch
{
intErrorCount += 2;
}
if (intErrorCount == 1)
{
valCustom.ErrorMessage = "Please enter a valid Date
From";
blnValid = false;
value.IsValid = blnValid;
return;
}
else if (intErrorCount == 2)
{
valCustom.ErrorMessage = "Please enter a valid Date To";
blnValid = false;
value.IsValid = blnValid;
return;
}
else if (intErrorCount == 3)
{
valCustom.ErrorMessage = "Please enter a valid Date From
and Date To";
blnValid = false;
value.IsValid = blnValid;
return;
}
else
{
if (DateFrom > DateTo)
{
valCustom.ErrorMessage = "The date in the Date To
field must be the same or later than the date in the Date From field";
blnValid = false;
}
else
{
blnValid = true;
}
}
}
value.IsValid = blnValid;
return;
}
event. I keep getting the error :
Use of unassigned local variable 'DateFrom'
I'm new to c# so I'm not really sure about the syntax I'm using. But it
seems the problem is that the DateFrom and DateTo values get lost
somewhere between being initialised and the line if (DateFrom > DateTo).
Can anybody help me out?
public void ValidateDates(object sender,
System.Web.UI.WebControls.ServerValidateEventArgs value)
{
bool blnValid = false;
DateTime DateFrom, DateTo;
int intErrorCount = 0;
if (SelectLog.Items[0].Selected)
{
blnValid = true;
}
else
{
try
{
DateFrom = Convert.ToDateTime(DayFrom.SelectedItem.Value
+ "/" + MonthFrom.SelectedItem.Value + "/" +
YearFrom.SelectedItem.Value);
}
catch
{
intErrorCount += 1;
}
try
{
DateTo = Convert.ToDateTime(DayTo.SelectedItem.Value +
"/" + MonthTo.SelectedItem.Value + "/" + YearTo.SelectedItem.Value);
}
catch
{
intErrorCount += 2;
}
if (intErrorCount == 1)
{
valCustom.ErrorMessage = "Please enter a valid Date
From";
blnValid = false;
value.IsValid = blnValid;
return;
}
else if (intErrorCount == 2)
{
valCustom.ErrorMessage = "Please enter a valid Date To";
blnValid = false;
value.IsValid = blnValid;
return;
}
else if (intErrorCount == 3)
{
valCustom.ErrorMessage = "Please enter a valid Date From
and Date To";
blnValid = false;
value.IsValid = blnValid;
return;
}
else
{
if (DateFrom > DateTo)
{
valCustom.ErrorMessage = "The date in the Date To
field must be the same or later than the date in the Date From field";
blnValid = false;
}
else
{
blnValid = true;
}
}
}
value.IsValid = blnValid;
return;
}