Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm developing a C# .Net WindowsForm application in Visual Studio 2015. I'm trying to filter records by supplying initial and final dates from TextBoxes on a Form when I click a button. Can you please take a look at my code and tell me what's wrong? I really appreciate it. Thanks a lot!

What I have tried:

private void buttonFilter_Click(object sender, EventArgs e)
{
    try
    {
        if (string.IsNullOrEmpty(textBoxDataIni.Text) | string.IsNullOrEmpty(textBoxDataFin.Text))
        {
            qry_retornosBindingSource.Filter = string.Empty;
        }
        else
        {

            qry_retornosBindingSource.Filter = string.Format("data >= {0}%' & data <= '{1}%'", textBoxDataIni.Text, textBoxDataFin.Text);
            this.qry_retornosTableAdapter.Fill(this.dBpetControlDataSet.qry_retornos);
        }
    }
    catch (Exception)
    {
    }
}
Posted
Updated 14-Sep-18 11:57am
v2
Comments
phil.o 14-Sep-18 16:49pm    
Are you storing datetimes as strings in your database?

1 solution

I solved the problem. I had to Convert.ToDateTime the TextBox Texts and fix the code to filter the records. SEe the correct code below:


private void buttonFilter_Click(object sender, EventArgs e)
{
    try
    {
        if (string.IsNullOrEmpty(textBoxDataIni.Text) || string.IsNullOrEmpty(textBoxDataFin.Text))
        {
            this.qry_retornosBindingSource.Filter = string.Empty;
        }
        else
        {
            this.qry_retornosBindingSource.Filter = string.Format("Data >= '{0}' AND Data <= '{1}'", Convert.ToDateTime(textBoxDataIni.Text), Convert.ToDateTime(textBoxDataFin.Text));
        }
    }
    catch (Exception)
    {
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900