Add Events in the Calendar with the help of ASP.NET

In this article, we will discuss about the calendar control in ASP.Net. Here we add the Birthaday, Anniversary and Meetings in the particular date of the Calendar. For this Follow these steps:

Step 1: First we create a table in database like this:

Create table AddOccasion
(
Occasion varchar(20),
Date DateTime,
Title varchar(60),
DateOfTheday int
)

Step 2: Now we take a Calendar Control in out .aspx page:

<asp:Calendar ID="Calendar1" runat="server"
></asp:Calendar>

Step 3: After That we take Three Buttons ( one for add the BirthDay, one for add the Anniversary and one for add the meeting).

<asp:Button ID="btnBirthDay" runat="server" Text="Add Birthday"
        Width="84px" />
    &nbsp;<asp:Button ID="btnAnniversary" runat="server" Text="Add Anniversary"  <asp:Button ID="btnBirthDay" runat="server" Text="Add Birthday"
        onclick="btnBirthDay_Click" Width="84px" />
    &nbsp;<asp:Button ID="btnAnniversary" runat="server" Text="Add Anniversary"
        onclick="btnAnniversary_Click" />
    &nbsp;<asp:Button ID="btnmeeting" runat="server" Text="Add Meeting" onclick="btnmeeting_Click"
        />
            />
    &nbsp;<asp:Button ID="btnmeeting" runat="server" Text="Add Meeting"        />



Here we take a TextBox which is helpful for save the data. And set its visible property to false:

<asp:TextBox ID="TextBox1" runat="server" Visible="false"></asp:TextBox>

Here is the Output:

ClndrASP1.jpg

Step 4: After that we add another page in our project for add the Occasion ( Add Occasion.aspx)

Step 5: After that we add the controls in the (Add occasion.aspx) page like this:

  <tr>
                <td class="style2">
                    Occasion</td>

                <td>
                    <asp:TextBox ID="txtOccasion" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Date</td>
                <td>
                    <asp:TextBox ID="txtdate" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Title</td>
                <td>
                    <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="btnAdd" runat="server" Text="Add" Width="64px"
/>
                    &nbsp;<asp:Button ID="btnCancel" runat="server" Text="Cancel"
/>
                </td>
            </tr>

The Output Will Be:

ClndrASP2.jpg

Step 6: Now we write the code for the Add BirthDay in the Default.aspx page:

protected void btnBirthDay_Click(object sender, EventArgs e)
{
    String a = Calendar1.SelectedDate.Date.Date.Day.ToString();
    Response.Write(a);
    Int32 b = Convert.ToInt32(a);

    Response.Redirect("Add Occasion.aspx?Occasion=" + "Birthday"+"&DateOfTheDay="+a + "&OnDate=" + Calendar1.SelectedDate.Date.ToShortDateString());
}

Here we transfer the data on the Add Occasion.aspx page in the form of the querystring. Here we take the date of the day, which will be helpful to fetch the data.

Step 7: After that we select the date on the calendar and click the Add Birthday Button. The Output Will Be:

ClndrASP3.jpg

public Int32 a;
    protected void Page_Load(object sender, EventArgs e)
    {
        txtdate.Text = Request.QueryString["OnDate"];
        txtOccasion.Text = Request.QueryString["Occasion"];
        a = Convert.ToInt32(Request.QueryString["DateOfTheday"]);
    }

Step 8: Now we write the code for the Add button:

protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection conn1 = new SqlConnection("Data Source=localhost;Initial
Catalog=calendar;Integrated Security=SSPI"
);
conn1.Open();
SqlCommand sa1=new SqlCommand ("insert into AddOccasion(Occasion,Date,Title,DateOfTheday) values('" + txtOccasion.Text + "','" + txtdate.Text + "','" + txtTitle.Text + "','"+a+"')", conn1);
    sa1.ExecuteNonQuery();
    conn1.Close();
    Response.Redirect("Default.aspx");
}

Here we save the data in the AddOccasion table. And Redirect the page to the Default.aspx.

Step 9: After that we write the code for Add Anniversary and Add Meeting Button in the Default.aspx page:

protected void btnBirthDay_Click(object sender, EventArgs e)
{
    String a = Calendar1.SelectedDate.Date.Date.Day.ToString();
    Response.Write(a);
    Int32 b = Convert.ToInt32(a);

    Response.Redirect("Add Occasion.aspx?Occasion=" + "Birthday"+"&DateOfTheDay="+a + "&OnDate=" + Calendar1.SelectedDate.Date.ToShortDateString());
}
protected void btnAnniversary_Click(object sender, EventArgs e)
{
    String a = Calendar1.SelectedDate.Date.Date.Day.ToString();
    Response.Write(a);
    Int32 b = Convert.ToInt32(a);

    Response.Redirect("Add Occasion.aspx?Occasion=" + "Anniversary" + "&DateOfTheDay=" + a + "&OnDate=" + Calendar1.SelectedDate.Date.ToShortDateString());
}
protected void btnmeeting_Click(object sender, EventArgs e)
{
    String a = Calendar1.SelectedDate.Date.Date.Day.ToString();
    Response.Write(a);
    Int32 b = Convert.ToInt32(a);

    Response.Redirect("Add Occasion.aspx?Occasion=" + "Meeting" + "&DateOfTheDay=" + a + "&OnDate=" + Calendar1.SelectedDate.Date.ToShortDateString());
}

And add the data in the Database. Which we can add in Step7.

Step 10: Now we write the code for the Calendar event.

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
String a = Calendar1.SelectedDate.Date.Date.Day.ToString();
Response.Write(a);
Int32 b = Convert.ToInt32(a);

SqlConnection conn1 = new SqlConnection("Data Source=localhost;Initial Catalog=calendar;Integrated Security=SSPI");
  conn1.Open();
  SqlCommand sq1 = new SqlCommand("select * from AddOccasion", conn1);
 SqlDataReader dr1 = sq1.ExecuteReader();
if(dr1.Read())
 {
     TextBox1.Text = Calendar1.SelectedDate.Date.ToShortDateString();
     conn1.Close();
     dr1.Close();
     conn1.Open();
     SqlCommand sq2 = new SqlCommand("select Occasion,Title from AddOccasion where Date='"+TextBox1.Text+"'", conn1);
       SqlDataReader dr2 = sq2.ExecuteReader();
       GridView gv = new GridView();
       gv.DataSource = dr2;
       gv.DataBind();
       form1.Controls.Add(gv);
   }
}

Here we add a Gridview to show the data. By this when we click on the Date the Data for this date (BirthDay, Anniversary, Meeting) will be show in the GridView Like this:

ClndrASP4.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all