Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to search data from database between two dates using datetimepicker control in window form application asp.net C#
Posted
Comments
syed shanu 30-Jun-14 1:37am    
so what is the problem .write a query and pass from and to date of datetime piker in between sqlqct list for example "select * from your_tableName where youdates between '"+ datetimepicker1.value +"' and '"datetimepicker1.value +"'"
Awadhendra Tripathi 30-Jun-14 1:58am    
What you tried write your code...
Nishant.Chauhan80 30-Jun-14 2:24am    
private void button1_Click(object sender, EventArgs e)
{
con.Open();

SqlCommand cmd = new SqlCommand("select * from tbadd where Dop between (@param1) and (@param2)", con);
cmd.Parameters.Add("@param1", SqlDbType.VarChar, 50).Value = dateTimePicker1.Value;
cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = dateTimePicker2.Value;

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(ds,"sr");
dataGridView1.DataSource = ds.Tables["sr"];
con.Close();
}
Nishant.Chauhan80 30-Jun-14 2:22am    
but i use two datetimepicker
PrakashCs.net 30-Jun-14 4:19am    
is it giving some error or returning empty result set?

Hi Hope this will help
you should set your datatype in sql table must be Date
SQL
SELECT * FROM tableName WHERE Date BETWEEN ‘Datetimepicker1.Text’ AND ‘Datetimepicker1.Text’
 
Share this answer
 
Hi,
you declare the SqlVariable in Varchar data type.so u must give the single quote in front and rear of the value.

C#
SqlCommand cmd = new SqlCommand("select * from tbadd where Dop between (@param1) and (@param2)", con);
cmd.Parameters.Add("@param1", SqlDbType.VarChar, 50).Value = '"+dateTimePicker1.Value+"';
cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value ='"+dateTimePicker2.Value+"';
 
Share this answer
 
v2
Comments
Nishant.Chauhan80 30-Jun-14 2:44am    
Not working this code

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