Simple Chat Application in ASP.NET


Friends, all of you are familiar with the Global.asax file in ASP.Net; if you don't then first read this about Global.asax:

discuss the various events in Global.asax file.

Global Application Class [Global.asax]

Application state only achieved by adding Global.asax file into our project; it is not mandatory to define a Global.asax file web project and if you have not added a Global.asax file then the ASP.NET Runtime detects that there was no Application State mangement in the current project.

 Events in Global.asax File

Some of the Events in the Global.asax file are depicted as follows; this is not a complete list of events.

 Application_Start: This event fires executes when the application starts running

 Application_End: This event ocurs on application shutdown

 Application_Error: This event ocurs when unhandled error occurs

 Session_Start: This event ocurs when new session is started.

 Session_End: This event occurs when session ends

So now we see the steps for the chat application:

Step 1) Open Visual Studio 2008/10

Step 2)Add wweb  two web form Default.aspx and Chating.aspx ok

Step 3) Now Right click on the project and Add New Item; in that select the Global.asax file and in it write the following:

{
<%@ Application Language="C#" %> 

<script runat="server">

    
void Application_Start(object sender, EventArgs e)
     {
         // Code that runs on application startup
         Application.Lock();
         Application["msg"]="";
         Application.UnLock();
     }   

    void Application_End(object sender, EventArgs e)
     {
         //  Code that runs on application shutdown
     }

    void Application_Error(object sender, EventArgs e)
     {
         // Code that runs when an unhandled error occurs
     } 

    void Session_Start(object sender, EventArgs e)
     {
         // Code that runs when a new session is started
     }

    void Session_End(object sender, EventArgs e)
     {
         // Code that runs when a session ends.
         // Note: The Session_End event is raised only when the sessionstate mod
         // is set to InProc in the Web.config file. If session mode is set to StateServer
         // or SQLServer, the event is not raised.
     }      

</script>

}

  Step 4) Now go to the Default.aspx page source code and write as following:

{

     <form id="form1" runat="server">
     <script language="jscript" type="text/javascript">
     function popup()
     {
        var a=window.open("chating.aspx",'width=250,height=500,left=350,top=120')
     }
     </script>
     <body onload="popup(); return false;">
}

Step 5) Now design the Chating.aspx page along with three textboxes and one button.

Step 6) Now go to the Chating.aspx page_load event and wrte code as follows:

{

     //Assigning Application Object to string Varaiable by Parsing

     string msg = (string)Application["msg"]; 

     TextBox1.Text = msg;
}

Step 7) Now go to the Chating.aspx button's double-click event and write code as follows:

{
string name = TextBox2.Text;
        string message = TextBox3.Text;
        string my = name + "::" + message; 

        Application["msg"] = Application["msg"] + my + Environment.NewLine;

        TextBox1.Text = Application["msg"].ToString();

       
TextBox3.Text = "";
}

Step 8) Now run the application in two browsers and verify that the Chat works.

Up Next
    Ebook Download
    View all
    Learn
    View all