好友
阅读权限10
听众
最后登录1970-1-1
|
本帖最后由 marcus1982 于 2022-8-11 22:14 编辑
前台:
[HTML] 纯文本查看 复制代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StudentEdit.aspx.cs" Inherits="考试系统.teacher.StudentEdit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
学号:<input type="text" name="txtStudentId" value="<%=EditStudentInfo.StudentId %>" /><br />
姓名:<input type="text" name="txtStudentname" value="<%=EditStudentInfo.StudentName %>" /><br />
<input type="submit" value="编辑确认" />
</div>
</form>
</body>
</html>
后台:
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 考试系统.teacher
{
public partial class StudentEdit : System.Web.UI.Page
{
kaoshidbEntities db = new kaoshidbEntities();
public StudentInfo EditStudentInfo { set; get; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowEditStundenInfo();
}
else
{
UpdateStudentInfo();
}
}
protected void ShowEditStundenInfo()
{
string studentId = Request.QueryString["studentid"].ToString();
StudentInfo studentInfo = db.StudentInfo.SingleOrDefault(S => S.StudentId == studentId);
if (studentInfo != null)
{
EditStudentInfo = studentInfo;
}
else
{
Response.Redirect("/MyErrorPage.aspx");
}
}
protected void UpdateStudentInfo()
{
string studentId = Request.QueryString["studentid"].ToString();
StudentInfo studentInfo = db.StudentInfo.SingleOrDefault(S => S.StudentId == studentId);
if (studentInfo != null)
{
studentInfo.StudentId = Request.Form["txtStudentId"].ToString();
studentInfo.StudentName = Request.Form["txtStudentname"].ToString();
}
int r = db.SaveChanges();
if (r > 0)
{
Response.Write("<script languge='javascript'>alert('添加成功!');window.location.href='StudentManage.aspx'</script>");
}
else
{
Response.Redirect("/MyErrorPage.aspx");
}
}
}
}
提交后,出现以下错误,但是查看数据库后,修改也能成功。新手求救。
“/”应用程序中的服务器错误。 未将对象引用设置到对象的实例。说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 11: <form id="form1" runat="server">行 12: <div>行 13: 学号:<input type="text" name="txtStudentId" value="<%=EditStudentInfo.StudentId %>" /><br />行 14: 姓名:<input type="text" name="txtStudentname" value="<%=EditStudentInfo.StudentName %>" /><br />行 15: |
源文件: c:\kaoshi2022\考试系统\考试系统\teacher\StudentEdit.aspx 行: 13
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。] ASP.teacher_studentedit_aspx.__Renderform1(HtmlTextWriter __w, Control parameterContainer) in c:\kaoshi2022\考试系统\考试系统\teacher\StudentEdit.aspx:13 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +103 System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +177 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32 System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +360 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +128 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +287 System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +53 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +197 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +9 System.Web.UI.Page.Render(HtmlTextWriter writer) +30 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +128 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +287 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +27 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5628 |
版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.8.4494.0 |
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|