Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i develop event calender database table
is

EventId int
EventName varchar(50)
EventDate datetime

eventcalender.cs
C#
strConn="Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\omar\\Documents\\Visual Studio 2005\\WebSites\\Project2\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True";
mycn=new SqlConnection(strConn);
        myda=new SqlDataAdapter("Select* From Events",mycn);
        myda.Fill(ds,"Table");
    }

    protected void CalendarDRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
    {
        // If the month is CurrentMonth
        if (!e.Day.IsOtherMonth)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if ((dr["EventDate"].ToString() != DBNull.Value.ToString()))
                {
                    DateTime dtEvent = (DateTime)dr["EventDate"];
                    if (dtEvent.Equals(e.Day.Date))
                    {
                       e.Cell.BackColor=;
                    }
                }
            }
        }
        //If the month is not CurrentMonth then hide the Dates
        else
        {
            e.Cell.Text = "";
        }
    }

  protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
    {
        myda = new SqlDataAdapter("Select * from Events where EventDate='" +Calendar1.SelectedDate.ToString()+ "'", mycn);erroris here conversion of chardatatype result in an out of range datetime        dsSelDate = new DataSet();
        myda.Fill(dsSelDate, "AllTables");
        if (dsSelDate.Tables[0].Rows.Count == 0)
        {
            DataGrid1.Visible = false;
        }
        else
        {
            DataGrid1.Visible = true;
            DataGrid1.DataSource = dsSelDate;
            DataGrid1.DataBind();
        }
    }
Posted
Updated 30-Apr-11 2:56am
v2

1 solution

Try to change

myda = new SqlDataAdapter("Select * from Events where EventDate='" +Calendar1.SelectedDate.ToString()+ "'", mycn);


myda = new SqlDataAdapter("Select * from Events where EventDate='" +Calendar1.SelectedDate.ToString("s")+ "'", mycn);


Calendar1.SelectedDate.ToString() to Calendar1.SelectedDate.ToString("s");

The problem is that displaying a date/time and use it in a SQL statement is not the same. Displaying date/time with DateTime.ToString() uses the current CultureInfo for that thread/computer where the program runs.
 
Share this answer
 
Comments
Sandeep Mewara 30-Apr-11 12:16pm    
My 5!
Kim Togo 30-Apr-11 16:14pm    
Thanks Sandeep
Nish Nishant 30-Apr-11 12:35pm    
Good answer, my vote of 5!
Kim Togo 30-Apr-11 16:14pm    
Thanks Nishant

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