Hello friends,
Today I will be explain to you that how we
can develop a group chat application for our website with the help of c#
,asp.net and SQL Server. Where people after submitting their name, they can
start text chat with known and unknown person without sharing any type signup
or registration.
I will be explaining all development activities one by one.
Step 1: First of I will be design database.
CREATE TABLE [dbo].[Livegroupcaht](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NULL,
[comment] [nvarchar](250) NULL,
[createdate] [datetime] NULL
) ON [PRIMARY]
Storeprocedure :
Create PROCEDURE Usp_Chat
@Action nvarchar(50)='',
@name nvarchar(50)='',
@comment nvarchar(150)=''
As
BEGIN
-- SET NOCOUNT ON added to prevent
extra result sets from
-- interfering with SELECT
statements.
SET NOCOUNT
ON;
-- Insert
statements for procedure here
if(@Action='insertgroupcaht')
begin
insert into dbo.Livegroupcaht (name,comment,createdate) values(@name,@comment,GETDATE())
end
if(@Action='displaychatconvertaion')
begin
select * from dbo.Livegroupcaht order by ID asc
end
END
Step 2: create a page chat.aspx.
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="chat.aspx.cs"
Inherits="chat"
%>
<%@ Register
Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit"
TagPrefix="asp"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<div>
<form id="form2" runat="server"><div>
<table style=" width:600PX; height:150px; border:1px solid
#d5d5d5;"><tr><td><table cellpadding="0"
cellspacing="0">
<tr id="tr1" runat="server"><td>Enter your
Name</td><td><asp:TextBox ID="txtanme"
runat="server"
MaxLength="50"></asp:TextBox><asp:Button ID="cmdstar" runat="server" Text="Start Chat"
onclick="cmdstar_Click" ValidationGroup="abc"/><asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="Enter
name!" ControlToValidate="txtanme" ValidationGroup="abc"></asp:RequiredFieldValidator></td></tr>
<tr><td><table id="table1" runat="server"
visible="false">
<tr><td colspan="2"><iframe src="Default.aspx" style=" border:1px solid #d5d5d5; height:150px;"></iframe></td></tr>
<tr><td style="vertical-align:middle"><asp:Label ID="lblme" runat="server" Text="Me" ></asp:Label><asp:Label ID="lblname" runat="server" Visible="false"></asp:Label><asp:TextBox ID="txtcomment"
runat="server"
TextMode="MultiLine"
MaxLength="150"
></asp:TextBox></td><td><asp:Button ID="cmdsend"
runat="server" Text="Send" onclick="cmdsend_Click"/></td></tr>
<tr><td></td></tr>
<tr><td align="center"> </td></tr></table></td></tr>
</table></td></tr></table>
</div>
</form>
</div>
</body>
</html>
Step 3: Chat.cs page
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Data.SqlClient;
public partial class chat :
System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection con = new
SqlConnection();
SqlCommand cmd = new
SqlCommand();
SqlDataAdapter adpt = new
SqlDataAdapter();
Hashtable hst = new
Hashtable();
DataTable dt = new
DataTable();
DataSet ds = new DataSet();
protected void
Page_Load(object sender, EventArgs e)
{
}
public DataSet
GetDataSet(string spName, Hashtable hst)
{
con =
new SqlConnection();
cmd =
new SqlCommand();
con.ConnectionString = constr;
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = spName;
con.Open();
if (hst != null)
if (hst.Count > 0)
AttachParameters(cmd, hst);
DataSet dataset = new
DataSet();
SqlDataAdapter adapter = new
SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(dataset);
con.Close();
return dataset;
}
private void
AttachParameters(SqlCommand command, Hashtable hstParameters)
{
try
{
IEnumerator parameters =
hstParameters.Keys.GetEnumerator();
while (parameters.MoveNext())
{
SqlParameter param = new SqlParameter();
param.ParameterName = "@" +
parameters.Current;
param.Value = hstParameters[parameters.Current];
command.Parameters.Add(param);
}
}
catch (Exception
exception)
{
HttpContext.Current.Response.Write("Error : " + exception.Message + " </br> Source : " +
exception.Source);
}
}
protected void
cmdstar_Click(object sender, EventArgs e)
{
lblname.Text = txtanme.Text;
table1.Visible = true;
tr1.Visible = false;
}
protected void
cmdsend_Click(object sender, EventArgs e)
{
hst.Clear();
ds.Clear();
hst.Add("action", "insertgroupcaht");
hst.Add("name",
lblname.Text);
hst.Add("comment",
txtcomment.Text);
ds =
GetDataSet("[Usp_Chat]", hst);
txtcomment.Text = "";
}
}
Now I will be take
another page default.aspx page, where I will be fetch all information from
database and bind this information with the help of Repeater control. And refresh
page after every 10 second with the help of Timer control.
Step 4: default.aspx
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<%@ Register
Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit"
TagPrefix="asp"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager
ID="ToolkitScriptManager1"
runat="server">
</asp:ToolkitScriptManager><asp:UpdatePanel ID="UpdatePanel1"
runat="server"><ContentTemplate>
<div>
<asp:Repeater ID="repdata"
runat="server"><ItemTemplate><table><tr><td><asp:Label ID="lblstu" ForeColor="Black" Font-Bold="true" runat="server" Text='<%#Eval("name") %>'></asp:Label> is saying : <asp:Label ID="Label1" runat="server" Text='<%#Eval("comment") %>'></asp:Label></td></tr></table></ItemTemplate></asp:Repeater>
</div><asp:Timer ID="timer1" runat="server"
Interval="10"
Enabled="true"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Step 6:default.cs page
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Data.SqlClient;
public partial class _Default :
System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection con = new
SqlConnection();
SqlCommand cmd = new
SqlCommand();
SqlDataAdapter adpt = new
SqlDataAdapter();
Hashtable hst = new
Hashtable();
DataTable dt = new
DataTable();
DataSet ds = new DataSet();
protected void
Page_Load(object sender, EventArgs e)
{
chatconvertation();
}
public DataSet GetDataSet(string
spName, Hashtable hst)
{
con =
new SqlConnection();
cmd =
new SqlCommand();
con.ConnectionString = constr;
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = spName;
con.Open();
if (hst != null)
if (hst.Count > 0)
AttachParameters(cmd, hst);
DataSet dataset = new
DataSet();
SqlDataAdapter adapter = new
SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(dataset);
con.Close();
return dataset;
}
private void
AttachParameters(SqlCommand command, Hashtable hstParameters)
{
try
{
IEnumerator parameters =
hstParameters.Keys.GetEnumerator();
while (parameters.MoveNext())
{
SqlParameter param = new SqlParameter();
param.ParameterName = "@" +
parameters.Current;
param.Value = hstParameters[parameters.Current];
command.Parameters.Add(param);
}
}
catch (Exception
exception)
{
HttpContext.Current.Response.Write("Error : " + exception.Message + " </br> Source : " + exception.Source);
}
}
public void
chatconvertation()
{
hst.Clear();
ds.Clear();
hst.Add("Action", "displaychatconvertaion");
ds =
GetDataSet("Usp_Chat", hst);
if (ds.Tables[0].Rows.Count > 0)
{
repdata.DataSource = ds;
repdata.DataBind();
}
}
}
This is the Snapshot of this application
You can download full project code from here.
No comments:
Post a Comment