Link to home
Start Free TrialLog in
Avatar of mrcoulson
mrcoulsonFlag for United States of America

asked on

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

Avatar of mrcoulson
mrcoulson
Flag of United States of America image

ASKER

foreach (DataRow calendarDataRow in calendarDataTable.Rows)
        {
           
        }


That's where I need help.  I should have specified.
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?
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!
try
e.Day == Convert.ToDateTime(calendarDataRow["meeting_datetime"]).Day
No joy.  But the datatype for meeting_datetime in the database is already datetime.
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
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'".
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Got it!

if (e.Day.Date.Date == Convert.ToDateTime(calendarDataRow["meeting_datetime"]).Date)
You rule.  Thanks so much!
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
Thanks.  I was completely blind going into this.