2401_83786250 2024-04-25 18:11 采纳率: 0%
浏览 1

使用ASP.NET用c#写奖学金评选系统,这个是学生登录后进入学生页面点击"我的信息"查看自己详细信息.我用的是SQL server数据库

        if (!IsPostBack)
        {
            // 从数据库中获取学生信息
            string connectionString = "Data Source=localhost;Initial Catalog=奖学金评定系统;Integrated Security=True"; // 这里替换为你的数据库连接字符串
            string query = "SELECT * FROM Student WHERE 学号 = @StudentID";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    // 假设学生的学号存储在 Session 中
                    string studentID = Session["学号"].ToString();

                    command.Parameters.AddWithValue("@StudentID", studentID);

                    try
                    {
                        connection.Open();
                        SqlDataReader reader = command.ExecuteReader();

                        if (reader.Read())
                        {
                            // 读取学生信息
                            string 学号 = reader["学号"].ToString();
                            string 姓名 = reader["姓名"].ToString();
                            string 性别 = reader["性别"].ToString();
                            string 学院 = reader["学院"].ToString();
                            int 成绩 = Convert.ToInt32(reader["成绩"]);
                            string 辅导员 = reader["辅导员"].ToString();
                            string 身份证号 = reader["身份证号"].ToString();


                            // 将学生信息存储到 Session 中
                            Session["学号"] = 学号;
                            Session["姓名"] = 姓名;
                            Session["性别"] = 性别;
                            Session["学院"] = 学院;
                            Session["成绩"] = 成绩;
                            Session["辅导员"] = 辅导员;
                            Session["身份证号"] = 身份证号;
                        }
                        else
                        {
                            // 处理未找到学生信息的情况
                        }

                        reader.Close();
                    }
                    catch (Exception ex)
                    {
                        // 处理异常情况
                    }
                }
            }
        }

img


该怎么改啊?求帮帮我!

  • 写回答

6条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-25 18:11
    关注

    引自免费微信小程序:皆我百晓生

    看起来你在尝试从Session中获取学生ID,然后使用它来查询数据库中的学生信息。但是,你在第27行尝试访问Session["学号"]时遇到了一个NullReferenceException,这通常意味着Session["学号"]可能为null,也就是说,在访问该值之前,Session中并没有名为“学号”的项。

    要解决这个问题,你需要确保在尝试从Session中获取数据之前,已经设置了相应的键值对。可以在用户登录成功后立即保存学号到Session中,或者在加载学生个人信息页面前验证用户是否已登录。下面是一个修改后的示例代码,包含了这些验证逻辑:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // 验证用户是否已登录
            if (Session["学号"] == null)
            {
                Response.Redirect("Login.aspx"); // 如果未登录,重定向到登录页
            }
            else
            {
                string studentID = Session["学号"].ToString();
    
                // 从数据库中获取学生信息
                string connectionString = "Data Source=localhost;Initial Catalog=奖学金评定系统;Integrated Security=True";
                string query = "SELECT * FROM Student WHERE 学号 = @StudentID";
    
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@StudentID", studentID);
    
                        try
                        {
                            connection.Open();
                            SqlDataReader reader = command.ExecuteReader();
    
                            if (reader.Read())
                            {
                                // 读取学生信息
                                string 学号 = reader["学号"].ToString();
                                string 姓名 = reader["姓名"].ToString();
                                string 性别 = reader["性别"].ToString();
                                string 学院 = reader["学院"].ToString();
                                int 成绩 = Convert.ToInt32(reader["成绩"]);
                                string 辅导员 = reader["辅导员"].ToString();
                                string 身份证号 = reader["身份证号"].ToString();
    
                                // 更新Session中的学生信息
                                Session["学号"] = 学号;
                                Session["姓名"] = 姓名;
                                Session["性别"] = 性别;
                                Session["学院"] = 学院;
                                Session["成绩"] = 成绩;
                                Session["辅导员"] = 辅导员;
                                Session["身份证号"] = 身份证号;
    
                                // 在这里显示或操作学生信息
                            }
                            else
                            {
                                // 处理未找到学生信息的情况
                                Response.Write("找不到与提供的学号匹配的学生信息");
                            }
    
                            reader.Close();
                        }
                        catch (Exception ex)
                        {
                            // 处理异常情况
                            Response.Write("数据库查询时发生错误:" + ex.Message);
                        }
                    }
                }
            }
        }
    }
    

    这段代码首先检查了Session中是否存在“学号”,如果没有,则重定向到登录页面。如果有,再进行数据库查询。这样可以防止因Session中没有“学号”而导致的NullReferenceException

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月25日

悬赏问题

  • ¥15 Unity 2022.3.34版本安卓打包apk失败,gradle配置问题,用的是mono2x
  • ¥15 R语言中安装bibliometrix 后运行biblioshiny出现问题
  • ¥20 关于#android#的问题:用开发助手发现找不到控件(autojs)
  • ¥15 dir815漏洞反弹shell失败
  • ¥15 支付宝小程序云函数登录获取user_id失败
  • ¥50 python for 循环速度慢
  • ¥15 CubeMX生成的代码用keil编译有报错
  • ¥15 Stata链式中介效应代码修改
  • ¥15 pip安装PyAV报错
  • ¥15 latex投稿显示click download