Click here to Skip to main content
11,412,516 members (68,919 online)
Click here to Skip to main content

A simple ASP.NET chat

, 18 Feb 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple chat application using ASP.NET.

Introduction

This article describes how to create a chat application. In this application, a total of six pages are used.

Main Page

This page has three frames. The first frame will show the user information and the typed messages (left side). The right side frame will show how many users are online now and their names. The bottom side of the frame will show a textbox with a logout button.

There are three types of objects to store the user information and data:

  • Session
  • Cookies
  • Application

The Session object is used for storing a particular user's information in the server side. The Cookies object is used for storing a particular user's information in a particular client side. And, the Application object can be used across the network.

In this application, I have used the Session object for the login method.

session("UserName")=txtUserName.text

This will create a Session object in the server side. We can check the user's credentials by using this object.(Authentication).

After checking the user's credentials, the application object is created like this:

Application("Chat")=Application("Chat") & name & "$"

If the user types something in the textbox, the form gets refreshed and the Application("chat") value is changed with the new value, and the list of Application("chat") arrays are shown in the main display page.

Th user's input will displayed by using the auto-refresh method in the meta HTTP object.

<meta http-equiv="refresh" content="4">

This will make the page refresh.

List of Users

Application("Users") is used to count the number of users online. The count is taken by splitting the special characters in application("users").

Screenshot - chat.png

Logout

This logout page is used to clear the session of the user and remove the user name from application("users"). So, the user can close his session from the chat. For private chat, application(user & session("user")) is used to differentiate the other users from the current user.

The left side of the main page shows all the users; when we click on a user, the private page will show a new page with the requested user.

if Con.State = ConnectionState.Open Then Con.Close()
Con.Open()
name = rdname(0)
Con.State = ConnectionState.Open Then Con.Close()Dim cmd_name As New SqlCommand(
   "Select Emp_Name From UserAccounts_Master Where UserId = '" & Session("UserId") & "'",
   Con)Dim rdname As SqlDataReader = cmd_name.ExecuteReaderWhile rdname.ReadEnd While 
rdname.Close()
If name <> "" Then If Application("invite") = name Then 
Response.Write("<script language="'javascript'">{ window.alert(
    '" & Application("invite") & "," & Application("whom") & " invited you to chat') } 
</script>")
Application("invite") = " "
'Dim focusString As String = ("<script language="'javascript'">{ window.alert(
   '" & Application("invite") & "," & Application("whom") & " invited you to chat') }
</script>") 'Page.RegisterStartupScript("FocusScript",
    focusString) 'Session.Timeout = 20000 End If End If 
rdname.Close()
Con.Close()

Try this. Thanks!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sathesh pandian
Technical Lead
India India
have been working in web technologies for the last 8 years.

Comments and Discussions

 
Questionproblem in build Pin
breemo at 6-Feb-13 3:56
memberbreemo6-Feb-13 3:56 
GeneralMy vote of 3 Pin
rkumar670 at 22-Feb-11 19:54
memberrkumar67022-Feb-11 19:54 
GeneralC# Pin
driftking at 18-Feb-09 23:15
memberdriftking18-Feb-09 23:15 
GeneralRe: C# Pin
sathesh_pandian at 19-Feb-09 0:16
membersathesh_pandian19-Feb-09 0:16 
GeneralRe: C# Pin
Member 7682375 at 17-Feb-11 6:23
memberMember 768237517-Feb-11 6:23 
GeneralRe: C# Pin
sathesh_pandian at 24-Feb-11 19:16
membersathesh_pandian24-Feb-11 19:16 
Generalhelp me Pin
chandrashekharsani at 14-Nov-08 0:25
memberchandrashekharsani14-Nov-08 0:25 
GeneralRe: help me Pin
sathesh_pandian at 15-Nov-08 5:16
membersathesh_pandian15-Nov-08 5:16 
Generalunable to run private chat code Pin
chandrashekharsani at 17-Oct-08 0:37
memberchandrashekharsani17-Oct-08 0:37 
Generalnot able to download sample Pin
namrshri at 29-Sep-08 3:10
membernamrshri29-Sep-08 3:10 
Generalplz help Pin
harivinod at 13-Feb-08 15:26
memberharivinod13-Feb-08 15:26 
GeneralRe: plz help Pin
sravanibhanu at 19-Mar-08 22:33
membersravanibhanu19-Mar-08 22:33 
GeneralRe: plz help Pin
sathesh_pandian at 21-Apr-08 23:21
membersathesh_pandian21-Apr-08 23:21 
GeneralPrivate Chat HELP!!!!! Pin
arindam.roy at 27-Oct-07 0:56
memberarindam.roy27-Oct-07 0:56 
Questionhow to notify other user when one is typing message. Pin
sai412 at 9-Aug-07 22:37
membersai4129-Aug-07 22:37 
Questionhow to notify other user when one is typing message. Pin
sai412 at 9-Aug-07 22:35
membersai4129-Aug-07 22:35 
AnswerRe: how to notify other user when one is typing message. Pin
sathesh_pandian at 14-Aug-07 2:46
membersathesh_pandian14-Aug-07 2:46 
Generali want clear the single user page only on multi user page chat application Pin
karthikeyan_caleb at 11-Jul-07 18:11
memberkarthikeyan_caleb11-Jul-07 18:11 
GeneralRe: i want clear the single user page only on multi user page chat application Pin
sathesh_pandian at 12-Jul-07 21:11
membersathesh_pandian12-Jul-07 21:11 
Questionhow to clear the current login user page Pin
karthikeyan_caleb at 24-Jun-07 23:50
memberkarthikeyan_caleb24-Jun-07 23:50 
AnswerRe: how to clear the current login user page Pin
sathesh_pandian at 27-Jun-07 19:39
membersathesh_pandian27-Jun-07 19:39 
Generalprivate chat application Pin
karthikeyan_caleb at 21-Jun-07 21:37
memberkarthikeyan_caleb21-Jun-07 21:37 
GeneralRe: private chat application Pin
sathesh_pandian at 22-Jun-07 1:15
membersathesh_pandian22-Jun-07 1:15 
GeneralRe: private chat application Pin
karthikeyan_caleb at 22-Jun-07 2:34
memberkarthikeyan_caleb22-Jun-07 2:34 
GeneralRe: private chat application Pin
adarshr_ushus at 20-Jun-08 1:47
memberadarshr_ushus20-Jun-08 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150427.1 | Last Updated 19 Feb 2007
Article Copyright 2007 by sathesh pandian
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid