Hi Guys this must be a simple logic for most programmers but i am not able to figure it out.
I have 2 datetimepicker on my windows form datetimepicker 1 = fromdate datetimepicker 2 = todate
the from date and to date i get from the below sql
SELECT MIN(TransDate) AS mindate, MAX(TransDate) AS maxdate
FROM dbo.EtimePunchDetail WHERE (EmpID = @empid)
fromdate = mindate and todate = maxdate
fromdate.mindate = mindate todate.maxdate = maxdate ("So the user is only working with the selected date range")
i added two textboxes with search functionality that the user can enter the fromdate and todate into and it checks against the mindate and maxdate and if the user enters a date out of the range i set the message box to throw an error
I have an textbox changed even that has the following query:
private void Min_TextChanged(object sender, EventArgs e)
{
DateTime date = DateTime.Parse(Min.Text);
if (date < DateTime.Parse(AvailableMin.Text))
{
MessageBox.Show("The Date you entered is either out of range or an invalid format");
}
else
{
FromDate.MinDate = date;
FromDate.Value = date;
}
}
private void Max_TextChanged(object sender, EventArgs e)
{
DateTime date = DateTime.Parse(Max.Text);
if (date > DateTime.Parse(AvailableMax.Text))
{
MessageBox.Show("The Date you entered is either out of range or an invalid format");
}
else
{
ToDate.MaxDate = date;
ToDate.Value = date;
}
}
But as i change the text the textchanged event gets fired with the message and is not letting me change the date or it says it is an invalid date. i want to be able to enter a date and then the textchanged should check to see if the date entered is not inrange how can i do that??
here is a visual representation for what i am asking for:
More Code on how i am getting the min and max date and the other things i am doing with those values i should have included this in my question before i apologize i thing the datetimepicker validation is interfering with the textbox validation
Min and Max Value
private void mindateset() // fill the listbox of values of mindate and maxdate
{
if (Employee.SelectedValue != null)
{
if (Employee.SelectedValue.ToString().Trim().Length > 0)
{
try
{
using (MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
{
timepunchnew = new EtimeHistoryDataSet();
connection.Open();
using (MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT MIN(TransDate) AS mindate, MAX(TransDate) AS maxdate FROM dbo.EtimePunchDetail WHERE (EmpID = @empid)", connection))
{
MSSQL.SqlParameter myminparam = new MSSQL.SqlParameter();
myminparam.Direction = ParameterDirection.Input;
myminparam.ParameterName = "@empid";
myminparam.Value = Employee.SelectedValue;
command.Parameters.Add(myminparam);
MSSQL.SqlDataAdapter myadapter = new System.Data.SqlClient.SqlDataAdapter();
myadapter.SelectCommand = command;
myadapter.Fill(timepunchnew, "Mindate");
AvailableMin.DataSource = timepunchnew.Mindate;
AvailableMin.DisplayMember = "mindate";
AvailableMax.DataSource = timepunchnew.Mindate;
AvailableMax.DisplayMember = "maxdate";
FromDate.MinDate = DateTime.Parse(AvailableMin.Text);
FromDate.Value = FromDate.MinDate;
ToDate.MaxDate = DateTime.Parse(AvailableMax.Text);
ToDate.Value = ToDate.MaxDate;
Min.Text = FromDate.MinDate.ToString("d");
Max.Text = ToDate.MaxDate.ToString("d");
}
}
}
catch (Exception) { /*Handle error*/ }
}
}
}
Validation on the datetimepicker values
private void FromDate_ValueChanged_1(object sender, EventArgs e)
{
if (empchanging == false)
{
if (FromDate.Value > ToDate.Value)
{
// MessageBox.Show("From Date Cannot Be Greater Than To Date");
if (DialogResult.OK == MessageBox.Show("From Date Cannot Be Greater Than To Date"))
{
FromDate.MinDate = DateTime.Parse(AvailableMin.Text);
FromDate.Value = FromDate.MinDate;
}
}
}
}
private void ToDate_ValueChanged_1(object sender, EventArgs e)
{
if (empchanging == false)
{
if (ToDate.Value < FromDate.Value)
{
//MessageBox.Show("To Date Cannot Be Less Than From Date");
if (DialogResult.OK == MessageBox.Show("To Date Cannot Be Less Than From Date"))
{
ToDate.MaxDate = DateTime.Parse(AvailableMax.Text);
ToDate.Value = ToDate.MaxDate;
}
}
}
}
Validating Available date range for empty string
private void AvailableMin_SelectedIndexChanged(object sender, EventArgs e)
{
if (AvailableMin.Text == string.Empty)
{
textBox2.Visible = true;
textBox2.Text = "There is no From Date available for this particular user";
}
else
{
textBox2.Visible = false;
}
}
private void AvailableMax_SelectedIndexChanged(object sender, EventArgs e)
{
if (AvailableMax.Text == string.Empty)
{
textBox1.Visible = true;
textBox1.Text = "There is no To Date available for this particular user";
}
else
{
textBox1.Visible = false;
}
}
I tried the below solution
private void Min_TextChanged(object sender, EventArgs e)
{
DateTime date;
if (!DateTime.TryParse(Min.Text, out date))
{
formerrorprovider.SetError(this.Min,"The Date you entered is in invalid format");
}
else if (date < DateTime.Parse(AvailableMin.Text))
{
formerrorprovider.SetError(this.Min, "The Date you entered is either out of range");
}
else
{
formerrorprovider.SetError(this.Min, string.Empty);
FromDate.MinDate = date;
FromDate.Value = date;
}
it accepts the date i enter but is interfering with the datetimepicker validation.
PLZ HELP
as a possible UI solution: don't show a MessageBox if date is invalid but add an ErrorProvider control to Form and set errors description to Max and Min textboxes. Error provider will show Error icon if there are incorrect data and hide it when input is acceptable
private void Max_TextChanged(object sender, EventArgs e)
{
DateTime date;
if (!DateTime.TryParse(Max.Text, out date))
{
formErrorProvider.SetError(this.Max, "The Date you entered is in invalid format");
}
else if (date > DateTime.Parse(AvailableMax.Text))
{
formErrorProvider.SetError(this.Max, "The Date you entered is out of range");
}
else
{
formErrorProvider.SetError(this.Max, string.Empty);
ToDate.MaxDate = date;
ToDate.Value = date;
}
}