ajax+asp.net+mssql no refresh chat room
Ajax non-refresh chat room implemented by ajax+asp.net+mssql, supports html web editor. For your reference. However, there is a small problem, that is, when using the shortcut key ctrl+enter to send information under Firefox, if the sending function is not called with a delay, the ff download dialog box will pop up, which is strange.
JScript codeif(e.ctrlKey&&e.keyCode==13){
e.preventDefault();
e.stopPropagation();
setTimeout("Showbo.Chat.send()",50);//When the alert prompt is used immediately in ff, the download tool pops up, which is strange.
return false;}
},false);
To speed things up, stored procedures are used.
Please see the following article for asp and php versions
ajax+asp+mssql no refresh chat room
ajax+php+mssql no refresh chat room
To use different dynamic pages, just change the value of the RequstUrl variable in the lib.js file.
Full sample download
The effect is as follows
There is a content length limit, so I won’t post all the code. To see all the code, please download the example or view the following article.
ajax+asp.net+mssql no refresh chat room
The main code of C# is posted below. For other versions, please check the link above.
ajax.cs
C# codeusing System;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
public class Ajax
{
private static bool IsNull(string v)
{
if (v == null || v.Trim() == "") return true;
else return false;
}
private static string Js(string v)
{
return v.Replace("'", "'");
}
public static string Login()
{
HttpRequest Request = HttpContext.Current.Request;
string rStr = "";
string UserName = Request.Form["nn"];
if (IsNull(UserName))
{
rStr = "success:false,err:'The nickname cannot be empty!'";
}
else if (UserName.Length > 20)
{
rStr = "success:false,err:'The nickname cannot exceed 20 characters!'";
}
else
{
string UserId = "", Key = "";
SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["db"]);
cn.Open();
try
{
SqlCommand cm = new SqlCommand("ajaxLogin", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, 50));
cm.Parameters["@UserName"].Value = UserName;
//==========Output parameters
cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
cm.Parameters["@UserId"].Direction = ParameterDirection.Output;
cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
cm.Parameters["@UserKey"].Direction = ParameterDirection.Output;
cm.ExecuteNonQuery();
UserId = cm.Parameters["@UserId"].Value.ToString().Trim();
Key = cm.Parameters["@UserKey"].Value.ToString().Trim();
if (UserId == "-1") rStr = @"success:false,err:'An error occurred, please try again later!'";
else if (UserId == "0") rStr = @"success:false,err:'This user nickname already exists, please modify your nickname!'";
else rStr += "success:true,UserId:'" + UserId + "',Key:'" + Key + "'";
cm.Dispose();
}
catch (Exception e)
{
rStr = @"success:false,err:'reason n" + Js(e.Message) + "'";
}
cn.Close();
}
return rStr;
}
public static string Logout()
{
HttpRequest Request = HttpContext.Current.Request;
string rStr = "", UserId = Request.Form["uid"], Key = Request.Form["key"];
if (IsNull(UserId) || IsNull(Key)) return "success:false,err:'User information lost!'";
SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["db"]);
cn.Open();
try
{
SqlCommand cm = new SqlCommand("ajaxLogout", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
cm.Parameters["@UserId"].Value = UserId;
cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
cm.Parameters["@UserKey"].Value = Key;
cm.Parameters.Add(new SqlParameter("@Result", SqlDbType.Int));
cm.Parameters["@Result"].Direction = ParameterDirection.Output;
cm.ExecuteNonQuery();
if(cm.Parameters["@UserId"].Value.ToString().Trim()=="0")rStr = "success:false,err:'User information does not exist!'";
else rStr="success:true";
cm.Dispose();
}
catch (Exception e)
{
}
cn.Close();
return rStr;
}
public static string Say()
{
HttpRequest Request = HttpContext.Current.Request;
string From = Request.Form["from"], To = Request.Form["to"]
, Key = Request.Form["key"], Msg = Request.Form["ct"], rStr = "";
if (IsNull(From) || IsNull(Key) || IsNull(To) || IsNull(Msg)) rStr = "success:false,err:'Incomplete information transfer!'";
else
{
SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["db"]);
cn.Open();
try
{
SqlCommand cm = new SqlCommand("ajaxSay", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
cm.Parameters["@UserKey"].Value = Key;
cm.Parameters.Add(new SqlParameter("@From", SqlDbType.NVarChar, 18));
cm.Parameters["@From"].Value = From;
cm.Parameters.Add(new SqlParameter("@To", SqlDbType.NVarChar, 18));
cm.Parameters["@To"].Value = To;
cm.Parameters.Add(new SqlParameter("@Msg", SqlDbType.NVarChar, 800));
cm.Parameters["@Msg"].Value = Msg;
cm.Parameters.Add(new SqlParameter("@Result", SqlDbType.Int));
cm.Parameters["@Result"].Direction = ParameterDirection.Output;
cm.ExecuteNonQuery();
if (cm.Parameters["@Result"].Value.ToString() == "0") rStr = "sucess:false,err:'Publishing failed! Reason: The receiver no longer exists!'";
else rStr = "success:true";
cm.Dispose();
}
catch (Exception e)
{
rStr = "sucess:false,err:'Publishing failed! Reason n" + Js(e.Message) + "'";
}
cn.Close();
}
return rStr;
}
public static string ReadUser()
{
HttpRequest Request = HttpContext.Current.Request;
string rStr = "", UserId = Request.Form["uid"];
if (IsNull(UserId)) rStr += "success:false,err:'User id is lost!'";
else
{
SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["db"]);
cn.Open();
try
{
SqlCommand cm = new SqlCommand("ajaxReadUser", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
cm.Parameters["@UserId"].Value = UserId;
string j = "";
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read()) j += ",{id:'" + dr[0] + "',nn:'" + Js(dr[1].ToString()) + "'}";
dr.Close();
cm.Dispose();
rStr = "success:true,data:[" + (j == "" ? "" : j.Substring(1)) + "]";
}
catch (Exception e)
{
rStr = @"success:false,err:'The following error occurred n" + Js(e.Message) + "'";
}
cn.Close();
}
return rStr;
}
public static string Read()
{
HttpRequest Request = HttpContext.Current.Request;
string rStr = "";
string UserId = Request.Form["uid"], Key = Request.Form["key"];
if (IsNull(UserId) || IsNull(Key)) rStr = "success:false,err:'User information lost!'";
else
{
SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["db"]);
cn.Open();
try
{
SqlCommand cm = new SqlCommand("ajaxRead", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
cm.Parameters["@UserId"].Value = UserId;
cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
cm.Parameters["@UserKey"].Value = Key;
SqlDataReader dr = cm.ExecuteReader();
string j = "";
while (dr.Read()) j += ",'" + Js(dr[0].ToString()) + "'";
dr.Close();
cm.Dispose();
rStr = "success:true,data:[" + (j == "" ? "" : j.Substring(1)) + "]";
}
catch (Exception e)
{
rStr = "success:false,err:'The following error occurred" + Js(e.Message) + "'";
}
cn.Close();
}
return rStr;
}