[求助]数据写入无效?
在用AJAX实现一个聊天室~但是在提交文本框输入的内容后,却没写入相应的表里~
存储过程:
CREATE PROCEDURE [dbo].[SendNewMsg]--获取输入的信息
(
@content varchar(300),
@user_to varchar(50),
@username varchar(50),
@face varchar(50),
@color varchar(50)
)
AS
insert into [Chat_Info] (content,user_from,user_to,face,color,composetime) values (@content,@username,@user_to,@face,@color,Getdate())
GO
============================================
WebForm1.aspx:
<%@ Page language= "c# " Codebehind= "WebForm1.aspx.cs " AutoEventWireup= "false " Inherits= "MyChatRoomTest.WebForm1 " %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN " >
<HTML>
<HEAD>
<title> WebForm1 </title>
<meta name= "GENERATOR " Content= "Microsoft Visual Studio .NET 7.1 ">
<meta name= "CODE_LANGUAGE " Content= "C# ">
<meta name= "vs_defaultClientScript " content= "JavaScript ">
<meta name= "vs_targetSchema " content= "http://schemas.microsoft.com/intellisense/ie5 ">
<script language= "jscript ">
////////////////////====下面的FUNCTION就是把文本框的内容写入到表里====////////
function SendNewMsg()
{
var txtContent =document.all( "content ");
if(txtContent== " ")return;
var user_to=document.all( "user_to ").value;
var face=document.all( "face ").value;
var color=document.all( "color ").value;
WebForm1.SendNewMsg(txtContent,user_to,face,color);
document.all( "content ").value= " ";//清空输入框;
} </script>
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id= "Form2 " method= "post " runat= "server ">
<FONT face= "宋体 "> </FONT>
<table width= "747 " height= "719 " border= "1 ">
<tr>
<td width= "141 " height= "659 "> </td>
<td width= "590 "> <div id= "chatcontent ">
欢迎来到聊天室~
</div>
</td>
</tr>
<tr>
<td height= "52 " colspan= "2 "> <form id= "form1 " name= "form1 " method= "post ">
<input type= "text " name= "content " onkeydown= "if (event.keyCode == 13) {SendNewMsg();return false;} "> <!--此为按ENTER即发送的方法 -->
表情
<select name= "face ">
<option value= "笑着 " selected>
笑着 </option>
</select>
发往
<select name= "user_to ">
<option value= "大家 " selected> 大家 </option>
</select>
颜色
<select name= "color ">
绝对黑色
<option style= "COLOR: #000080 " value= "000080 " selected>
忧郁的蓝 <option style= "COLOR: #808080 " value= "808080 ">
伦敦灰雾 <option style= "COLOR: #ccaa00 " value= "ccaa00 ">
卡布其诺 <option style= "COLOR: #800000 " value= "800000 ">
苦涩心红 <option style= "COLOR: #ff0000 " value= "ff0000 ">
正宗喜红 </option>
</select> </LABEL> <input type= "submit " onclick= "SendNewMsg();return false; " name= "Submit " value= "提交 ">
</form>
</td>
</tr>
</table>
</form>
</body>
</HTML>
===============================================================
Webform1.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace MyChatRoomTest
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(WebForm1));// 在此处放置用户代码以初始化页面
}
public string UserName= "shizumaru ";
[Ajax.AjaxMethod()]
public void SendNewMsg(string content,string user_to,string face,string color)
{
string aaa=@ "Data Source=localhost;user ID=sa;password=111;initial catalog=MyChatRoom ";
SqlConnection strconn=new SqlConnection(aaa);
SqlCommand sqlconn=strconn.CreateCommand();
sqlconn.CommandType=CommandType.StoredProcedure;
sqlconn.CommandText= "SendNewMsg ";
sqlconn.Parameters.Add( "@user_to ",user_to);
sqlconn.Parameters.Add( "@content ",content);
sqlconn.Parameters.Add( "@username ",UserName);
sqlconn.Parameters.Add( "@face ",face);
sqlconn.Parameters.Add( "@color ",color);
strconn.Open();
sqlconn.ExecuteNonQuery();
strconn.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
WEB.CONFIG里添加
<httpHandlers>
<add verb= "POST,GET " path= "ajax/*.ashx " type= "Ajax.PageHandlerFactory, Ajax " />
</httpHandlers>
[解决办法]
sqlconn.Parameters.Add( "@user_to ",user_to);
改成:
m_command.Parameters.Add( "@user_to ",SqlDbType.NVarChar,40).Value=user_to
[解决办法]
CREATE PROCEDURE [dbo].[SendNewMsg]--获取输入的信息
AS
insert into [Chat_Info] values (@content,@username,@user_to,@face,@color,Getdate())
GO
这样如何
[解决办法]
打断点啊 一句句找 看看 insert 那边执行的怎么样