Showing posts with label Facebook. Show all posts
Showing posts with label Facebook. Show all posts

How to Get User Posts on Social Networking Website

Get Posts from user on Social networking website

  1. Every Visitor on social netoworking website should be able to see the posts that are posted by him 
  2. The recent posts should be available at the top and the old posts should be available at last 
  3. To Get the latest updates and posts we have created a user control which we will drag and drop on main .aspx page and on that page user will be able to see the latest updates 

Code for Main.aspx 

<%@ Page Title="" Language="C#" MasterPageFile="~/ProfileMaster.master" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main"  EnableEventValidation="false"%>
<%@ Register src="Controls/LatestUpdates.ascx" tagname="LatestUpdates" tagprefix="uc1" %>

     

Here we have dragged and dropped latestupdates.ascx user control on main.aspx page 

Code for Main.aspx.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
}

Social Networking like Facebook Profile Page in asp.net c#

Social Networking facebook website Source Code Open Soure Project Application Profile Page 

  1. The User Controls like Header, Profile Picture,Friend Suggestions are dragged and dropped onto the ProfileMaster.master master page 
  2. we can say that this master page is holding all the usercontrols and keeping them togather so that it can look like facebook 
  3. so let us now take a look at the code and asp source of ProfileMaster page 


ProfileMaster.master Code:

  1. Open ProfileMaster.master MasterPage Which You have created in step 1.
  2. In the Source of ProfileMaster.master you can add the following piece of code given below.
  3. On ProfileMaster.master page we have made a struture some what similar to facebook.
  4. We have placed all the user controls into a tabular structure such that it resembles the tabular structure of social networking website like facebook 
  5. On the footer we have written the copyright notice to 20Fingers2Brains
  6. The snapshot of the profilemaster.master page is shown above
  7. The source code of the profile master page is shown below 
Source Code:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="ProfileMaster.master.cs"
    Inherits="ProfileMaster" %>

<%@ Register Src="Controls/Header.ascx" TagName="Header" TagPrefix="uc1" %>
<%@ Register Src="Controls/ProfilePic.ascx" TagName="ProfilePic" TagPrefix="uc2" %>
<%@ Register Src="Controls/PeopleYouMayKnow.ascx" TagName="PeopleYouMayKnow" TagPrefix="uc3" %>
<!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></title>
    <style type="text/css">
        .style2
        {
            width: 100%;
        }
        
        .style3
        {
            width: 900px;
        }
        .style4
        {
            width: 150px;
            vertical-align: top;
            text-align: center;
            padding-top: 7px;
        }
        .style5
        {
            width: 600px;
            vertical-align: top;
        }
        .style6
        {
            width: 120px;
            vertical-align: top;
        }
    </style>
</head>
<body>
    <form runat="server" id="MainForm">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <table cellpadding="0" cellspacing="0" class="style2">
        <tr>
            <td align="center" style="border: 1px ridge #CCCCCC; height: 35px; background-color: #0066CC;">
                <table align="center" cellpadding="0" cellspacing="0" class="style3">
                    <tr>
                        <td>
                            <uc1:Header ID="Header1" runat="server" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td align="center">
                <table align="center" cellpadding="0" cellspacing="0" class="style3" style="border: 1px ridge #CCCCCC">
                    <tr>
                        <td class="style4" style="border-right-style: ridge; border-width: 1px; border-color: #CCCCCC">
                            <uc2:ProfilePic ID="ProfilePic1" runat="server" />
                        </td>
                        <td class="style5">
                            <table cellpadding="0" cellspacing="0" class="style2">
                                <tr>
                                    <td colspan="2">
                                        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                                        </asp:ContentPlaceHolder>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td class="style6" style="border-left-style: ridge; border-width: 1px; border-color: #CCCCCC">
                            <br />
                            <uc3:PeopleYouMayKnow ID="PeopleYouMayKnow1" runat="server" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td align="center" style="border: 1px ridge #CCCCCC">
                <table align="center" cellpadding="0" cellspacing="0" class="style3">
                    <tr>
                        <td>
                            © Copyright  HeartBeat
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

  1. The code behind of Profile master page is empty as we have written all the respective code to for wall updates and friend suggestions on the user controls which we are dragging and dropping on the master page
  2. The empty code behind is shown below in the code snippet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProfileMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
}

Ajax Auto Complete Extender for social networking website using ajax



Ajax Auto Complete Extender for social networking website using ajax



  1. Ajax Auto complete extender is a part of ajax tool kit and it is used to show suggestions so that any user does not have to type the complete sentence or word.
  2.  In our Social Networking project HeartBeat we have Autocomplete extender to show friend name suggestions when user types any name. 
  3. To Accomplish this task we have made use of web service with a function in it which will return the result with real time effect.

WebService.cs C# Code:

  1. Open WebService.cs User Control Which You have created in AppCode Folder. In the Source of WebService.cs file you can add the following piece of code given below.
  2. On Opening the Webservice you will find that there is a hello world function by defualt it comes with every new webservice that is added.
  3. We have created a function named as SearchFriends which will receive text in string format and return an array of string.Through this webservice we have made a database connection and we are searching the name of the registered user using like operator.
  4. We are storing the retrived results in a datatable and after processing the datatable using foreach loop we are returning an array of strings


using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Configuration;


[ScriptService]
[WebService(Namespace = "http://tempuri.org/")]
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{

    public WebService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public string[] SearchFriends(string prefixText)
    {
        string connstring = Database.connString.ToString();
        string sql = "Select * from Register Where Name like @prefixText";
        SqlDataAdapter da = new SqlDataAdapter(sql, connstring);
        da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%";
        DataTable dt = new DataTable();
        da.Fill(dt);
        string[] items = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            items.SetValue(dr["Name"].ToString(), i);
            i++;
        }
        return items;
    }

}


About Me in Social Networking Website like Facebook using asp.net csharp



About Me in Social Networking Website like Facebook using asp.net csharp(c#)



  • About Me Page as the name suggests it shows the information provided by the user at the time of registration on HearBeat Project.
  • It shows general information like Name,Emailid,Gender,date of birth,Country and a short sentence about the user.
  • It Generally Fetches the Information about the User who is Currently Logged in and It also shows the information about the Current Profile User.
  • For Example:Currently i am looking at my friends profile and when i click on about me link it will show my friends aboutme and when i am on my profile then it shows my information

AboutMe.aspx Code:

  • Open AboutMe.aspx page.In the Source of aboutme.aspx page you can make a tabular structure as shown in below code snippet.
  • Design Explanation:
  • Inside the Content place holder of AboutMe.aspx we have made a tabular structure as shown in the code snippet.
  • Inside One td we have dragged dropped one Datalist and Inside Datalist we have again made a tabular structure to show the user information in proper manner.We have named the datalist as ListAboutMe
  • We have Binded the User Information using DataBinder.Eval(Container.DataItem, "FieldName") Evaluation Function


<%@ Page Title="" Language="C#" MasterPageFile="~/ProfileMaster.master" AutoEventWireup="true"
    CodeFile="AboutMe.aspx.cs" Inherits="AboutMe" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <table cellspacing="1" class="style2">
        <tr>
            <td align="center" style="width: 280px">
                 
            </td>
            <td align="left">
                 
            </td>
        </tr>
        <tr>
            <td align="center" colspan="2">
                <hr />
                <asp:DataList ID="ListAboutMe" runat="server">
                    <ItemTemplate>
                        <table cellspacing="1" style="width: 432px">
                            <tr>
                                <td align="center" style="width: 183px">
                                     
                                </td>
                                <td align="center">
                                     
                                </td>
                            </tr>
                            <tr>
                                <td align="center" style="width: 183px">
                                    Name
                                </td>
                                <td align="left">
                                    <%# DataBinder.Eval(Container.DataItem, "Name")%>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" style="width: 183px">
                                    EmailId
                                </td>
                                <td align="left">
                                    <%# DataBinder.Eval(Container.DataItem, "EmailId")%>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" style="width: 183px">
                                    Date Of Birth
                                </td>
                                <td align="left">
                                    <%# DataBinder.Eval(Container.DataItem, "DOB")%>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" style="width: 183px">
                                    Country
                                </td>
                                <td align="left">
                                    <%# DataBinder.Eval(Container.DataItem, "Country")%>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" style="width: 183px">
                                    Gender
                                </td>
                                <td align="left">
                                    <%# DataBinder.Eval(Container.DataItem, "Gender")%>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" style="width: 183px">
                                    AboutMe
                                </td>
                                <td align="left">
                                    <%# DataBinder.Eval(Container.DataItem, "AboutMe")%>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" style="width: 183px">
                                     
                                </td>
                                <td align="center">
                                     
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:DataList>
                <hr />
            </td>
        </tr>
        <tr>
            <td align="center" style="width: 280px">
                 
            </td>
            <td align="left">
                 
            </td>
        </tr>
        <tr>
            <td align="center" style="width: 280px">
                 
            </td>
            <td align="left">
                 
            </td>
        </tr>
    </table>
</asp:Content>

AboutMe.aspx.cs C# Code:

  • Now Open AboutMe.aspx.cs file.In the Code behind file of AboutMe.aspx you can write the following piece of code.
  • Code Explanation:
  • From the Page_Load Method we are calling a Function which we have named as GetAboutMe().
  • In GetAboutMe() Function we have created a new Datatable Object and after that we are writing a query the get all the information of the user whose RegisterId is CurrentPofileId which is stored in session. 
  • We have Used the Database.GetData() Function which we have created in Database Connectivity Part of this project.Database.cs is a static class and it contains static function GetData() so we can call it from anywhere in our project without even creating an object of it and it returns datatable
  • Finally we are checking whether there are any records for the current user.If records are there then we are binding it to datalist ListAboutMe


        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using System.Data;

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

           private void GetAboutMe()
           {
                DataTable dt = new DataTable();
 string query = "select  *  from Register where RegisterId='" + Session["CurrentProfileId"] + "'";
                dt = Database.GetData(query);
                if (dt.Rows.Count > 0)
                {
                    ListAboutMe.DataSource = dt;
                    ListAboutMe.DataBind();
                }
            }
        }

    


Auto Complete Search Results in Social Networking Website like Facebook using Asp.Net



Search Results in Social Networking Website like Facebook using Asp.Net Csharp(c#)



  1. Searching is also an integral part of social networking website as it helps to get new friends and expand the social network in a better way.
  2. We are showing the search results in the form of datalist with username ,profile picture and from the search results you can also navigate to any profile page.
  3. We have also made use of Ajax Autocomplete Extender to give search suggestions from our friends database list

Search.ascx Code:

  1. Open Search.ascx User Control Which You have created in Controls Folder.
  2.  In the Source of Search.ascx user control you can add the following piece of code given below.
  3. For Search results also we have made a different user controls as it sepearats the code from the main page.The search user control is quite simple as we have created a tablular struture with the datalist binding user name and profile picture.In the ImageButton Command argument we are binding the registerid so that user can go to other profiles from here


<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="Search.ascx.cs" Inherits="Controls_Search" %>
<style type="text/css">
    .styleFriends
    {
        width: 100%;
    }
    .friendsTD
    {
        width: 30%;
        text-align: center;
    }

</style>

<asp:DataList ID="SearchList" runat="server" Width="100%">
    <ItemTemplate>
        <table cellpadding="2" class="styleFriends" style="border: 1px ridge #CCCCCC">
            <tr>
                <td class="friendsTD" rowspan="2">
                    <asp:ImageButton ID="imgbtnPic" runat="server" Height="84px" Width="84px" 
                     ImageUrl='<%# "ImageHandler.ashx?RegisterId="+ Eval("RegisterId") %>' 
                     BorderColor="#CCCCCC" BorderStyle="Ridge" BorderWidth="1px" 
                     CommandArgument='<%#Bind("RegisterId") %>' onclick="imgbtnPic_Click"/>
                </td>
                <td align="left"> 
                    <asp:Label ID="lblFriendName" runat="server" Font-Bold="False" 
                        Font-Names="Franklin Gothic Medium" Text='<%# Bind("Name") %>'></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                     </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>
    
    

Search.ascx.cs C# Code:

  1. Now Open the Code behind file of Friends User Control. In Search.ascx.cs file you can add the following piece of code given below.
  2. In the Search.ascx user control code behind from the page load method we are calling GetSearchResults() to get the search results.
  3. what ever the user have entered to search we are storing it in session and based on the session we are search the results.
  4. We have made use of sql like operator to get the results which begins with the search text


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Controls_Search : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GetSearchResults();
    }

    private void GetSearchResults()
    {
        DataTable dt = new DataTable();
        string query = "Select * FROM [Register] 
        where Name like '" + Session["SearchText"].ToString() + "%'";
        dt = Database.GetData(query);
        if (dt.Rows.Count > 0)
        {

            SearchList.Visible = true;
            SearchList.DataSource = dt;
            SearchList.DataBind();
        }
        else
        {

        }
    }

    protected void imgbtnPic_Click(object sender, ImageClickEventArgs e)
    {
        Session["CurrentProfileId"] = (((ImageButton)sender).CommandArgument).ToString();
        Response.Redirect("Main.aspx");
    }
}

    

Search.aspx Code:

  1. Once the Search.ascx user control is ready we have to add it to Search.aspx page as shown below code snippet.
  2. Here we have done nothing but dragged dropped the user control on the search.aspx page


<%@ Page Title="" Language="C#" MasterPageFile="~/ProfileMaster.master" 
AutoEventWireup="true" CodeFile="Search.aspx.cs" 
Inherits="Search" EnableEventValidation="false"%>

<%@ Register src="Controls/Search.ascx" tagname="Search" tagprefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <table align="center" class="style2">
        <tr>
            <td>
                 </td>
        </tr>
        <tr>
            <td>
                Search Results:</td>
        </tr>
        <tr>
            <td>
                 </td>
        </tr>
        <tr>
            <td>
                <uc1:Search ID="Search1" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                 </td>
        </tr>
        <tr>
            <td>
                 </td>
        </tr>
        <tr>
            <td>
                 </td>
        </tr>
    </table>

</asp:Content>

    

Profile Picture in Social networking Website using asp.net csharp



Profile Picture in Social networking Website using asp.net csharp



  1. Profile Picture is the heart of all social netoworking websites like facebook,twitter,google plus.
  2. For our Social Networking site HeartBeat we have created a user control to show the left side menu along with the profile picture

ProfilePic.ascx Code:

  1. Open ProfilePic.ascx User Control Which You have created in Controls Folder.
  2. In the Source of ProfilePic.ascx user control you can add the following piece of code given below.
  3. In Profile picture user control we have created a tabular structure and dragged dropped one datalist to show the profile picture which is converted into bytes using handler,also we have dragged dropped two link buttons for aboutme and friends.
  4. We have also created one label to show the user name along with profile picture


<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="ProfilePic.ascx.cs" Inherits="Controls_ProfilePic" %>
<style type="text/css">
    .ProfilePic
    {
        width: 171px;
    }
</style>
<table align="center" class="ProfilePic">
    <tr>
        <td align="center">
            <asp:DataList ID="dlProfile" runat="server" BorderColor="Black" 
            BorderStyle="Ridge"
                BorderWidth="0px" Visible="false" Width="60px">
                <ItemTemplate>
                    <table align="center" style="width: 100%">
                        <tr>
                            <td>
                            <asp:ImageButton ID="ImagePhoto" runat="server"
                             BorderColor="Black" BorderStyle="Ridge"
                             BorderWidth="1px" Height="170px"
                             ImageUrl='<%# "ImageHandler.ashx?RegisterId="+ Eval("RegisterId") %>'
                             Width="158px" />
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>
        </td>
    </tr>
    <tr>
        <td align="center" style="border: 1px ridge #CCCCCC">
            <asp:LinkButton ID="btnAboutMe" runat="server" 
            OnClick="btnAboutMe_Click">AboutMe</asp:LinkButton>
        </td>
    </tr>
    <tr>
        <td align="center" style="border: 1px ridge #CCCCCC">
            <asp:LinkButton ID="btnFriends" runat="server" 
            OnClick="btnFriends_Click">Friends</asp:LinkButton>
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
</table>

     
Now Open the Code behind file of Friends User Control. In ProfilePic.ascx.cs file you can add the following piece of code given below.

ProfilePic.ascx.cs C# Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;

    public partial class Controls_ProfilePic : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
       
          GetProfilePic();

        }

        private void GetProfilePic()
        {
            DataTable dt = new DataTable();
            string query = "select  *  from Register where RegisterId='" + Session["CurrentProfileId"] + "'";
            dt = Database.GetData(query);
            if (dt.Rows.Count > 0)
            {
                dlProfile.Visible = true;
                Session["Name"] = dt.Rows[0]["Name"];
                dlProfile.DataSource = dt;
                dlProfile.DataBind();
            }
            else
            {
            
            }
        }
        protected void btnAboutMe_Click(object sender, EventArgs e)
        {
            Response.Redirect("AboutMe.aspx");
        }
        protected void btnFriends_Click(object sender, EventArgs e)
        {
            Response.Redirect("Friends.aspx");
        }
 
    }
    

Friend Suggestions People You May Know in Social Networking Website using asp.net csharp


People You May Know in Social Networking Website using asp.net csharp


  1. Facebook also have a Control which shows and keeps on giving friend suggestions so that we can expand our social network and make more friends.
  2. We have also developed a user control which gives suggestions about the people you may know

PeopleYouMayKnow.ascx Code:

  1. Open PeopleYouMayKnow.ascx User Control Which You have created in Controls Folder. 
  2.  In the Source of PeopleYouMayKnow.ascx user control you can add the following piece of code given below.
  3. In this User control we have a datalist which is showing all the people who are not in your friends list

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PeopleYouMayKnow.ascx.cs"
    Inherits="Controls_PeopleYouMayKnow" %>
<style type="text/css">
    .FriendsSuggestions
    {
        width: 130px;
        text-align: center;
    }
    .FriendsSuggestionsTD
    {
        height: 20px;
    }
</style>
<table align="center" cellpadding="0" cellspacing="0" class="FriendsSuggestions">
    <tr>
        <td align="center" class="FriendsSuggestionsTD">
            <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="X-Small"
                Text="People You May Know"></asp:Label>
        </td>
    </tr>
    <tr>
        <td align="center">
            <asp:DataList ID="dlProfile" runat="server" BorderColor="Black" BorderStyle="Ridge"
                BorderWidth="0px" Visible="true" Width="100px">
                <ItemTemplate>
                    <table align="center" cellspacing="1" style="width: 100%">
                        <tr>
                            <td align="center">
                                <asp:ImageButton ID="ImagePeopleYouMayknow" runat="server" 
                                BorderColor="Black" BorderStyle="Ridge"
                                BorderWidth="1px" Height="63px" 
                                ImageUrl='<%# "ImageHandler.ashx?RegisterId="+ Eval("RegisterId") %>'
                                Width="74px" OnClick="ImagePeopleYouMayknow_Click" 
                                CommandArgument='<%#Bind("RegisterId") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                                <asp:Label ID="lblName" runat="server" Font-Size="Small" Text='<%# Bind("Name") %>'
                                    ForeColor="Black"></asp:Label>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>
        </td>
    </tr>
    <tr>
        <td align="center">
             
        </td>
    </tr>
</table>

    

PeopleYouMayKnow.ascx.cs C# Code:

  1. Open PeopleYouMayKnow.ascx User Control Which You have created in Controls Folder.
  2.  In the Source of PeopleYouMayKnow.ascx user control you can add the following piece of code given below.
  3. In the code behind we are calling a Method PeopleYouMayKnow() which is getting all the friends who you may know based on some query.
  4. Finally we are binding all the records to datalist.We have written a query with sub query and union of friends and register tables

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Controls_PeopleYouMayKnow : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
         PeopleYouMayKnow();
    }

    private void PeopleYouMayKnow()
    {
        DataTable dt = new DataTable();
        string query = "Select * from Register R where RegisterId not in ((select F.FriendId as RegisterId  from Friends f where f.MyId='" + Session["UserId"] + "' and f.Status=1) Union  (select F.MyId as RegisterId  from Friends f where f.FriendId='" + Session["UserId"] + "' and f.Status=1))and R.RegisterId !='" + Session["UserId"] + "'";

        dt = Database.GetData(query);
        if (dt.Rows.Count > 0)
        {
            
            dlProfile.Visible = true;
            dlProfile.DataSource = dt;
            dlProfile.DataBind();
        }
        else
        {

        }
    }
    protected void ImagePeopleYouMayknow_Click(object sender, ImageClickEventArgs e)
    {
        Session["CurrentProfileId"] = (((ImageButton)sender).CommandArgument).ToString();
        Response.Redirect("Main.aspx");
    }
}


Wall and Latest Updates User Control for Social Networking


Wall and Latest Updates User Control for Social Networking website Using Asp.net,Csharp


  1. Wall just like facebook is an very impoartant part of social networking website where user can express their feelings and emotions.
  2. It is a place where our friends can also write what they feel about us.We have also taken care that we can only see our friends wall

LatestUpdates.ascx Code:


  1. Open LatestUpdates.ascx User Control Which You have created in Controls Folder. 
  2.  In the Source of LatestUpdates.ascx user control you can add the following piece of code given below.
  3. In this user control first we have a Add as friend Button.
  4. If you are on your friends profile then this add as friend button is not visible.
  5. If the Friend request is pending it will show as the friend request pending status.
  6. Next is inside an ajax update panel which we have used for partial staus update.It will also show the progress bar while updating the wall status.
  7. We have a text box where we can type our status and that status will be updated on our wall.
  8. Our Status updates are binded to datalist with the profile picture status,posted by and post date.we have associated AsyncPostBackTrigger with the button post so that there is no page refresh when we update our status on heartbeat

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LatestUpdates.ascx.cs"
    Inherits="Controls_LatestUpdates" %>
<style type="text/css">
    .styleLatestUpdates
    {
        width: 400px;
    }
    .stylePost
    {
        width: 98%;
        height: 90px;
    }
    .styleComment
    {
        width: 103px;
    }
    .stylePostPic
    {
        width: 110px;
        vertical-align: top;
    }
</style>
<table style="width: 550px" align="center">
    <tr>
        <td align="left" style="padding-left: 10px;">
            <asp:Label ID="lblName" runat="server" Font-Bold="True" Font-Names="Georgia" Font-Size="Large"></asp:Label>
        </td>
        <td align="right">
            <asp:LinkButton ID="btnAddAsFriend" runat="server" Text="Add As Friend" Font-Bold="True"
                Font-Italic="True" OnClick="btnAddAsFriend_Click"></asp:LinkButton>
            <br />
        </td>
    </tr>
</table>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <table align="center" cellpadding="0" cellspacing="0" class="styleLatestUpdates">
            <tr>
                <td>
                    <table style="width: 550px">
                        <tr>
                            <td align="left">
                                <panel runat="server" id="pnlStatus">
                            
                                <asp:TextBox ID="txtWhatsOnYourHeart" runat="server" Height="59px" TextMode="MultiLine"
                                    Width="543px"></asp:TextBox>
                                <br />
                                 <asp:Button ID="btnPost" runat="server" BackColor="#CCCCCC" BorderColor="#CCCCCC"
                                    BorderStyle="Ridge" BorderWidth="1px" Font-Bold="True" ForeColor="#333333" OnClick="btnPost_Click"
                                    Text="Post" Width="52px" />
                                <asp:UpdateProgress ID="ProgressBar" runat="server">
                                    <ProgressTemplate>
                                        <asp:Image ID="Image1" runat="server" ImageUrl="~/images/loading.gif" />
                                    </ProgressTemplate>
                                </asp:UpdateProgress>  
                                </panel>
                            </td>
                        </tr>
                    </table>
                    <hr />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:DataList ID="dlPosts" runat="server" Width="550px">
                        <ItemTemplate>
                            <div>
                                <table cellpadding="0" cellspacing="0" class="stylePost">
                                    <tr>
                                        <td class="stylePostPic" rowspan="2" align="center">
                                            <asp:ImageButton ID="YourPic" runat="server" BorderColor="Black" BorderStyle="Ridge"
                                                BorderWidth="1px" Height="60px" ImageUrl='<%# "ImageHandler.ashx?RegisterId="+ Eval("RegisterId") %>'
                                                Width="60px" OnClick="YourPic_Click" CommandArgument='<%#Bind("RegisterId") %>' />
                                        </td>
                                        <td valign="top" align="left">
                                            <asp:Label runat="server" ID="lblName" ForeColor="Blue" Font-Bold="True" Font-Italic="True">
                                            <%# DataBinder.Eval(Container.DataItem, "Name")%> Posted:</asp:Label>
                                             <div style="width: 300px">
                                                <%# DataBinder.Eval(Container.DataItem, "Post")%>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right" valign="bottom">
                                            <asp:Label runat="server" ID="lblPostedOn" ForeColor="Black" Font-Italic="True" Font-Size="Small">
                                            Posted On: <%# DataBinder.Eval(Container.DataItem, "PostDate")%>
                                            </asp:Label>
                                        </td>
                                    </tr>
                                </table>
                                <br />
                                <hr />
                            </div>
                        </ItemTemplate>
                    </asp:DataList>
                </td>
            </tr>
        </table>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnPost" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

    

LatestUpdates.ascx.cs C# Code:

Now Open the Code behind file of Friends User Control. In LatestUpdates.ascx.cs file you can add the following piece of code given below.
From the Page Load method we are calling two methods LatestUpdates() and CheckFriendshipStatus().LatestUpdates function is getting the latest updates from database and check friendship status is checking the status of friendship so that we can hide and show the wall and add as friend button.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Controls_LatestUpdates : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LatestUpdates();
        CheckFriendshipStatus();
    }


    private void LatestUpdates()
    {
        if (Session["Name"]!=null)
        {
            lblName.Text = Session["Name"].ToString();
        }
        DataTable dt = new DataTable();
        string query = "SELECT u.RegisterId,u.Name,s.FromId,s.ToId,s.Post,s.PostId,s.PostDate FROM [Register] as u, Posts as s WHERE u.RegisterId=s.FromId AND s.ToId='" + Session["CurrentProfileId"] + "' order by s.PostId desc";
        dt = Database.GetData(query);

        if (dt.Rows.Count > 0)
        {
            dlPosts.Visible = true;
            dlPosts.DataSource = dt;
            dlPosts.DataBind();
        }
        else
        {

        }


    }

    private void CheckFriendshipStatus()
    {
        if (Object.Equals(Session["UserId"].ToString(), Session["CurrentProfileId"].ToString()))
        {
            btnAddAsFriend.Visible = false;
        }
        else
        {
            DataTable dt1 = new DataTable();
            string chkfriendRequest = "SELECT * FROM Friends WHERE (MyId='" + Session["UserId"].ToString() + "' and FriendId='" + Session["CurrentProfileId"].ToString() + "') OR (MyId='" + Session["CurrentProfileId"].ToString() + "' and FriendId='" + Session["UserId"].ToString() + "')";

            dt1 = Database.GetData(chkfriendRequest); ;
            if (dt1.Rows.Count > 0)
            {
                if (dt1.Rows[0]["Status"].ToString() == "1")
                {
                    //lblError.Text = "Already in friend list";
                    btnAddAsFriend.Visible = false;

                }
                if (dt1.Rows[0]["Status"].ToString() == "0")
                {
                    //lblError.Text = "Friend Request Pending";
                    btnAddAsFriend.Visible = true;
                    btnAddAsFriend.Text = "Friend Request Pending";
                    btnAddAsFriend.Enabled = false;
                    pnlStatus.Visible = false;
                    dlPosts.Visible = false;
                }
                if (dt1.Rows[0]["Status"].ToString() == "2")
                {
                    //lblError.Text = "Friend Request deny";
                    pnlStatus.Visible = false;
                    dlPosts.Visible = false;
                }
            }
            else
            {
                pnlStatus.Visible = false;
                dlPosts.Visible = false;
            }
        }


    }
    protected void btnPost_Click(object sender, EventArgs e)
    {
        string PostDate = DateTime.Now.ToLongDateString();
        string InsertPostquery = "Insert into Posts (Post,FromId,ToId,PostDate)values('" + txtWhatsOnYourHeart.Text + "','" + Convert.ToInt32(Session["UserId"]) + "','" + Convert.ToInt32(Session["CurrentProfileId"]) + "','" + PostDate + "')";
        Database.InsertData(InsertPostquery);
        LatestUpdates();
        txtWhatsOnYourHeart.Text = string.Empty;

    }

    protected void btnAddAsFriend_Click(object sender, EventArgs e)
    {


        if (!object.Equals(Session["UserId"], null))
        {
            if (Object.Equals(Session["UserId"], Session["CurrentProfileId"]))
            {
                btnAddAsFriend.Visible = false;
            }
            else
            {
                DataTable dt1 = new DataTable();
                string chkfriendRequest = "SELECT * FROM Friends WHERE (MyId='" + Session["UserId"].ToString() + "' and FriendId='" + Session["CurrentProfileId"].ToString() + "') OR (MyId='" + Session["CurrentProfileId"].ToString() + "' and FriendId='" + Session["UserId"].ToString() + "')";

                dt1 = Database.GetData(chkfriendRequest); ;
                if (dt1.Rows.Count > 0)
                {
                    if (dt1.Rows[0]["Status"].ToString() == "1")
                    {
                        //lblError.Text = "Already in friend list";
                        btnAddAsFriend.Visible = false;

                    }
                    if (dt1.Rows[0]["Status"].ToString() == "0")
                    {
                        //lblError.Text = "Friend Request Pending";
                        btnAddAsFriend.Visible = true;
                        btnAddAsFriend.Text = "Friend Request Pending";
                        btnAddAsFriend.Enabled = false;
                    }
                    if (dt1.Rows[0]["Status"].ToString() == "2")
                    {
                        //lblError.Text = "Friend Request deny";

                    }
                }
                else
                {
                    string friendRequest = "Insert INTO Friends (MyId,FriendId,Status) VALUES('" + Session["UserId"].ToString() + "','" + Session["CurrentProfileId"].ToString() + "','" + 0 + "')";
                    Database.InsertData(friendRequest);
                    btnAddAsFriend.Text = "Friend Request Sent.";
                }
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx");
        }
    }

    protected void YourPic_Click(object sender, ImageClickEventArgs e)
    {
        Session["CurrentProfileId"] = (((ImageButton)sender).CommandArgument).ToString();
        Response.Redirect("Main.aspx");
    }
}


Facebook Like Header User Control using Asp.net for Social networking website



Facebook Like Header User Control using Asp.net for Social networking website



  1. We have created a Header similar to facebook header with Heartbeat logo,Notifications icon,Messages icon and Friend Requests icon,Search Box and Logout button.
  2. To create a facebook like header we have created a user control and used it on profile master page


Header.ascx Code:



  1. Open Header.ascx User Control Which You have created in Controls Folder. 
  2. In the Source of Header.ascx user control you can add the following piece of code given below. 
  3. We have created a user control and named it as Header. 
  4. On the user control we have make a tabular structure and properly alligned the logo and other images,seach box and logout button. We have also added AutoCompleteExtender and TextBoxWatermarkExtender on the header control


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Header.ascx.cs" Inherits="Controls_Header" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<style type="text/css">
    .HeaderStyle
    {
        width: 80px;
        padding-right: 7px;
    }
    .LogoStyle
    {
        width: 50px;
        padding-right: 7px;
    }
    .Red
    {
        color: Red;
        font-weight: bold;
    }
</style>
<table cellpadding="0" cellspacing="1" width="900px">
    <tr>
        <td align="center" class="LogoStyle">
            <asp:LinkButton ID="btnLogo" runat="server" Font-Bold="True" 
            Font-Names="Franklin Gothic Medium"
            Font-Underline="False" ForeColor="White" 
            Font-Size="Large" 
            OnClick="btnLogo_Click">HeartBeat</asp:LinkButton>
        </td>
        <td class="HeaderStyle">
            <table>
                <tr>
                    <td>
                        <asp:HyperLink ID="HyperLink1" runat="server" 
                        ImageUrl="~/images/globe.png"></asp:HyperLink>
                    </td>
                    <td>
                        <asp:ImageButton ID="imgBtnMsg" runat="server" 
                        ImageUrl="~/images/msgr.png" />
                    </td>
                    <td>
                        <asp:ImageButton ID="imgBtnFrendReq" runat="server" 
                        ImageUrl="~/images/friendRequest.png" />
                    </td>
                </tr>
            </table>
        </td>
        <td align="left">
            <asp:TextBox ID="txtSearch" runat="server" Width="300px" 
            Height="21px"></asp:TextBox>

            <asp:TextBoxWatermarkExtender ID="txtSearch_TextBoxWatermarkExtender" 
            runat="server" Enabled="True" 
            TargetControlID="txtSearch" WatermarkText="Search Friends..">
            </asp:TextBoxWatermarkExtender>

            <asp:AutoCompleteExtender ID="txtSearch_AutoCompleteExtender" 
            runat="server" DelimiterCharacters="" ServiceMethod="SearchFriends" 
            Enabled="True" TargetControlID="txtSearch" ServicePath="~/WebService.asmx"
            CompletionInterval="100" MinimumPrefixLength="1">
            </asp:AutoCompleteExtender>

            <asp:Button ID="btnSearch" runat="server" Text="Search"
             OnClick="btnSearch_Click" BorderColor="#0033CC" 
             BorderStyle="Ridge" Font-Bold="True" ForeColor="Black"
             Height="27px" />

        </td>
        <td align="right">
            <asp:LinkButton ID="btnLogout" runat="server" 
            Font-Bold="False" ForeColor="White"
            OnClick="btnLogout_Click" 
            Font-Names="Arial Rounded MT Bold" 
            Font-Underline="False">Logout</asp:LinkButton>
        </td>
    </tr>
</table>


Header.ascx.cs C# Code:



  1. Now Open the Code behind file of Header User Control. 
  2. In Header.ascx.cs file you can add the following piece of code given below. 
  3. In the csharp file of header control we have three OnClick as Logo Click , Logout Button Click and Search Button Click.
  4. On Logo Click we are changing the session.On Logout Button Click we are redirecting the user to login page and Search Button we are directing the user to Search page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Controls_Header : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    
    protected void btnLogo_Click(object sender, EventArgs e)
    {
        Session["UserId"] = Session["UserId"];
        Session["CurrentProfileId"] = Session["UserId"];
        Response.Redirect("Main.aspx");
        
    }
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["SearchText"]=txtSearch.Text;
        Response.Redirect("~/Search.aspx");
    }
}

Friend List User Control in a Social Networking website using asp.net csharp


User Friend List User Control in a Social Networking website using asp.net csharp


  1. Friend List is very important part of any social networking project then it may be facebook or twitter or google plus the names may be different facebook calls it friend list,twitter calls it followers and google plus calls it cirlces, but the functionlity is similar. 
  2. We have also created Friendlist on HeartBeat to show the Friends of User.
  3. For this we have created a different user control and named it as Friends

Friends.ascx Code:


  1. Open Friends.ascx User Control Which You have created in Controls Folder.
  2.  In the Source of Friends.ascx user control you can add the following piece of code given below.
  3. On the User control we have dragged dropped one datalist and inside the item template we have created a tabular structure to show the Friends Photo and Name.
  4. As we have stored user images inside our database and not in any folder.
  5. We have converted our profile pictures into bytes and stored in database field of datatype image.
  6. So to retrive it and showing it in a datalist we have made use of ImageHandler and by passing the RegisterId we are fetching the profile picture and binding it to datalist

 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Friends.ascx.cs" Inherits="Controls_Friends" %>
<style type="text/css">
    .styleFriends
    {
        width: 100%;
    }
    .friendsTD
    {
        width: 20%;
        text-align: center;
    }
</style>
<asp:DataList ID="dlFriends" runat="server" Width="100%">
    <ItemTemplate>
        <table cellpadding="2" class="styleFriends" style="border: 1px ridge #CCCCCC">
            <tr>
                <td class="friendsTD" rowspan="2">
                    <asp:ImageButton ID="imgbtnYou" runat="server" Height="84px" Width="84px" 
                    ImageUrl='<%# "ImageHandler.ashx?RegisterId="+ Eval("RegisterId") %>'
                    BorderColor="#CCCCCC" BorderStyle="Ridge" BorderWidth="1px" OnClick="imgbtnYou_Click"
                    CommandArgument='<%#Bind("RegisterId") %>' />
                </td>
                <td align="left">
                    <asp:Label ID="lblFriendName" runat="server" Font-Bold="False" Font-Names="Franklin Gothic Medium"
                        Text='<%# Bind("Name") %>'></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                     
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>

    
 

Friends.ascx.cs C# Code:


  1. Now Open the Code behind file of Friends User Control. In Friends.ascx.cs file you can add the following piece of code given below.
  2. If you see the below code from the Page load method we are calling a function MyFriends() which is responsible finding the friends of the current profile which is being viewed.
  3. If it is your profile it will show your friends and if it is other profile it will his her friends.
  4. We have written a query to get those friends whose Friendhsip status is Accepted and not Pending or Rejected.
  5. To achieve this we have made a join and union between Register table and Friends Table.Finally we are checking whether the query has returned any records or not.
  6. If records are there then we are binding it to datalist.Once the User Control is ready we are dragging and dropping it from the Solution Explorer onto the page where we want it to be.
  7. In this way the usercontrol we will be registred on to that page.we can also call the control by manually wrting the aspx tags

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Controls_Friends : System.Web.UI.UserControl
{
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
            MyFriends();
    }

    private void MyFriends()
    {

        string getFriendQuery = "Select * FROM [Register] where RegisterId IN (SELECT MyId  FROM Friends WHERE FriendId='" + Session["CurrentProfileId"] + "' AND Status=1 UNION SELECT FriendId  FROM Friends WHERE MyId='" + Session["CurrentProfileId"] + "' AND Status=1) ";
        dt = Database.GetData(getFriendQuery);
        if (dt.Rows.Count > 0)
        {
            dlFriends.DataSource = dt;
            dlFriends.DataBind();
        }

    }
    protected void imgbtnYou_Click(object sender, ImageClickEventArgs e)
    {
        Session["CurrentProfileId"]=(((ImageButton)sender).CommandArgument).ToString();
        Response.Redirect("Main.aspx");
    }
}

Friends.aspx Code:


  1. Once the Friends.ascx user control is ready we have to add it to Friends.aspx page as shown below code snippet.
  2. Here we have done nothing but dragged dropped the user control on the Friends.aspx page

   <%@ Page Title="" Language="C#" MasterPageFile="~/ProfileMaster.master" 
   AutoEventWireup="true" CodeFile="Friends.aspx.cs" Inherits="Friends" 
   EnableEventValidation="false" %>

<%@ Register src="Controls/Friends.ascx" tagname="Friends" tagprefix="uc1" %>
<%@ Register src="Controls/FriendRequests.ascx" tagname="FriendRequests" tagprefix="uc2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table cellpadding="3" cellspacing="3" style="width: 100%">
        <tr>
            <td>
                <uc2:FriendRequests ID="FriendRequests1" runat="server" />
            </td>
        </tr>
        <tr>
            <td align="center">
                <asp:Label ID="Label1" runat="server" Text="Your Friends Your HeartBeats" 
                    Font-Names="Franklin Gothic Medium"></asp:Label>
                <br />
                <hr />
            </td>
        </tr>
        <tr>
            <td>
                <uc1:Friends ID="Friends1" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                 </td>
        </tr>
    </table>
</asp:Content>


Sending Friend Request Like Facebook in Social networking using asp.net



Sending Friend Request Like Facebook in Social networking using asp.net csharp(c#)

  1. Friend Requests are a very important part of any social networking website.
  2. To Receive the Friend requests we have created a user control.In this user control we have shown the username ,photo along with accept and reject button.
  3. We are showing the Friends Requests above the Friendlist so that the changes are refelected direclty in the friendlist when the user accept or reject any friend request

FriendRequests.ascx Code:

  1. Open FriendRequests.ascx User Control Which You have created in Controls Folder.
  2. In the Source of FriendRequests.ascx user control you can add the following piece of code given below.
  3. In FriendRequests user control we have created a tabular structure and in that tabular structure we have dragged dropped one datalist and two link buttons for accept and reject.
  4. Datalists Itemtemplate contains the user profile picture and also the username.
  5. As always we are binding the image to profile picture using image handler.
  6. We have also binded the RegisterId to Accept and Reject Button in the form of command argument so that we can update the frienship status properly when the user clicks accept or reject button. 
  7. Finally we are adding this control to Friends.aspx page


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FriendRequests.ascx.cs"
    Inherits="Controls_FriendRequests" %>
<asp:DataList ID="dlFriendRequests" runat="server" Width="100%">
    <ItemTemplate>
        <table cellpadding="2" style="border: 1px ridge #CCCCCC; width: 100%;">
            <tr>
                <td class="style1" rowspan="2" align="center">
                    <asp:ImageButton ID="imgbtnYou" runat="server" BorderColor="#CCCCCC" BorderStyle="Ridge"
                        BorderWidth="1px" CommandArgument='<%#Bind("RegisterId") %>' 
                        Height="84px" ImageUrl='<%# "ImageHandler.ashx?RegisterId="+ Eval("RegisterId") %>'
                        OnClick="imgbtnYou_Click" Width="84px" />
                </td>
                <td align="left">
                    <asp:Label ID="lblFriendName" runat="server" Font-Bold="False" 
                    Font-Names="Franklin Gothic Medium"
                    Text='<%# Bind("Name") %>'></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Accept" runat="server" ToolTip="Accept" 
                    Text="Accept" OnClick="Accept_Click"
                     CommandArgument='<%# Bind("RegisterId") %>' /> 
                    <asp:Button ID="Reject" runat="server" ToolTip="Reject" Text="Reject" Width="59px"
                    CommandArgument='<%#Bind("RegisterId") %>' OnClick="Reject_Click" />
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>

    

FriendRequests.ascx.cs C# Code:

  1. Now Open the Code behind file of Friends User Control. In FriendRequests.ascx.cs file you can add the following piece of code given below.
  2. In code behind from page load method we are calling a function ShowFriendRequests().
  3. This function will check for friends requests from database and if there is a friend rquests it will bind it to the friendlist datalist.
  4. We have added a userid equals currentprofileid so that we only see ours friend requests and not others friend requests.
  5. To get the friend requests we have made a sub query between register table and friends table and checking the friendship status as zero.while checking the friend requests if the user clicks on the profile picture of the friendrequests then we are diverting it to his her profile.
  6. We also have two Button Click Events Accept and Reject.On Accept we are updating the friendship status with one and On Reject we are updating the friendship status with two.
  7. To get the friendId we have made use of Command Argument so that we can update the friendship status for particular user only.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

public partial class Controls_FriendRequests : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {
        ShowFriendRequests();
    }
    private void ShowFriendRequests()
    {
        if (Session["UserId"] == Session["CurrentProfileId"])
        {
            DataTable dt = new DataTable();
            string query = "Select * FROM [Register]  where RegisterId IN (SELECT MyId  FROM Friends WHERE FriendId='" + Session["UserId"] + "' AND Status=0) ";
            dt = Database.GetData(query);

            if (dt.Rows.Count > 0)
            {

                dlFriendRequests.DataSource = dt;
                dlFriendRequests.DataBind();
            }
            else
            {

            }
        }

    }

    protected void imgbtnYou_Click(object sender, ImageClickEventArgs e)
    {
        Session["CurrentProfileId"] = (((ImageButton)sender).CommandArgument).ToString();
        Response.Redirect("Main.aspx");
    }

    protected void Accept_Click(object sender, EventArgs e)
    {
        string SenderFriendId = (((Button)sender).CommandArgument).ToString();
        string MyID = Session["UserId"].ToString();
        string AcceptFriendQuery = "Update Friends set Status=1 
        where MyId='" + SenderFriendId + "' AND FriendId='" + MyID + "'";
        Database.UpdateData(AcceptFriendQuery);
        Response.Redirect("~/Friends.aspx");
    }
    protected void Reject_Click(object sender, EventArgs e)
    {
        string SenderFriendId = (((Button)sender).CommandArgument).ToString();
        string MyID = Session["UserId"].ToString();
        string RejectFriendQuery = "Update Friends set Status=2 
        where MyId='" + SenderFriendId + "' AND FriendId='" + MyID + "'";
        Database.UpdateData(RejectFriendQuery);
        Response.Redirect("~/Friends.aspx");
    }
}

    

Friends.aspx Code:

  1. Once the FriendRequests.ascx user control is ready we have to add it to Search.aspx page as shown below code snippet.
  2. Here we have done nothing but dragged dropped the user control on the Friends.aspx page


   <%@ Page Title="" Language="C#" MasterPageFile="~/ProfileMaster.master" 
   AutoEventWireup="true" CodeFile="Friends.aspx.cs" Inherits="Friends" 
   EnableEventValidation="false" %>

<%@ Register src="Controls/Friends.ascx" tagname="Friends" tagprefix="uc1" %>
<%@ Register src="Controls/FriendRequests.ascx" tagname="FriendRequests" tagprefix="uc2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table cellpadding="3" cellspacing="3" style="width: 100%">
        <tr>
            <td>
                <uc2:FriendRequests ID="FriendRequests1" runat="server" />
            </td>
        </tr>
        <tr>
            <td align="center">
                <asp:Label ID="Label1" runat="server" Text="Your Friends Your HeartBeats" 
                    Font-Names="Franklin Gothic Medium"></asp:Label>
                <br />
                <hr />
            </td>
        </tr>
        <tr>
            <td>
                <uc1:Friends ID="Friends1" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                 </td>
        </tr>
    </table>
</asp:Content>