hi guys i need help with date time picker.What i am trying to do is to select data from table according to teh date selected by the user .My code looks like this but it doesnt work, it doesnt pull anything, can anyone help me with where am i going wrong.

private void dateTimePicker1_ValueChanged_1(object sender, EventArgs e)
            SqlConnection cn = new SqlConnection(@"Server=;Database=RTLUser;UID=sa;");
            string myString = "";
            SqlDataAdapter dtadp = new SqlDataAdapter();
            DataTable dtbl = new DataTable();
            myString = @"Select word from wordTbl where date = @date";
           SqlCommand cmd = new SqlCommand(myString, cn);
            cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = dateTimePicker1.Value;
            DataSet data = new DataSet();
            SqlDataReader rdr = cmd.ExecuteReader();

            bool iftrue = false;

            while (rdr.Read())
                if (rdr[0] == (dateTimePicker1))
                    iftrue = true;
            if (iftrue == true)
                cmd = new SqlCommand("SELECT word from wordTbl where date ='" + dateTimePicker1 + "'");

                rdr = cmd.ExecuteReader();
                dtadp.SelectCommand = cmd;
                dataGridView2.DataSource = dtbl;

8 Years
Discussion Span
Last Post by nick.crane

Your SELECT statement only requests the word field.
This code if (rdr[0] == (dateTimePicker1)) compares the returned word field value with a DataTimePicker object (not even its value).
You need to review your code.

I would also recommend changing the code that sets the parameter value to use the date part only; assuming that is all your DB contains.

cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = dateTimePicker1.Value.Date; //<-- use DateTime.Date

Or change the SELECT comand to use a range (e.g. selected date +/- 1 day).

Votes + Comments
Nice explanation.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.