Click here to Skip to main content
16,022,752 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Developers,

I am creating a project i need to change the background colour of doctors available dates. i am try to done it.when i am run my code i got this type of error.



Line 235: if ((dr["appointmentdate"].ToString() != DBNull.Value.ToString()))
Line 236: {
Line 237: DateTime dtEvent=(DateTime)dr["appointmentdate"];
Line 238: if (dtEvent.Equals(e.Day.Date))
Line 239: {

My C# Code is below

protected void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
foreach (DataRow dr in dt.Rows)
{

if ((dr["appointmentdate"].ToString() != DBNull.Value.ToString()))
{
DateTime dtEvent=(DateTime)dr["appointmentdate"];
if (dtEvent.Equals(e.Day.Date))
{
e.Cell.BackColor = Color.PaleVioletRed;
}
}
}
}

So anyone please help me to how i am Correct the error and done this task.

Thanks with
Paul.S

What I have tried:

protected void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
if (e.Day.IsWeekend)
{
e.Cell.BackColor = System.Drawing.Color.Yellow;
e.Cell.ForeColor = System.Drawing.Color.Green;
}
DataTable dt = new DataTable();
string str = "SELECT * FROM tbl_OnlineChatCalendar";
OleDbCommand com = new OleDbCommand(str, con);
OleDbDataAdapter sqlda = new OleDbDataAdapter(com);
sqlda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{

if ((dr["appointmentdate"].ToString() != DBNull.Value.ToString()))
{
DateTime dtEvent=(DateTime)dr["appointmentdate"];
if (dtEvent.Equals(e.Day.Date))
{
e.Cell.BackColor = Color.PaleVioletRed;
}
}
}
}
Posted
Updated 5-Apr-16 21:35pm
Comments
Sergey Alexandrovich Kryukov 6-Apr-16 3:32am    
In what line? It it a cast to DateTime? What makes you thinking that it should cast? and what is the SQL type of this column, exactly?
—SA
Karthik_Mahalingam 6-Apr-16 4:57am    
wrtie a line string a = dr["appointmentdate"] + ""; before to the below line.
place a break point on below line and check what you are getting in the string object a
DateTime dtEvent=(DateTime)dr["appointmentdate"];

1 solution

Presumably - and we can;t check - the error is coming on this line:
C#
DateTime dtEvent=(DateTime)dr["appointmentdate"];

Since it is right in the middle of your "my error is here" bit. (In future, tell us exactly which line it is)
And teh only cast there is to DateTime - which implies that whatever you have in your appointmentdate column is not a type that can be cast to a DateTime value. And that probably means that the source column in your database is not a DATE or DATETIME column, since only those values will cast to DateTime.
So start with your DB, and look at your table definition. At a guess, you are storing dates in VARCHAR or NVARCHAR columns, and that is what is causing the problem.
If so, the solution is simple: store data in appropriate data types. DateTime values go in DATE or DATETIME, int32 values in INT, and so on. Storing things in strings may seem really easy - but it's a PITA to work with later!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900