get Datetime array through DataReader
-
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
-
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
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 aList
.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
-
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 aList
.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
-
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
-
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
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.