Technologies
Asp.Net
Create Chat Application in Asp.net
  sridotnet
  Thursday, 30 Mar 2017
  674
    0

In this article i'm going to explain one of the method how to create simple web chat application without using SignalR . DDL files and any other third parties codes. This article creating with some reference sites.I'm not the author of this project but what ever code its posted its completely modify as per my requirement.

To develop this application i'm going to use Datalist , UpdatePanel and Timer Event and etc.

Create Database Table

CREATE TABLE ChatterTable
(
  ID int,
  SenderUser varchar(25),
  ReceiverUser varchar(25),
  Message nvarchar(max),
  Date datetime,
)

CREATE TABLE UserTable
(
  ID int,
  Username varchar(25),
  Password varchar(25),
)

Create HTML Markup Language : Login Page

<table>
 <tr>
  <td>Username : </td>
  <td><asp:TextBox ID="txtUsername" runat="server"></asp:TextBox></td>
 </tr>
  <tr>
  <td>Password : </td>
  <td><asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
  <td colspan="2"><asp:Button ID="btnLogin" runat="server" Text="Login"></asp:Button>  
  </tr>
</table>

C# Code : Login Button

I'm not writing complete code of login just simple using simple code Response.Redirect line

 //btnLogin Click Event
 // if Login successfully then its redirect Chatter.aspx Page
  Response.Redirect("Chatter.aspx?Username="+txtUsername.Text.Trim());

Create HTML Markup Language : Chatter Page

In chatter page i'm using Label , DataList's and also UpdatePanel

<body>
<asp:Label ID="lblUsername" runat="server" Visible="false"></asp:Label>
<asp:DataList ID="DataListLoadUser" runat="server" RepeatDirection="Vertical" Height="1">
        <ItemTemplate>
                 <asp:LinkButton ID="lnkUserChatter" ForeColor="Black" runat="server" Text='<%# Evalundefined"Username") %>' OnClick="lnkUserChatter_Click"></asp:LinkButton>
       </ItemTemplate>
</asp:DataList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
     <ContentTemplate>
         <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000"></asp:Timer>
                 <asp:DataList ID="DataListChatterPanel" runat="server">        
                </asp:DataList>
<asp:TextBox ID="txtMessage" runat="server" width="400px" height="51px"></asp:TextBox>.
<asp:Button ID="btnSend" runat="server" Text="Send"  OnClick="btnSend_Click"/> 
 </ContentTemplate>
 <Trigger>
<asp:AsyncPostBackTrigger Control="btnSend" Event="Click" />
 </Trigger>
</asp:UpdatePanel>
</body> 

C# Coding

Load UserList in FirstDataList

protected void Page_Load(----)
 {
   if(!IsPostBack)
   {
     LoadUsers();
   }
 } 
public void LoadUsers()
{
      string Username = Request.QueryString["Username"].ToString();           
      con.Open();
      string strQuery = "select DISTINCT Username from UserTable where Username!='" + Username + "'";
      SqlCommand cmd = new SqlCommand(strQuery, con);
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      DataSet ds = new DataSet();
      da.Fill(ds);
      DataListLoadUser.DataSource = ds;
      DataListLoadUser.DataBind();
      con.Close();
} 
protected void lnkUserChatter_Click(object sender, EventArgs e)
{
        LinkButton lnkUser = sender as LinkButton;
        string id = ((LinkButton)sender).CommandArgument.ToString();
        Label1.Text = lnkUser.Text; 
        LoadChatbox();
}
public void LoadChatbox()
{
        string Username = Request.QueryString["Username"].ToString();      
        con.Open();
        string strQuery = "select * from ChatterTable where SenderUser ='" + Username + "' and ReceiverUser ='" + Label1.Text + "' or SenderUser ='" + Label1.Text + "' and ReceiverUser ='" + Username + "'";
        SqlCommand cmd = new SqlCommand(strQuery, con);
        SqlDataAdapterda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataListChatterPanel.DataSource = ds;
        DataListChatterPanel.DataBind();
        con.Close();
} 
protected void Timer1_Tick(object sender, EventArgs e)
{
        LoadChatbox();
}

C# Coding for Message Send i.e btnSend

protected void btnSend_Click(object sender, EventArgs e)
{
 string date = DateTime.Now.ToString("MM-dd-yyyy");
 string Username = Request.QueryString["Username"].ToString();
 SqlCommand cmd = new SqlCommand("Insert into ChaterTable values('" + Username + "','" + Label1.Text + "','" + txtMessage.Text + "','" + date + "')", con);
 con.Open();
 int i = cmd.ExecuteQuery();
 con.Close();
 if (i > 0)
  {
      txtMessage.Text = "";
  }
}