Monday 1 February 2016

ASP.NET UpdatePanel Implementing Chat(also Showing Online Friends) similar to that of Facebook


ASP.NET UpdatePanel Implementing Chat(also Showing Online Friends) similar to that of Facebook (ASP.NET+HTML+C#+SQL)



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

<asp:UpdatePanel runat="server" ID="UpdatePanel1">
    <ContentTemplate>
        <asp:Timer runat="server" ID="timer1" OnTick="HaZaTimer_Tick" Interval="1000"></asp:Timer>

        <h3>Online Friends</h3>

        <asp:DataList runat="server" ID="OnlineDataList">
            <ItemTemplate>
                <div id="online">
                    <div id="onlinealign">
                        <asp:LinkButton ID="Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"First_Name") %>'></asp:LinkButton>
                    </div>
                </div>
            </ItemTemplate>
        </asp:DataList>

        <asp:DataList runat="server" ID="ChatDataList">
            <ItemTemplate>
                <div id="chatlist">
                    <div id="chatlistalign">
                        <asp:LinkButton ID="Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"First_Name") %>'></asp:LinkButton>
                        sent on
                        <asp:Label ID="Time" runat="server" Text='<%# Eval("DateAndTime") %>'></asp:Label>
                        &nbsp &nbsp &nbsp Message:
                        <asp:Label ID="Message" runat="server" Text='<%# Eval("Message") %>'></asp:Label>
                    </div>
                </div>
            </ItemTemplate>
        </asp:DataList>
    </ContentTemplate>
</asp:UpdatePanel>

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.LOADCHAT(uid);
    ChatDataList.DataSource = data;
    ChatDataList.DataBind();

    data = obj.LOADONLINE(uid);
    OnlineDataList.DataSource = data;
    OnlineDataList.DataBind();
}

DAL-Data Access Layer (C#)

Load Chat

public DataTable LOADCHAT(string id)
{
    DataTable d = new DataTable();
    SqlConnection con = new SqlConnection(connString); //connString should be defined
    con.Open();
    SqlCommand cmd;
    try
    {
        cmd = new SqlCommand("LoadMessages", 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;

}
Load Online

public DataTable LOADONLINE(string id)
{
    DataTable d = new DataTable();
    SqlConnection con = new SqlConnection(connString); //connString should be defined
    con.Open();
    SqlCommand cmd;
    try
    {
        cmd = new SqlCommand("LoadOnlineFriends", 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)

Load Messages

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

select

User_Info.First_Name,Messages.MessageID,Messages.SenderID,Messages.Message,Messages.DateAndTime
from User_Info inner join Messages on User_Info.UserID=Messages.SenderID
where Messages.ReceiverID=@id
order by Messages.DateAndTime desc

Load Online Friends

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


     select User_Info.First_Name
     from User_Info join Friends on User_Info.UserID=Friends.FriendID join Online ON Friends.FriendID=Online.UserID
     where Friends.UserID=@id AND Online.isOnline=1
    --There is a table in the database names “Online” which keeps the info. that which users are currently online

end



No comments

Post a Comment

Recent Posts