Hi,
I'm looking for a way of looping through the days in my calendar so that I can display some of them as bold depending on whether they match a database record. So far Ive tried the following, but it gives me an error. Any advice would be most appreciated.
Dim calendar1As Calendar = Page.FindControl("calendar1")For Each dayAs CalendarDayIn calendar1.daysNextInstead of trying to loop thru each day, use DayRender event and apply styling as each day is rendered.
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.calendar.dayrender.aspx
Thanks for your reply.
I don't think is the best way of going about what I'm trying to do. I'm making a database query to check if their are any related records. By checking the database every time a day is rendered is its putting a lot of strain on the server. In fact I just tried this out and it crashed the server.
I don't know what exactly is your comparison criteria, but you can pull the data before hand and put it in some data structure like a hashtable or a datatable and then compare values from this hashtable or datatable instead of making a trip to the database in DayRender event.
Thanks again for your reply. Ive tried creating a datatable to reference to, but Ive got some errors. Where have I gone wrong? Thanks.
Function dttable()As DataTableDim dtAs New DataTable()Dim myConnectionAs New SqlConnection(ConfigurationManager.AppSettings("conString"))Const strSQLAs String ="select [date] from EventsDates"Dim myCommandAs New SqlCommand(strSQL, myConnection)Dim myAdapterAs New SqlDataAdapter(myCommand) myAdapter.Fill(dt)Return dtEnd Function Sub dayrender(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.DayRenderEventArgs)If dttable.Rows.Contains(e.Day.Date) =True Then e.Cell.ForeColor = Color.FromArgb(162, 162, 162)End If End SubWhat were the errors?
bullpit:
What were the errors?
Table doesn't have a primary key.
Hi CurtWRC,
Based on my understanding, you want to retrieve the date from database and store the data in the DataTable. When you use the Contains method with your Table, you get the error message above. If I have misunderstood you, please feel free to let me know.
The Contains method is used to check if the primary key of the row in the collection contains the value specified. We can create the primary key to your table. For example:
Function dttable()As Data.DataTableDim dtAs New Data.DataTable()Dim myConnectionAs New Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("conString"))Const strSQLAs String ="selectDISTINCT([date]) from EventsDates"Dim myCommandAs New Data.SqlClient.SqlCommand(strSQL, myConnection)Dim myAdapterAs New Data.SqlClient.SqlDataAdapter(myCommand) myAdapter.Fill(dt)Dim keysAs Data.DataColumn() =New Data.DataColumn(0) {} keys(0) = dt.Columns(0) dt.PrimaryKey = keysReturn dtEnd Function Besides, the primary key is unique, so we can use theDISTINCT in the Sql statement.
I hope this helps.
Thanks Thomas Sun, thats exactly what I was after and has solved my problem!
0 comments:
Post a Comment