how to highlight dates of Month Calendar from database windows form
-
Dim DateAsString As New ArrayList this is the part i am using in form load to get all the dates for the Meeting. private sub getalldate() Dim connectionString As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MyDB;Data Source=192.168.0.8" Dim connection As SqlConnection = New SqlConnection(connectionString) connection.Open() Dim sql As String = "SELECT MeetingDate FROM Meetings" Dim command As SqlCommand = New SqlCommand(sql, connection) Dim reader As SqlDataReader = command.ExecuteReader() If (reader.Read()) Then If (reader.HasRows) Then While reader.Read() dateasstring.Add("#" & reader(0) & "#") End While End If End If reader.Close() connection.Close() end sub private sub makedatesboldincalendar() Dim num As Date Dim dtArrSpecialDates() As Date For Each num In dateasstring dtArrSpecialDates = {num} MonthCalendar1.BoldedDates = dtArrSpecialDates Next end sub what happens is it makes date bold but only the last date it is getting from database. there are more than 10 entries in the Meetings table. so, how can i highligh all the dates i am getting from database?
-
Dim DateAsString As New ArrayList this is the part i am using in form load to get all the dates for the Meeting. private sub getalldate() Dim connectionString As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MyDB;Data Source=192.168.0.8" Dim connection As SqlConnection = New SqlConnection(connectionString) connection.Open() Dim sql As String = "SELECT MeetingDate FROM Meetings" Dim command As SqlCommand = New SqlCommand(sql, connection) Dim reader As SqlDataReader = command.ExecuteReader() If (reader.Read()) Then If (reader.HasRows) Then While reader.Read() dateasstring.Add("#" & reader(0) & "#") End While End If End If reader.Close() connection.Close() end sub private sub makedatesboldincalendar() Dim num As Date Dim dtArrSpecialDates() As Date For Each num In dateasstring dtArrSpecialDates = {num} MonthCalendar1.BoldedDates = dtArrSpecialDates Next end sub what happens is it makes date bold but only the last date it is getting from database. there are more than 10 entries in the Meetings table. so, how can i highligh all the dates i am getting from database?
-
The first port of call should be MonthCalendar.BoldedDates Property (System.Windows.Forms)[^] where it explains that each call to
BoldedDates
will clear any previously set. -
Dim DateAsString As New ArrayList this is the part i am using in form load to get all the dates for the Meeting. private sub getalldate() Dim connectionString As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MyDB;Data Source=192.168.0.8" Dim connection As SqlConnection = New SqlConnection(connectionString) connection.Open() Dim sql As String = "SELECT MeetingDate FROM Meetings" Dim command As SqlCommand = New SqlCommand(sql, connection) Dim reader As SqlDataReader = command.ExecuteReader() If (reader.Read()) Then If (reader.HasRows) Then While reader.Read() dateasstring.Add("#" & reader(0) & "#") End While End If End If reader.Close() connection.Close() end sub private sub makedatesboldincalendar() Dim num As Date Dim dtArrSpecialDates() As Date For Each num In dateasstring dtArrSpecialDates = {num} MonthCalendar1.BoldedDates = dtArrSpecialDates Next end sub what happens is it makes date bold but only the last date it is getting from database. there are more than 10 entries in the Meetings table. so, how can i highligh all the dates i am getting from database?
Don't use
ArrayList
; useList<DateTime>
. Don't convert the dates to strings; keep them asDateTime
. Don't loop over the array setting theBoldedDates
for each element; just set theBoldedDates
to the list of dates. Don't call.Read
before testing whether the data reader has any rows; you're skipping the first row.Private AllDates As DateTime()
Private Sub GetAllDate()
'TODO: Load the connection string from the configuration file
Const connectionString As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MyDB;Data Source=192.168.0.8"
Const sql As String = "SELECT MeetingDate FROM Meetings"Using connection As New SqlConnection(connectionString) connection.Open() Using command As New SqlCommand(sql, connection) Using reader As SqlDataReader = command.ExecuteReader() Dim dates As New List(Of DateTime)() While reader.Read() dates.Add(reader.GetDateTime(0)) End While AllDates = dates.ToArray() End Using End Using End Using
End Sub
Private Sub MakeDatesBoldInCalendar()
MonthCalendar1.BoldedDates = AllDates
End Sub
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Don't use
ArrayList
; useList<DateTime>
. Don't convert the dates to strings; keep them asDateTime
. Don't loop over the array setting theBoldedDates
for each element; just set theBoldedDates
to the list of dates. Don't call.Read
before testing whether the data reader has any rows; you're skipping the first row.Private AllDates As DateTime()
Private Sub GetAllDate()
'TODO: Load the connection string from the configuration file
Const connectionString As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MyDB;Data Source=192.168.0.8"
Const sql As String = "SELECT MeetingDate FROM Meetings"Using connection As New SqlConnection(connectionString) connection.Open() Using command As New SqlCommand(sql, connection) Using reader As SqlDataReader = command.ExecuteReader() Dim dates As New List(Of DateTime)() While reader.Read() dates.Add(reader.GetDateTime(0)) End While AllDates = dates.ToArray() End Using End Using End Using
End Sub
Private Sub MakeDatesBoldInCalendar()
MonthCalendar1.BoldedDates = AllDates
End Sub
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
i have managed to do it. but really appreciate that you have explained so well and gave code too. hopefully some other user will find it useful. thank you.
Psst to thank him properly up vote the answer - arrows appear top left of the answer box.
Never underestimate the power of human stupidity RAH
-
Psst to thank him properly up vote the answer - arrows appear top left of the answer box.
Never underestimate the power of human stupidity RAH