Click here to Skip to main content
15,881,715 members
Articles / Web Development / ASP.NET

A simple ASP.NET chat

Rate me:
Please Sign up or sign in to vote.
3.90/5 (21 votes)
18 Feb 2007CPOL2 min read 453.5K   5K   27   35
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.

VB
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:

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

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

VB
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)


Written By
Technical Lead
India India
have been working in web technologies for the last 8 years.

Comments and Discussions

 
GeneralRe: private chat application [modified] Pin
G.Suman Babu23-Jun-07 1:18
G.Suman Babu23-Jun-07 1:18 
GeneralRe: private chat application Pin
karthikeyan_caleb24-Jun-07 22:52
karthikeyan_caleb24-Jun-07 22:52 
Questionhow can i create rooms Pin
manikandanpro21-Jun-07 1:21
manikandanpro21-Jun-07 1:21 
Generalprivate chat not working ? pls help me Pin
karthikeyan_caleb18-Jun-07 17:03
karthikeyan_caleb18-Jun-07 17:03 
GeneralRe: private chat not working ? pls help me Pin
manikandanpro21-Jun-07 1:18
manikandanpro21-Jun-07 1:18 
GeneralRe: private chat not working ? pls help me Pin
manikandanpro21-Jun-07 1:26
manikandanpro21-Jun-07 1:26 
GeneralASP.Net Chat Pin
sandy197114-Mar-07 23:04
sandy197114-Mar-07 23:04 
GeneralRe: ASP.Net Chat Pin
sathesh pandian22-Jun-07 0:22
sathesh pandian22-Jun-07 0:22 
QuestionHi Pin
Member 252898527-Feb-07 1:00
Member 252898527-Feb-07 1:00 
AnswerRe: Hi Pin
sathesh pandian2-Mar-07 2:45
sathesh pandian2-Mar-07 2:45 

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

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