How to show today birthday list/notification in grid view in asp.net c#

In this article, I will show how to today show birthday list in grid view in c#, I will fetch the value from Sql Server and bind the grid view.
Data base (SQL Server):-

Design of table:-







Script of the creating table:-
create database CodeSolution

USE [CodeSolution]
GO

/****** Object:  Table [dbo].[reg]    Script Date: 01/07/2015 00:15:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[reg](
          [id] [int] IDENTITY(1,1) NOT NULL,
          [Name] [nvarchar](50) NULL,
          [Gender] [nvarchar](50) NULL,
          [Brithday] [datetime] NULL,
 CONSTRAINT [PK_reg] PRIMARY KEY CLUSTERED
(
          [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Data of the table:-



Source code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="brithdaynotification.aspx.cs" Inherits="brithdaynotification" %>

<!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>Brithday notification in gridview from database</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="grdbirthday" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        DataKeyNames="ID" Width="350px" GridLines="None" Font-Names="Times New Roman"
                        ForeColor="#333333" AllowPaging="True" RowStyle-HorizontalAlign="Center" >
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                           
                            <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" ItemStyle-Width="50px" />
                            <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender"
                                ItemStyle-Width="50px" />
                            <asp:BoundField DataField="Brithday" HeaderText="Brithday" SortExpression="Brithday"  DataFormatString="{0:d}"
                                ItemStyle-Width="50px" />
                                                 </Columns>
                        <EditRowStyle BackColor="#7C6F57" />
                        <FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
                        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                        <PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#666666" />
                        <RowStyle BackColor="#E3EAEB" />
                        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F8FAFA" />
                        <SortedAscendingHeaderStyle BackColor="#246B61" />
                        <SortedDescendingCellStyle BackColor="#D4DFE1" />
                        <SortedDescendingHeaderStyle BackColor="#15524A" />
                    </asp:GridView>
    </div>
    </form>
</body>
</html>


Code behind (C#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class brithdaynotification : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        birthday();


    }
    protected void birthday()
    {
       
        SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from reg where DATEPART(m, Brithday)=DATEPART(m,GETDATE()) AND DATEPART(d, Brithday) = DATEPART(d, GETDATE()) ", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        grdbirthday.DataSource = ds;
        grdbirthday.DataBind();
    }
}

Out-Put:-



How to show today birthday list/notification in grid view in asp.net c# How to show today birthday list/notification in grid view in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 12:41:00 PM Rating: 5

No comments:

Powered by Blogger.