Saturday, 5 November 2016

Create a Simple Chat Web Application in asp.net with C#



Introduction:

How to create an Simple Chat Web Application in asp.net with C# without using any third-party libraries? It was very big question for me when I was newbie in programming. Yeah but it is possible just using “Datalist tool” and with the help of other tools also like Timer, update panel and etc..





Run bellow command in sql sever to create tables:


CREATE TABLE chat
(
ID int,
Sender varchar(255),
Receiver varchar(255),
Message varchar(255),
Date date,
);

CREATE TABLE tblfrends
(
ID int,
Name varchar(255),
Password varchar(255),
);

Use bellow code to load frends or staff:
In .aspx:

<asp:DataList ID="DataList1" runat="server" RepeatDirection="Vertical" Height="1">
        <ItemTemplate>
                 <asp:LinkButton ID="LinkButton1" ForeColor="Black" runat="server" Text='<%# Bind("Name") %>' OnClick="LinkButton1_Click"></asp:LinkButton>
       </ItemTemplate>
</asp:DataList>


In .cs:


public void LoadFriends()
{
      string agent = Session["Name"].ToString();           
      conn.Open();
      string str = "select DISTINCT Name from [tblfrends] where Name!='" + agent + "'";
      SqlCommand cmd1 = new SqlCommand(str, conn);
      SqlDataAdapter da = new SqlDataAdapter(cmd1);
      DataSet ds = new DataSet();
      da.Fill(ds);
      DataList1.DataSource = ds;
      DataList1.DataBind();
      conn.Close();
}

Call this method in page_load() method to get friends after page load..
Add onClick attribute in linkbutton to load friends chat. And use one more datalist tool to display chat.
In .aspx: 

 <asp:LinkButton ID="LinkButton1" ForeColor="Black" runat="server" Text='<%# Bind("name") %>' OnClick="LinkButton1_Click"></asp:LinkButton>

In .cs:

protected void LinkButton1_Click(object sender, EventArgs e)
{
        LinkButton lBtn = sender as LinkButton;
        string id = ((LinkButton)sender).CommandArgument.ToString();
        Label1.Text = lBtn.Text; //This lable used to display selected frend name.
        LoadChatbox();
}
public void LoadChatbox()
{
        string name = Session["Name"].ToString();
        conn.Open();
        string str = "select * from Chat where Sender ='" + name + "' and Receiver ='" + Label1.Text + "' or Sender ='" + Label1.Text + "' and Receiver ='" + name + "'";
        SqlCommand cmd = new SqlCommand(str, conn);
        SqlDataAdapterda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataList2.DataSource = ds;
        DataList2.DataBind();
        conn.Close();
}


Use Timer to load chat in every 1 second and use update panel to avoid refreshing page..
In .aspx:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
     <ContentTemplate>
         <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000"></asp:Timer>
                 <asp:DataList ID="DataList2" runat="server">
                             …………………         
                </asp:DataList>
 </ContentTemplate>
</asp:UpdatePanel>


In .cs:

protected void Timer1_Tick(object sender, EventArgs e)
{
        LoadChatbox();
}

Add one textbox and button to send message.Like…
In .aspx:

 <asp:TextBox ID="TextBox1" runat="server" width="400px" height="51px"></asp:TextBox>.
<asp:Button ID="Button1" runat="server" Text="Replay"  OnClick="Button1_Click"/> 

In .cs:

protected void Button1_Click(object sender, EventArgs e)
{
string d1 = DateTime.Now.ToString("MM-dd-yyyy");
string name = Session["Name"].ToString();
string query = "insert into Chat values('" + name + "','" + Label1.Text + "','" + TextBox1.Text + "','" + time + "')";
int i = db.ExecuteQuery(query);
if (i == 1)
{
            TextBox1.Text = "";
}
}
 


That’s it…
Now try to run code and enjoy the project :)
Or
You can download Project in bellow link..