944,158 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 603
  • C# RSS
Nov 9th, 2009
0

Fetching records based on range of dates

Expand Post »
Hai,
I need to fetch records in to gridview based on fromdate and todate using c#,plzzz help asap
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dhana3 is offline Offline
3 posts
since Oct 2009
Nov 9th, 2009
0
Re: Fetching records based on range of dates
Are you wanting a WHERE clause example? If so, what database are you using?
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Nov 9th, 2009
0
Re: Fetching records based on range of dates
Use BETWEEN caluse - BETWEEN returns TRUE if the value of test_expression is greater than or equal to the value of begin_expression and less than or equal to the value of end_expression.

text Syntax (Toggle Plain Text)
  1. SELECT * FROM TableName Where TestDate
  2. BETWEEN '01/01/2009' AND '01/31/2009'
Last edited by adatapost; Nov 9th, 2009 at 9:31 pm.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Nov 10th, 2009
0
Re: Fetching records based on range of dates
Here is another example of MSSQL using the between operater with parameterized SQL:
C# Syntax (Toggle Plain Text)
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
  4. const string query = "Select * From Invoice Where (InHomeDate BETWEEN @Date1 and @Date2)";
  5. using (SqlConnection conn = new SqlConnection(connStr))
  6. {
  7. conn.Open();
  8. using (SqlCommand cmd = new SqlCommand(query, conn))
  9. {
  10. cmd.Parameters.Add(new SqlParameter("@Date1", SqlDbType.DateTime)).Value = DateTime.Today.AddDays(-180);
  11. cmd.Parameters.Add(new SqlParameter("@Date2", SqlDbType.DateTime)).Value = DateTime.Today;
  12. using (SqlDataReader dr = cmd.ExecuteReader())
  13. {
  14. DataTable dt = new DataTable();
  15. dt.Load(dr);
  16. dataGridView1.DataSource = dt;
  17. }
  18. }
  19. }
  20. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Web content blocking
Next Thread in C# Forum Timeline: socket exception





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC