We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Thursday, May 28, 2015

Example of Calendar control and retrieve birthday date from database and display on Calendar control and when click birthday date the displayed details of the person(s).


Step1:

->Open SQL Management studio
->Create a database(Ex:Sampledatabase)
->Create a table(Ex:Contact)
see below

->Insert some record manually

see below

Step2:
->Add an aspx page(New->Website and give any name Ex: Calendar.aspx),
->Add a web form,
->Create a folder and Add some birthday images in a folder.

Step3:
->Go to web.config,
->And set the connection.

see below

<connectionStrings>

<add name="conStr" connectionString="Server=mithilesh;Database=sampledatabase; User Id=sa; password=12345" providerName="System.Data.SqlClient" />

</connectionStrings>

Step4:

->Go to Default.aspx page(design page)
->Add a calendar control and a grid view control
->Write code

see below
<div align="center">

<h2 style="text-align:center;border:2px solid aqua;background-color:green;color:white">Birthday Calendar View</h2>

<asp:Calendar ID="Calendar1" runat="server" ShowGridLines="True" Width="850px" OnDayRender="Calendar1_DayRender" OnSelectionChanged="Calendar1_SelectionChanged" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="3px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="12pt" ForeColor="#663399" Height="400px">

<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />

<NextPrevStyle Font-Size="19pt" ForeColor="#FFFFCC" />

<OtherMonthDayStyle ForeColor="#CC9966" />

<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />

<SelectorStyle BackColor="#FFCC66" />

<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="19pt" ForeColor="#FFFFCC" />

<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
</asp:Calendar>

       <br />
<asp:GridView ID="GridView1" runat="server" Caption="Birthday Event Details" Width="700px" AutoGenerateColumns="false" CellPadding="5" ForeColor="#333333" GridLines="Both">

<Columns>
<asp:BoundField HeaderText="Id" DataField="Id" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" />

<asp:BoundField HeaderText="Last Name" DataField="LastName" />

<asp:BoundField HeaderText="Date of Birth" DataField="DateOfBirth" />

<asp:TemplateField>

<ItemTemplate>

<asp:LinkButton ID="btnclick" runat="server" Text='<%#Eval("EmailId") %>'></asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

  <asp:BoundField HeaderText="Mobile No" DataField="MobileNo" />

</Columns>

<HeaderStyle BackColor="#990000" Font-Bold="true" ForeColor="White" />

<RowStyle BackColor="#FFFBd6" ForeColor="#333333" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

 </div>

->Go to code behind page(Deafault.aspx.page)
->And write code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net.Mail;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

public partial class CalanderExam2 : System.Web.UI.Page

{
SqlConnection con = null;

SqlDataAdapter da = null;

DataSet ds = null;

string strSqlCommand = string.Empty;

protected void Page_Load(object sender, EventArgs e)

{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);

}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)

{
strSqlCommand = "Select * from Contacts";

da = new SqlDataAdapter(strSqlCommand, con);

ds = new DataSet();

da.Fill(ds, "Contacts");

DataTable dt = ds.Tables["Contacts"];

DataRowCollection drc = dt.Rows;

if (drc.Count > 0)

{
Literal literal1 = new Literal();

literal1.Text = "<br/>";

e.Cell.Controls.Add(literal1);
foreach (DataRow dr in drc)

{
DateTime dtDob = Convert.ToDateTime(dr["DateOfBirth"]);

if (e.Day.Date.Day == dtDob.Day && e.Day.Date.Month == dtDob.Month)
{
e.Cell.BackColor = System.Drawing.Color.Yellow;

e.Cell.ForeColor = System.Drawing.Color.Red;

e.Cell.ToolTip = "Birthday Events";

Image img1 = new Image();

img1.ImageUrl = "image/nuts_cake_2561.bmp";

img1.ToolTip = dr["FirstName"].ToString();

e.Cell.Controls.Add(img1);

}

}

}

}
 
protected void Calendar1_SelectionChanged(object sender, EventArgs e)

{
 
DateTime dtSeleted = Calendar1.SelectedDate;

string strDate = dtSeleted.ToString("dd-MMM");

strSqlCommand = "Select * from Contacts Where substring(DateOfBirth,1,6)='" + strDate + "'";

da = new SqlDataAdapter(strSqlCommand, con);

da.SelectCommand.CommandType = CommandType.Text;

ds = new DataSet();

da.Fill(ds, "Contacts");

GridView1.DataSource = ds.Tables["Contacts"];

GridView1.DataBind();
 
GridView1.Caption = "<h3 style='color:green'>Birthday Even Details Of Selected Date i.e" + strDate + "</he>";
}
}
}
 Step5:
->Run the program
->See the out put


 ->click the birthday date and see the output


 


No comments: