我正在使用 AJAX、LINQ to SQL 和一些安全功能在 ASP.NET 中制作聊天应用程序以进行身份验证。聊天将根据特定组进行。尝试登录聊天页面后出现错误(默认.aspx,在我的项目中),错误是:
“INSERT 语句与 FOREIGN KEY 约束“FK_tbmsg_tbusr2”冲突。冲突发生在数据库“test”、表“dbo.tbusr”、列“usrcod”中。语句已终止。”
注意:tbmsg 是用于存储消息数据的表,tbusr 是用于存储用户数据的表。Default.aspx 的后端代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
chatDataContext obj = new chatDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
Label1.Text = "Welcome:" + Session["usrnam"].ToString();
Insert_Msg("Just Login:");
GetLoggedUser();
Get_Msg();
}
}
private void Insert_Msg(String m)
{
tbmsg k = new tbmsg { msgdsc = m, msgdat = DateTime.Now, msgfrmusrcod = Convert.ToInt32(Session["usrcod"]), msgtousrcod = null, msggrpcod = Convert.ToInt32(Request.QueryString["grpcod"]) };
obj.tbmsgs.InsertOnSubmit(k);
obj.SubmitChanges();
}
private void Get_Msg()
{
var q = from p in obj.tbmsgs
where p.msggrpcod == Convert.ToInt32(Request.QueryString["grpcod"])
select p;
StringBuilder sb = new StringBuilder();
foreach (var t in q)
{
sb.Append(t.msgdsc + ":" + t.msgdat.ToString() + ":" + t.tbusr.usrnam + "<br/>");
}
ltmsg.Text = sb.ToString();
}
private void GetLoggedUser()
{
var q = (from p in obj.tblogusrs where p.logusrusrcod == Convert.ToInt32(Session["usrcod"]) select p).SingleOrDefault();
if (q == null)
{
tblogusr x = new tblogusr { logusrusrcod = Convert.ToInt32(Session["usrcod"]), logusrgrpcod = Convert.ToInt32(Request.QueryString["grpcod"]) };
obj.tblogusrs.InsertOnSubmit(x);
obj.SubmitChanges();
}
StringBuilder sb = new StringBuilder();
var q1 = from p in obj.tblogusrs where p.logusrgrpcod == Convert.ToInt32(Request.QueryString["grpcod"]) select p;
foreach (var t in q1)
{
sb.Append(t.tbusr.usrnam + "<br/>");
}
ltuser.Text = sb.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Length > 0)
{
Insert_Msg(TextBox1.Text);
TextBox1.Text = String.Empty;
TextBox1.Focus();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
var q = (from p in obj.tblogusrs where p.logusrusrcod == Convert.ToInt32(Session["usrcod"]) select p).SingleOrDefault();
if (q != null)
{
obj.tblogusrs.DeleteOnSubmit(q);
obj.SubmitChanges();
}
Insert_Msg("Just Logout:");
FormsAuthentication.SignOut();
Response.Redirect("login.aspx");
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Get_Msg();
GetLoggedUser();
}
}
HTML 代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1
{
width: 70%;
height: 25px;
}
.auto-style2
{
height: 25px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 286px; width: 849px">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
<table border="5" style="width: 100%; margin-right: 0px">
<tr>
<td width="30%" class="auto-style2">
<asp:Literal ID="ltuser" runat="server" />
</td>
<td class="auto-style1">
<asp:Literal ID="ltmsg" runat="server" />
</td>
</tr>
</table>
<br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send" />
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Logout" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
tbusr 表说明:
usrcod int primary key (auto increment)
usrnam varchar(50)
usrpwd varchar(50)
tbmsg 表说明:
msgcod int primary key (auto increment)
msgdsc varchar(100)
msgdat datetime
msgfrmusrcod int (foreign key with usrcod of tbusr)
msgtousrcod int (foreign key with usrcod of tbusr)
msggrpcod int (foreign key with grpcod of tbgrp)
注意:tbgrp 是另一个用于存储组数据的表