Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my Default.aspx page code:
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
<asp:Calendar id="Calendar1" OnDayRender="CalendarDRender" runat="server" BorderWidth="1px" NextPrevFormat="FullMonth" BackColor="White" Width="350px" ForeColor="Black" Height="190px" Font-Size="9pt" Font-Names="Verdana" BorderColor="White">
<todaydaystyle backcolor="#CCCCCC"></todaydaystyle>
<nextprevstyle font-size="8pt" font-bold="True" forecolor="#333333" verticalalign="Bottom"></nextprevstyle>
<dayheaderstyle font-size="8pt" font-bold="True"></dayheaderstyle>
<SelectedDayStyle ForeColor="White" BackColor="#333399"></SelectedDayStyle>
<TitleStyle Font-Size="12pt" Font-Bold="True" BorderWidth="4px" ForeColor="#333399" BorderColor="Black" BackColor="White"></TitleStyle>
<othermonthdaystyle forecolor="#999999"></othermonthdaystyle>

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 23px; POSITION: absolute; TOP: 271px" runat="server" Font-Size="XX-Small" Font-Names="Verdana" Visible="False">    
    </div>
    </form>
</body>
</html>

Calendar and datagrid are perfectly shown.

Here is default.aspx.cs file:

C#
using System;
using System.Drawing;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    OleDbConnection mycn;
    OleDbDataAdapter myda;
    DataSet ds = new DataSet();
    DataSet dsSelDate;
    string strConn;
    protected void Page_Load(object sender, EventArgs e)
    {
        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb";
        mycn = new OleDbConnection(strConn);
        myda = new OleDbDataAdapter("Select * FROM EventsTable", mycn);
        myda.Fill(ds, "EventsTable");
    }
    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 = Color.PaleVioletRed;
                    }
                }
            }
        }
        //If the month is not CurrentMonth then hide the Dates
        else
        {
            e.Cell.Text = "";
        }
    }
    private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {
        myda = new OleDbDataAdapter("Select * from EventsTable where EventDate='" + Calendar1.SelectedDate.ToString() + "'", mycn);
        dsSelDate = new DataSet();
        myda.Fill(dsSelDate, "EventsTable");
        if (dsSelDate.Tables[0].Rows.Count == 0)
        {
            DataGrid1.Visible = false;
        }
        else
        {
            DataGrid1.Visible = true;
            DataGrid1.DataSource = dsSelDate;
            DataGrid1.DataBind();
        }
    }
}


Everything is working properly but when Calendar1_SelectionChanged event is fired the datagrid is not shown on the browser.
Can anyone help me to solve this problem.
Thanks in advance
Posted
Updated 27-Jul-11 23:48pm
v2

1 solution

Its obvious that your data table doesn't get populated with records. Perhaps something is wrong with your query, that's why it did not return any result. Try setting a breakpoint on the Calendar1.SelectedDate.ToString() part, get its value, and then verify through the database if the value is really valid to use as a filter.
 
Share this answer
 
Comments
sahabiswarup 28-Jul-11 6:01am    
can you give me some example.
walterhevedeich 28-Jul-11 6:09am    
Ehhrrrmmmm, are you familiar with using breakpoints? I suspect your Calendar1.SelectedDate.ToString() isn't returning a value, hence, the error. There are tons of tutorials on how to use a breakpoint and watches. Go on an take a look. :)
sahabiswarup 28-Jul-11 6:33am    
After setting the break point i make some changes in my code..

protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
try
{
myda = new OleDbDataAdapter("Select * from EventsTable where EventDate='" + Calendar1.SelectedDate.ToString() + "'", mycn);
OleDbCommandBuilder cmd = new OleDbCommandBuilder(myda);
//dsSelDate = new DataSet();
DataTable dt = new DataTable();
myda.Fill(dt);
myda.Update(dt);
//myda.Fill(dsSelDate, "EventsTable");
if (dsSelDate.Tables[0].Rows.Count == 0)
{
DataGrid1.Visible = false;
Response.Write("if");
}
else
{
DataGrid1.Visible = true;
DataGrid1.DataSource = dsSelDate;
DataGrid1.DataBind();
Response.Write("else");
}
}
catch
{
Response.Write("catch block");
DataGrid1.Visible = true;
}
}

But still the datagrid is not populated; every time it enters into the catch block.
walterhevedeich 28-Jul-11 19:41pm    
Catch the exception by using catch(Exception ex) instead of catch. And then set a breakpoint below the catch, check the value of ex.Message.

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