Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. I want to load data based from sql based on date. There are 2 datepicker(date1 and date2) and I want it to select from only 1 column(start_time). So far, I can already load the data but when I try to use the datepicker it will become wrong. Please help me as I am very new to this.

What I have tried:

private void Button_Click(object sender, RoutedEventArgs e)
       {
           using (SqlConnection sqlCon = new SqlConnection(connectionString))
           {
               sqlCon.Open();
               SqlDataAdapter sqlDa = new SqlDataAdapter ("empselect where Start_time
               BETWEEN @date1 AND @date2", sqlCon);
               DataTable dtbl = new DataTable();
               sqlDa.Fill(dtbl);
               emp.ItemsSource = dtbl.DefaultView;

           }
       }
Posted
Updated 30-Oct-19 2:09am

You need to provide the parameter data, as well as the command string (which looks unlikely to work anyway) via the SqlCommand.Parameters collection.
 
Share this answer
 
Comments
Member 14638721 30-Oct-19 4:12am    
Why unlikely to work? It cannot be done?
As OriginalGriff answered, you will need to add those date-picker values to the query.
Generally I do this by creating the SqlCommand object prior to the SqlDataAdapter definition
C#
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM empselect WHERE Start_time BETWEEN @date1 AND @date2", sqlCon);
sqlCmd.Parameters.AddWithValue("@date1", /* datepicker start date here */);
sqlCmd.Parameters.AddWithValue("@date2", /* datepicker stop  date here */);

SqlDataAdapter sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = sqlCmd;
 
Share this answer
 

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