• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 547
  • Last Modified:

What does my dayRender event need to do for ASP.Net calendar databind?

I am attempting to bind an ASP calendar to a database.  So far I am successfully styling the calendar and getting data from the database into a gridview for testing, but getting the data to the calendar is not going as well.

In a dayRender event, I have code that gets data from the database, puts it in a gridview, styles the calendar based on some random stuff, and then fails to style the calendar based on something in the database.  I can't figure out how to do this step, so I turn to the experts!
void DayRender(Object source, DayRenderEventArgs e)
    {
 
        System.Data.SqlClient.SqlConnection calendarConnection = default(System.Data.SqlClient.SqlConnection);
        System.Data.SqlClient.SqlDataAdapter calendarAdapter = default(System.Data.SqlClient.SqlDataAdapter);
        DataTable calendarDataTable = new DataTable();
        // DataRow calendarDataRow = default(DataRow);
        string strSQL = "SELECT * FROM tblCalendar";
 
        string calendarString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["planning_calendarConnectionString"].ConnectionString;
        calendarConnection = new System.Data.SqlClient.SqlConnection(calendarString);
        calendarConnection.Open();
        calendarAdapter = new System.Data.SqlClient.SqlDataAdapter(strSQL, calendarConnection);
        calendarAdapter.Fill(calendarDataTable);
        GridView1.DataSource = calendarDataTable;
        GridView1.DataBind();
        
        e.Cell.BorderStyle = BorderStyle.Solid;
        e.Cell.BorderWidth = 1;
 
        if (e.Day.IsOtherMonth)
        {
            e.Cell.Controls.Clear();
        }    
        
        // Change the background color of the days in the month
        // to yellow.
        if (!e.Day.IsOtherMonth && !e.Day.IsWeekend)
        {
            e.Cell.BackColor = System.Drawing.Color.Yellow;
        }
 
        // Add custom text to cell in the Calendar control.
        if (e.Day.Date.Day == 16)
        {
            e.Cell.Controls.Add(new LiteralControl("<br />Holiday"));
        }
 
        foreach (DataRow calendarDataRow in calendarDataTable.Rows)
        {
            
        }
    }

Open in new window

0
mrcoulson
Asked:
mrcoulson
  • 9
  • 4
1 Solution
 
mrcoulsonAuthor Commented:
foreach (DataRow calendarDataRow in calendarDataTable.Rows)
        {
           
        }


That's where I need help.  I should have specified.
0
 
mrcoulsonAuthor Commented:
foreach (DataRow calendarDataRow in calendarDataTable.Rows)
        {
            string strThisDay = e.Day.ToString();
            if (strThisDay == calendarDataRow[0])
            {
                e.Cell.BackColor = System.Drawing.Color.Green;
            }
        }

Maybe this does not work because I'm trying to compare a string to a datetime?
0
 
mrcoulsonAuthor Commented:
I'm making progress, but I still need help!

foreach (DataRow calendarDataRow in calendarDataTable.Rows)
        {
            string strThisDay = e.Day.ToString();
            string strEventDay = calendarDataRow["meeting_datetime"].ToString();
            if (strThisDay == strEventDay)
            {
                e.Cell.BackColor = System.Drawing.Color.Green;
            }
            lblTester.Text = strEventDay;
            lblTester2.Text = strThisDay;
        }

strEventDay gives me a string that looks like a date, but strThisDay just gives me System.Web.UI.WebControls.CalendarDay.  What am I missing?  I feel so close!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Anurag ThakurCommented:
try
e.Day == Convert.ToDateTime(calendarDataRow["meeting_datetime"]).Day
0
 
mrcoulsonAuthor Commented:
No joy.  But the datatype for meeting_datetime in the database is already datetime.
0
 
Anurag ThakurCommented:
ok but still when you bring it in a datatable its brought to you as object and to use it you have to convert it back to date time which i tried to do and after converting get the day from the date which you are trying to do
did you get a datetime value from the casting
Convert.ToDateTime(calendarDataRow["meeting_datetime"])

what is the value in the column and what is the actual value.... it might be the case that the conversion is failing becasue we are not using the culture of the thread
0
 
mrcoulsonAuthor Commented:
The value in the column is 5/8/2009 12:35:29 PM.

Is your code supposed to be the condition of the if statement?  

Here's my new code:

if (e.Day == Convert.ToDateTime(calendarDataRow["meeting_datetime"]).Day)
            {
                e.Cell.BackColor = System.Drawing.Color.Green;
            }

With this, however, I'm told "Operator '==' cannot be applied to operands of type 'System.Web.UI.WebControls.CalendarDay' and 'int'".
0
 
Anurag ThakurCommented:
try this
if (e.Day.Date.Day == Convert.ToDateTime(calendarDataRow["meeting_datetime"]).Day)
{
    //logic here
}
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.calendar.dayrender.aspx
0
 
mrcoulsonAuthor Commented:
Yeah!  Now we're getting somewhere!

So, now it's making the 8th of every month green.  How can I get just the date and not that day?

Thanks for the help so far and for any you'll offer for the rest of this.  I got myself in over my head again.
0
 
mrcoulsonAuthor Commented:
Got it!

if (e.Day.Date.Date == Convert.ToDateTime(calendarDataRow["meeting_datetime"]).Date)
0
 
mrcoulsonAuthor Commented:
You rule.  Thanks so much!
0
 
Anurag ThakurCommented:
i did not get your question at all
there are many decisions that can be taken in the day render event of the calendar control
http://www.java2s.com/Code/ASP/Asp-Control/CalendarcontrolincodebehindC.htm is a good tutorial to start with
0
 
mrcoulsonAuthor Commented:
Thanks.  I was completely blind going into this.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now