mrcoulson
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!
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)
{
}
}
ASKER
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?
{
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?
ASKER
I'm making progress, but I still need help!
foreach (DataRow calendarDataRow in calendarDataTable.Rows)
{
string strThisDay = e.Day.ToString();
string strEventDay = calendarDataRow["meeting_d atetime"]. 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. CalendarDa y. What am I missing? I feel so close!
foreach (DataRow calendarDataRow in calendarDataTable.Rows)
{
string strThisDay = e.Day.ToString();
string strEventDay = calendarDataRow["meeting_d
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.
try
e.Day == Convert.ToDateTime(calenda rDataRow[" meeting_da tetime"]). Day
e.Day == Convert.ToDateTime(calenda
ASKER
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(calenda rDataRow[" meeting_da tetime"])
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
did you get a datetime value from the casting
Convert.ToDateTime(calenda
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
ASKER
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(calenda rDataRow[" meeting_da tetime"]). 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 .CalendarD ay' and 'int'".
Is your code supposed to be the condition of the if statement?
Here's my new code:
if (e.Day == Convert.ToDateTime(calenda
{
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
Got it!
if (e.Day.Date.Date == Convert.ToDateTime(calenda rDataRow[" meeting_da tetime"]). Date)
if (e.Day.Date.Date == Convert.ToDateTime(calenda
ASKER
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
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
ASKER
Thanks. I was completely blind going into this.
ASKER
{
}
That's where I need help. I should have specified.