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..

47 comments:

  1. can you put your code pics in your blogger page

    ReplyDelete
    Replies
    1. Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. tango download free

      Delete
  2. One Request Please specify the files before giving extension.Your Coding Is very good,Can you please upload the images on working with it...

    ReplyDelete
  3. sir ur project was very helpful for me

    ReplyDelete
  4. If anybody has updated version of this application or implemented in MVC then please provide the link here for source code, Thanks in advance

    ReplyDelete
  5. Hi

    can you please give any idea about new messages.

    Regards,
    Vinoth.

    ReplyDelete
  6. Hi.
    if anyone implemented it on mvc plz share mvc source code. i am getting errors.

    ReplyDelete
  7. Conversion failed when converting date and/or time from character string.

    Pls Help

    ReplyDelete
    Replies
    1. check your database table, change the datatype from nvarchar to datetime

      Delete
  8. the data is fetched from database but not displaying on page
    in 1 sec the data disappearing

    ReplyDelete
    Replies
    1. if you are online just short out my error please
      its urgent bro

      Delete
  9. Table Structure And Code Different

    ReplyDelete
  10. Can I use for this code customer support chat

    ReplyDelete
  11. plz share the database to pradeep.cs.gaya@gmail.com

    ReplyDelete
  12. plz share the el proyecto completo to galinoe540@gmail.com

    ReplyDelete
  13. lot of bugs in this app but very userfule one

    ReplyDelete
  14. How to run the downloaded project in visual studio 2017

    ReplyDelete
    Replies
    1. I have also the same issue did u please find the way?

      Delete
  15. This comment has been removed by the author.

    ReplyDelete
  16. Thanks for sharing some information on chat web application. I loved it and looking forward to it.

    ReplyDelete
  17. How to run this application getting database error
    I appreciate your work but give guidelines bro

    ReplyDelete
  18. Label1.Text = lBtn.Text; //This lable used to display selected frend name.

    There is no Label 1.Text

    ReplyDelete
  19. sukhjinder hacked this webpage

    ReplyDelete
  20. why shows is you can not master data some thng this

    ReplyDelete
  21. write ..string time = date.ToString("HH:mm:ss"); in Button1.click() function

    ReplyDelete
  22. When I write from one user the other user my message can seen only for 1second???

    ReplyDelete
  23. Your blog is too much amazing. I have found with ease what I was looking. Moreover, the content quality is awesome. Thanks for the nudge! Allo Talk

    ReplyDelete
  24. thanks for sharing, helps me alot

    ReplyDelete
  25. why does db show an error to me?

    ReplyDelete
  26. can you please provide the source code ?

    ReplyDelete