Seryu Seryu - 5 months ago 17
SQL Question

How to filter date using datetimepicker via database

Can you help me, i have a button to filtered data with date range base on my 2 datetimepicker (datefrom and dateto), Below is my code, and when i click the button it displays nothing. can you help me what is the problem? im new in C#.

MySqlConnection mcon = new MySqlConnection("datasource=localhost;port=3306;username=8888;password=888888");
MySqlDataAdapter mda = new MySqlDataAdapter("select * from bio_db.daily_data2 where Date between '" + datefrom.Value.ToString() + "' and '" + dateto.Value.ToString() + "' ", mcon);
mcon.Open();
DataSet ds = new DataSet();
mda.Fill(ds);
dbgrid1.DataSource = ds;
dbgrid1.Refresh();
mcon.Close();

Answer

Hi guys i already figure out the missing link. Here's the corrected script. Thanks everyone!

DateTime dtFrom = Convert.ToDateTime(datefrom.Text); //some DateTime value, e.g. DatePicker1.Text;
DateTime dtTo = Convert.ToDateTime(dateto.Text); //some DateTime value, e.g. 
DatePicker1.Text;<n>MySqlConnection mcon = new MySqlConnection("datasource=localhost;port=3306;username=8888;password=8888888");
MySqlDataAdapter mda = new MySqlDataAdapter("select * from bio_db.daily_data2 where Date between '" + dtFrom.ToString("yyyy/MM/dd") + "' and '" + dtTo.ToString("yyyy/MM/dd") + "' ", mcon);

        System.Data.DataSet ds = new System.Data.DataSet();
        mcon.Open();
        mda.Fill(ds, "root");
        dbgrid1.DataSource = ds.Tables["root"];
        dbgrid1.Refresh();
        mcon.Close();