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
  1. Home
  2. General Programming
  3. C#
  4. get Datetime array through DataReader

get Datetime array through DataReader

Scheduled Pinned Locked Moved C#
data-structurestutorial
5 Posts 3 Posters 0 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
    Mr Kode
    wrote on last edited by
    #1

    hello all i want to know how to get DateTime array through DataReader and set it as bolded Dates on MonthCalender i have the following code .. SqlCommand GetDates = new SqlCommand(); GetDates.Connection = new SqlConnection(ConS); GetDates.CommandType = CommandType.Text; GetDates.CommandText = "select Reserve_Date from reservations where Done = 0"; GetDates.Connection.Open(); SqlDataReader rd = GetDates.ExecuteReader(); while (rd.Read()) { DateTime ReservDays = (DateTime)rd["Reserve_Date"]; monthCalendar1.AnnuallyBoldedDates = new DateTime[] { ReservDays }; } rd.Close(); GetDates.Connection.Close(); the reader works fine but the result is the last row only i want to get all rows and set it as AnnuallyBoldedDates on monthCalender thanks in Advance

    N P 2 Replies Last reply
    0
    • M Mr Kode

      hello all i want to know how to get DateTime array through DataReader and set it as bolded Dates on MonthCalender i have the following code .. SqlCommand GetDates = new SqlCommand(); GetDates.Connection = new SqlConnection(ConS); GetDates.CommandType = CommandType.Text; GetDates.CommandText = "select Reserve_Date from reservations where Done = 0"; GetDates.Connection.Open(); SqlDataReader rd = GetDates.ExecuteReader(); while (rd.Read()) { DateTime ReservDays = (DateTime)rd["Reserve_Date"]; monthCalendar1.AnnuallyBoldedDates = new DateTime[] { ReservDays }; } rd.Close(); GetDates.Connection.Close(); the reader works fine but the result is the last row only i want to get all rows and set it as AnnuallyBoldedDates on monthCalender thanks in Advance

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Mr.Kode wrote:

      the reader works fine but the result is the last row only

      Because you are overwriting the variable in each iteration. You need to get everything into an array and finally set to monthCalendar1.AnnuallyBoldedDates. Since you don't know the number of items, it is easy to use a List.

      var list = new List<DateTime>();
      while (rd.Read())
      {
      DateTime ReservDays = (DateTime)rd["Reserve_Date"];
      list.Add(ReservDays);
      }
      monthCalendar1.AnnuallyBoldedDates = list.ToArray();

      Your code can be improved further by calling Dispose() on SqlConnection, command and reader.

      Best wishes, Navaneeth

      M 1 Reply Last reply
      0
      • N N a v a n e e t h

        Mr.Kode wrote:

        the reader works fine but the result is the last row only

        Because you are overwriting the variable in each iteration. You need to get everything into an array and finally set to monthCalendar1.AnnuallyBoldedDates. Since you don't know the number of items, it is easy to use a List.

        var list = new List<DateTime>();
        while (rd.Read())
        {
        DateTime ReservDays = (DateTime)rd["Reserve_Date"];
        list.Add(ReservDays);
        }
        monthCalendar1.AnnuallyBoldedDates = list.ToArray();

        Your code can be improved further by calling Dispose() on SqlConnection, command and reader.

        Best wishes, Navaneeth

        M Offline
        M Offline
        Mr Kode
        wrote on last edited by
        #3

        thanks Navaneeth this really helps but how Dispose method improve performance?

        N 1 Reply Last reply
        0
        • M Mr Kode

          thanks Navaneeth this really helps but how Dispose method improve performance?

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Performance is not the reason to call Dispose. It avoids resource leaks. When you dispose your connection properly, ADO.NET's connection pooling can work efficiently. Here is a decent article on the subject : Implementing IDisposable and the Dispose Pattern Properly[^]

          Best wishes, Navaneeth

          1 Reply Last reply
          0
          • M Mr Kode

            hello all i want to know how to get DateTime array through DataReader and set it as bolded Dates on MonthCalender i have the following code .. SqlCommand GetDates = new SqlCommand(); GetDates.Connection = new SqlConnection(ConS); GetDates.CommandType = CommandType.Text; GetDates.CommandText = "select Reserve_Date from reservations where Done = 0"; GetDates.Connection.Open(); SqlDataReader rd = GetDates.ExecuteReader(); while (rd.Read()) { DateTime ReservDays = (DateTime)rd["Reserve_Date"]; monthCalendar1.AnnuallyBoldedDates = new DateTime[] { ReservDays }; } rd.Close(); GetDates.Connection.Close(); the reader works fine but the result is the last row only i want to get all rows and set it as AnnuallyBoldedDates on monthCalender thanks in Advance

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Your statement "monthCalendar1.AnnuallyBoldedDates = new DateTime[] { ReservDays };" recreates the array on each iteration. And also, the statement "DateTime ReservDays = (DateTime)rd["Reserve_Date"];" is needless. I also recommend using a try/finally, with the rd.Close() in the finally. And a using statement for the connection.

            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