Saturday, July 23, 2011

Display Events in Calendars from database in Aspnet

Hi friends,In this article i am going to explain the procedure of display evens in calendar from database in aspnet.Here we need to create two
forms ,one for adding events in the form and another one for displaying that events in the calendar and gridview.so lets start how to do this

First,we need to create a Entry form where we are going to enter the events

For this,Select a new Web form and change the name as per your requirment.Here i am using the default name as Default.aspx

NextAdd three textboxes,one button and one grdiview (See the Picture 1) in Default.aspx page
 
Next,Add the following code in Default.aspx.cs page
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;

public partial class add_events : System.Web.UI.Page
{
    SqlCommand com;
    SqlConnection con;
    SqlDataAdapter da;
    DataSet dsSelDate;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["SAMPLE_2"].ToString());
        con.Open();
        da = new SqlDataAdapter("select * from festival", con);
        da.Fill(ds, "Table");
    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["SAMPLE_2"].ToString());
        con.Open();
        com = new SqlCommand("insert into festival(FestivalName,Date,Description)values('" + txt_festival.Text + "','" + txt_date.Text + "','" + txt_description.Text + "')", con);
        com.ExecuteNonQuery();

    }
we are successfully created the Entry Form.Now we are going to create a calender display Form ,for displaying that events in the calender.


First, select a Next Web form and change the name as Events.aspx


Next ,Add the calender control and one gridview control(see the picture 2)



Next, add the following code Events.aspx.cs


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;

public partial class Events : System.Web.UI.Page
{
    SqlCommand com;
    SqlConnection con;
    SqlDataAdapter da;
    DataSet dsSelDate;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["SAMPLE_2"].ToString());
        con.Open();
        da = new SqlDataAdapter("select * from festival", con);
        da.Fill(ds, "Table");
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("select FestivalName,Description from festival where Date='" + Calendar1.SelectedDate.ToString() + "'", con);
        dsSelDate = new DataSet();
        da.Fill(dsSelDate, "AllTables");
        if (dsSelDate.Tables[0].Rows.Count == 0)
        {
            DataGrid1.Visible = false;
        }
        else
        {
            DataGrid1.Visible = true;
            DataGrid1.DataSource = dsSelDate;
            DataGrid1.DataBind();
        }
    }
   
 protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        string link = "";
        e.Cell.Text = e.Day.Date.Day.ToString();
        LiteralControl l = new LiteralControl();

        l.Text = e.Day.Date.Day.ToString() + "
";

        e.Cell.Controls.Add(l);
        // If the month is CurrentMonth
        if (!e.Day.IsOtherMonth)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if ((dr["Date"].ToString() != DBNull.Value.ToString()))
                {
                    DateTime dtEvent = (DateTime)dr["Date"];
                    if (dtEvent.Equals(e.Day.Date))
                    {
                        e.Cell.BackColor = Color.BurlyWood;
                        LinkButton lb = new LinkButton();

                        lb.Text = link + (int)dr["FestivalID"] + "'>" + dr["FestivalName"] as String + "
";

                        e.Cell.Controls.Add(lb);

                    }
                }
            }
        }
        //If the month is not CurrentMonth then hide the Dates
        else
        {
            e.Cell.Text = "";
        }
    }

Thats it..Now we can add events in the Event Entry form and display that events in the Calender..I am added the download link please download the full source code..if you have any doubts please feel free to ask me....Happy Coding
developercode
About the Author
Sayyad is a Software Engineer, Blogger and Founder of Developers Code from India.His articles mainly focus on .Net (Web and Windows based applications), Mobile Technologies (Android,IPhone,BlackBerry) and SEO.

Labels: , , , , , ,

6 Comments:

At August 8, 2011 at 3:14 PM , Anonymous Anonymous said...

online calculator

It's really great post.Thanks for sharing it.

 
At September 14, 2013 at 12:58 AM , Blogger Unknown said...

Hi
i got the Event Calendar for an ASP.NET MVC Application but i want same features to display in same manner by using asp.net web application. so plz give me t
Regards
B.Pradeep

 
At September 14, 2013 at 2:42 AM , Blogger Developers Code said...

Hi pradeep.

Could please tel me where you are getting issue exactly ..

 
At February 18, 2014 at 8:13 AM , Blogger Unknown said...

plz sir give me one solution like how to make a chat application

 
At February 18, 2014 at 8:15 AM , Blogger Unknown said...

plz tell me how to make a chat application

 
At February 20, 2014 at 4:20 PM , Blogger nandy said...

Hi Tanisha , article is really good, and i just want the same in MVC. do you have any idea in that.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home