Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, i want to select data from database between two dates, its showing data only that month, if i want to show data between two months its not showing.
I am using this code for retrieving data.
C#
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM [tablename] where Date BETWEEN '"+txtDateFrom.Text+"' AND '"+txtToDate.Text+"'"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
        }
Posted
Updated 20-Dec-13 20:26pm
v2
Comments
♥…ЯҠ…♥ 21-Dec-13 2:32am    
Nothing to do with C# here, Can you give sample data to optimize your query.....? what is the input and what is the expected output?
Sachin Mortale 21-Dec-13 2:35am    
I want to show data between '10/10/2013' and '20/12/2013' but its display only 12th(Dec) month data.
♥…ЯҠ…♥ 21-Dec-13 2:37am    
You sure you have data for the prev month also? execute your query in sql server and see what you get
Sachin Mortale 21-Dec-13 2:38am    
Yes i have data in 10th month and 12th month but not in 11th month
♥…ЯҠ…♥ 21-Dec-13 2:57am    
SELECT * FROM [tablename] where Date BETWEEN '10/10/2013' AND '20/12/2013' execute this query in sql server and check your data, if data exist then time could be the issue... check it now

Select * from Tablename where (Initiate_date BETWEEN '" + txtsdate.Text + "' AND '" + txtfdate.Text + "')
 
Share this answer
 
Hi,

You can use following code of SQL to get date between records

where Date BETWEEN Convert(DateTime,'"+txtFromDate.Text+"',101) AND Convert(DateTime,'"+txtToDate.Text+"',101)"


you can also use server side filter option on DataTable object
DataTable dt = new DataTable();
//Date between datefrom and dateto expression like sql server
string strSQLExp = "Date=21/12/2013";
dt.Select(strSQLExp);
that you have use for binding data to datacontrol


Don't forgot to mark this as solution if it is.

Happy Codding...
Nayan Chavada
 
Share this answer
 
Try like this,

SQL
SELECT * FROM tablename
WHERE datecolumn >= '"+txtDateFrom.Text+"'  AND datecolumn <=  '"+txtDateTo.Text+"'  
 
Share this answer
 
As per SQL database default date format is mm/dd/year.

so you have to convert your textbox date to sql's date.

DateTime.ParseExact(<textboxdate>, "dd/MM/yyyy", null).ToString("MM/dd/yyyy");
 
Share this answer
 
SQL
SELECT * FROM [tablename] where Date BETWEEN Convert(Datetime,'"+txtDateFrom.Text+"',103) AND Convert(datetime,'"+txtToDate.Text+"',103)"
 
Share this answer
 
v2

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