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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.