如何在测验制作web应用程序中修复此错误
本文关键字:应用程序 错误 web | 更新日期: 2023-09-27 18:16:48
我在ASP中开发了一个类似的测验引擎。. NET网站,但我只是添加了一个新的表来设计数据库。数据库的原始设计由以下三个表组成:
测验表:测验id,标题,描述
问题表: QuestionID, Question, Answer1, Answer2, Answer3, Answer4, CorrectAnswer, answerexplain, QuestionOrder, QuizID
UserQuiz Table: UserQuizID, QuizID, DateTimeComplete, Score, Username
每个表中的第一个属性是该表的主键
我所做的只是添加一个新表,它是:
User Table: Username, Name, Job, Address (Username为主键)
对于代码隐藏,我没有触及任何东西,但是当我试图执行代码时,它给了我一个错误。后面的代码是:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Results : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ArrayList al = (ArrayList)Session["AnswerList"];
if (al == null)
{
Response.Redirect("default.aspx");
}
resultGrid.DataSource = al;
resultGrid.DataBind();
// Save the results into the database.
if (IsPostBack == false)
{
// Calculate score
double questions = al.Count;
double correct = 0.0;
for (int i = 0; i < al.Count; i++)
{
Answer a = (Answer)al[i];
if (a.Result == Answer.ResultValue.Correct)
correct++;
}
double score = (correct / questions) * 100;
SqlDataSource userQuizDataSource = new SqlDataSource();
userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString();
userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [UserName]) VALUES (@QuizID, @DateTimeComplete, @Score, @UserName)";
userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString());
userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
userQuizDataSource.InsertParameters.Add("Score", score.ToString());
userQuizDataSource.InsertParameters.Add("UserName", User.Identity.Name);
int rowsAffected = userQuizDataSource.Insert();
if (rowsAffected == 0)
{
// Let's just notify that the insertion didn't
// work, but let' s continue on ...
errorLabel.Text = "There was a problem saving your quiz results into our database. Therefore, the results from this quiz will not be displayed on the list on the main menu.";
}
}
}
protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue;
}
}
错误:
INSERT语句与外键约束"FK_UserQuiz_employee"冲突。冲突发生在数据库"psspdb",表"dbo"。employee",列"Username"。语句已被终止。
源错误:
<>之前第50行:userQuizDataSource.InsertParameters。添加("用户名",User.Identity.Name);51行:第52行:int rowsAffected = userQuizDataSource.Insert()第53行:if (rowsAffected == 0)54行:之前UserQuiz表中的Username是User表中Username的外键。我知道,但是我应该做什么来解决这个问题
要添加的用户名在Employee表中不可用(当前)。请验证User.Identity.Name
属性的用户名
User.Identity。名称不在Users表中。您可以检查if User.Identity。名称在插入前已存在于Users表中。
编辑
如果用户存在,可以创建返回true的函数,否则返回false。
public bool UserExists(string UserName)
{
DataTable table = new DataTable("table");
string connectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "SELECT COUNT(UserName) FROM User WHERE UserName = " + UserName;
try
{
connection.Open();
object userName = command.ExecuteScalar();
return userName != null;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
则插入逻辑变为:
if(UserExists(User.Identity.Name))
{
// Your insert logic goes here...
}
注:此命令用于演示,不安全。"SELECT COUNT(UserName) FROM User WHERE UserName = " + UserName