Monday 1 February 2016

''Birthdays Today'' Code for Social Media Websites


Description:    ...



UI (.aspx) (ASP.Net+HTML)

<h2 style="text-align: center">Birthdays</h2>
<asp:DataList runat="server" ID="BirthdayDataList">
    <ItemTemplate>
   
        <asp:LinkButton ID="BD_Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"First_Name") %>'></asp:LinkButton>
                           
    </ItemTemplate>
</asp:DataList>

CodeBehind (.aspx.cs) (C#)


protected void HaZaTimer_Tick(object sender, EventArgs e)
{
    DataTable data = new DataTable();
    myDAL obj = new myDAL();

    //Get the User ID of the logged-in user from the Session
    string uid = Session["userID"] as string;      

    data = obj.LOADBIRTHDAYS(uid);
    BirthdayDataList.DataSource = data;
    BirthdayDataList.DataBind();
}

DAL-Data Access Layer (C#)


public DataTable LOADBIRTHDAYS(string id)
{
    DataTable d = new DataTable();
    SqlConnection con = new SqlConnection(connString); //connString should be defined
    con.Open();
    SqlCommand cmd;
    try
    {
        cmd = new SqlCommand("LoadBirthdays", con); //name of your Stored Procedure
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@email", SqlDbType.NVarChar, 50).Value = id.ToString();
        d.Load(cmd.ExecuteReader());
    }
    catch (SqlException ex)
    {
        Console.WriteLine("SQL Error" + ex.Message.ToString());
    }
    finally
    {
        con.Close();
    }

    return d;
}

Stored Procedure (SQL)

NOTE: In our DB, the three components of Date of Birth (i.e. Day, Month and Year) were stored as 3 different entities named DOB_Day, DOB_Month and DOB_Year respectively

create procedure [dbo].[LoadBirthdays](@email nvarchar(50) )
as
       declare @id int
       select @id=UserID
       from User_info
       where Email_ID=@email

       declare @i int
       declare @month varchar(30)
       SET @i=month(getdate())
       if(@i=1)
              SET @month='January'
       else if(@i=2)
              SET @month='February'
       else if(@i=3)
              SET @month='March'
       else if(@i=4)
              SET @month='April'
       else if(@i=5)
              SET @month='May'
       else if(@i=6)
              SET @month='June'
       else if(@i=7)
              SET @month='July'
       else if(@i=8)
              SET @month='August'
       else if(@i=9)
              SET @month='September'
       else if(@i=10)
              SET @month='October'
       else if(@i=11)
              SET @month='November'
       else if(@i=12)
              SET @month='December'
      
       print @month

select User_Info.First_Name
from User_Info inner join Friends on User_Info.UserID=Friends.FriendID
where Friends.UserID=@id AND User_Info.DOB_Day=day(getdate()) AND User_Info.DOB_Month=@month


No comments

Post a Comment

Recent Posts