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..
CREATE TABLE chat
(
ID int,
Sender varchar(255),
Receiver varchar(255),
Message varchar(255),
Date date,
);
(
ID int,
Sender varchar(255),
Receiver varchar(255),
Message varchar(255),
Date date,
);
Use bellow code to load frends or staff:
In .aspx:
<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);
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)
{
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);
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();
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..
can you put your code pics in your blogger page
ReplyDeleteThank 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
DeleteOne Request Please specify the files before giving extension.Your Coding Is very good,Can you please upload the images on working with it...
ReplyDeletesir ur project was very helpful for me
ReplyDeleteThanks
ReplyDeleteThank you bro
ReplyDeleteThank you. Useful sample.
ReplyDeleteThank you. Useful sample.
ReplyDeleteIf anybody has updated version of this application or implemented in MVC then please provide the link here for source code, Thanks in advance
ReplyDeleteHi
ReplyDeletecan you please give any idea about new messages.
Regards,
Vinoth.
Hi.
ReplyDeleteif anyone implemented it on mvc plz share mvc source code. i am getting errors.
thanks
ReplyDeleteConversion failed when converting date and/or time from character string.
ReplyDeletePls Help
check your database table, change the datatype from nvarchar to datetime
Deletethe data is fetched from database but not displaying on page
ReplyDeletein 1 sec the data disappearing
if you are online just short out my error please
Deleteits urgent bro
Table Structure And Code Different
ReplyDeleteshow your desing sir
ReplyDeleteCan I use for this code customer support chat
ReplyDeletethanks its working great.
ReplyDeletethanks
ReplyDeleteplz share the database to pradeep.cs.gaya@gmail.com
ReplyDeleteplz share the el proyecto completo to galinoe540@gmail.com
ReplyDeletelot of bugs in this app but very userfule one
ReplyDeleteHow to run the downloaded project in visual studio 2017
ReplyDeleteI have also the same issue did u please find the way?
DeleteI need database full
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for sharing us. Best information
ReplyDeleteThanks for sharing some information on chat web application. I loved it and looking forward to it.
ReplyDeleteHow to run this application getting database error
ReplyDeleteI appreciate your work but give guidelines bro
Label1.Text = lBtn.Text; //This lable used to display selected frend name.
ReplyDeleteThere is no Label 1.Text
hat matherchod
ReplyDeletesukh
ReplyDeletesukhjinder hacked this webpage
ReplyDeleteThank you very much sir.
ReplyDeletewhy shows is you can not master data some thng this
ReplyDeleteis source code complet?
ReplyDeleteNo i don't think so
Deletewrite ..string time = date.ToString("HH:mm:ss"); in Button1.click() function
ReplyDeleteand DateTime date = DateTime.Now;
DeleteWhen I write from one user the other user my message can seen only for 1second???
ReplyDeletecan u share your project
ReplyDeleteYour 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
ReplyDeletethanks for sharing, helps me alot
ReplyDeletewhy does db show an error to me?
ReplyDeletecan you please provide the source code ?
ReplyDelete