I have a DateTimePicker and a DataGridView. I want to display records which match the selected date from the DateTimePicker. But I am not able to code the Select statement properly to display matching records. If I am using "Select * from Results", then ALL records are beieing displayed.

Can anybody please help me.

My existing code is as below:

string strConnection = @"server=.\SQLEXPRESS; integrated security = true; database=MeraNumberAyega";
string strSQLFillGrid = @"Select * from Results where ResultDate = @ResultDate";

SqlConnection sqlConnFillGrid = null;
try
{
       sqlConnFillGrid = new SqlConnection(strConnection);

       SqlCommand sqlCmdFillGrid = new SqlCommand(strSQLFillGrid, sqlConnFillGrid);
       sqlCmdFillGrid.Parameters.Add(new SqlParameter("@ResultDate", dtpResultDate.Value.Date));
                
       SqlDataAdapter myDataAdapter = new SqlDataAdapter(sqlCmdFillGrid.CommandText, strConnection);
       //SqlDataAdapter myDataAdapter = new SqlDataAdapter("Select * from Results", strConnection);
       SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myDataAdapter);

       DataTable myDataTable = new DataTable();
       myDataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;

       myDataAdapter.Fill(myDataTable);

       bindingSource1.DataSource = myDataTable;

       dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (SqlException sqlExp)
{
       MessageBox.Show("Error : " + sqlExp.Message);
}

Thank you.

Lalit Kumar Barik
India

form your query dynamically:

... where ResultDate =" +selected_date;
here selected_date is date selected by the user in date time picker.
one more thing before preparing the query dont forget to format the date, as it is stored in data base.

This article has been dead for over six months. Start a new discussion instead.