Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. dateTimePicker chosen problem

dateTimePicker chosen problem

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    miss YY
    wrote on last edited by
    #1

    DateTime mm = dateTimePicker1.Value; DateTime nn = dateTimePicker2.Value; SqlCommand SCD = new SqlCommand("select * from table_inf where tim>= @mm and tim<=@nn", Conn); SCD.Parameters.AddWithValue("@mm", mm); SCD.Parameters.AddWithValue("@nn", nn); sqlDataAdapter1.SelectCommand = SCD; sqlDataAdapter1.Fill(DT); Conn.Open(); dataGridView1.DataSource = DT; when i chosen dateTimePicker1 on today and chosen dateTimePicker2 on today too,it can't select Today'data....when you want to select today'data,you must change dateTimePicker2 on tomorrow,it's trouble! How can i chosen dateTimePicker1 on today and chosen dateTimePicker2 on today also can chosen Today's Data! i don't know how to change my Code ,thanks to everyone! i,m poor in english,sorry i use DATA sql2000

    L D 2 Replies Last reply
    0
    • M miss YY

      DateTime mm = dateTimePicker1.Value; DateTime nn = dateTimePicker2.Value; SqlCommand SCD = new SqlCommand("select * from table_inf where tim>= @mm and tim<=@nn", Conn); SCD.Parameters.AddWithValue("@mm", mm); SCD.Parameters.AddWithValue("@nn", nn); sqlDataAdapter1.SelectCommand = SCD; sqlDataAdapter1.Fill(DT); Conn.Open(); dataGridView1.DataSource = DT; when i chosen dateTimePicker1 on today and chosen dateTimePicker2 on today too,it can't select Today'data....when you want to select today'data,you must change dateTimePicker2 on tomorrow,it's trouble! How can i chosen dateTimePicker1 on today and chosen dateTimePicker2 on today also can chosen Today's Data! i don't know how to change my Code ,thanks to everyone! i,m poor in english,sorry i use DATA sql2000

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Our problem is that you have he following being executed... WHERE tim >= 01-DEC-2009 00:00:00 AND tim <= 01-DEC-2009 00:00:00 (or something like that) because DateTimePicker.Value is a DateTime not a Date. SO you need to convert your mm to truncate the time portion and your nn to truncate the time and add one day, and change the WHERE to WHERE tim >= @mm and tim < @nn (i.e. use less than not less than or equal to) If DateTimePicker returns the date with a zero time (i.e. midnight on that day) then you just need to change the <= to < and change SCD.Parameters.AddWithValue(@nn, nn.AddDays(1))

      ___________________________________________ .\\axxx (That's an 'M')

      M 1 Reply Last reply
      0
      • M miss YY

        DateTime mm = dateTimePicker1.Value; DateTime nn = dateTimePicker2.Value; SqlCommand SCD = new SqlCommand("select * from table_inf where tim>= @mm and tim<=@nn", Conn); SCD.Parameters.AddWithValue("@mm", mm); SCD.Parameters.AddWithValue("@nn", nn); sqlDataAdapter1.SelectCommand = SCD; sqlDataAdapter1.Fill(DT); Conn.Open(); dataGridView1.DataSource = DT; when i chosen dateTimePicker1 on today and chosen dateTimePicker2 on today too,it can't select Today'data....when you want to select today'data,you must change dateTimePicker2 on tomorrow,it's trouble! How can i chosen dateTimePicker1 on today and chosen dateTimePicker2 on today also can chosen Today's Data! i don't know how to change my Code ,thanks to everyone! i,m poor in english,sorry i use DATA sql2000

        D Offline
        D Offline
        dan sh
        wrote on last edited by
        #3

        When you sleect the date from the DateTimePicker, it takes the selected date and the current time as the slected value. Now, in your query, say I selected date as 13-Dec-2009, the value that will be sent is "12/13/2009 10:42:54 AM". So, when you select same values in both the datepickers, the query will look like this:

        select * from table_inf where tim>= '12/13/2009 10:42:54 AM' and tim<='12/13/2009 10:42:59 AM'

        (Five second gap assuming it itakes you 5 seconds to select date in both the date pickers) Now, this query will search for reacords that fall under that 5 second period and hence you are not getting any result. You should make use of DateDiff method of SQL and compare only the date part.

        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

        1 Reply Last reply
        0
        • L Lost User

          Our problem is that you have he following being executed... WHERE tim >= 01-DEC-2009 00:00:00 AND tim <= 01-DEC-2009 00:00:00 (or something like that) because DateTimePicker.Value is a DateTime not a Date. SO you need to convert your mm to truncate the time portion and your nn to truncate the time and add one day, and change the WHERE to WHERE tim >= @mm and tim < @nn (i.e. use less than not less than or equal to) If DateTimePicker returns the date with a zero time (i.e. midnight on that day) then you just need to change the <= to < and change SCD.Parameters.AddWithValue(@nn, nn.AddDays(1))

          ___________________________________________ .\\axxx (That's an 'M')

          M Offline
          M Offline
          miss YY
          wrote on last edited by
          #4

          thanks,that's it i' ll do it

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups