asp.net结课作业中遇到的问题解决7

1、从客户端(ctl00$ContentPlaceHolder1$TextBox4="<p>一位真正的作家永远只为内心写作,只...")中检测到有潜在危险的 Request.Form 值。这个问题的解决

具体的修改步骤可以看一下这个博主的,说的很详细(侵权请联系删除):

从客户端中检测到有潜在危险的 Request.Form 值"的解决方案汇总_从客户端(extendparam="<extendparam><logint...")中检测到有潜在-CSDN博客

我采用的是第3种方法,但是也是他不推荐的方法(我暂时就先用了,不推荐也用了hhh)。

即在出错的那个文件中,添加这串代码:

ValidateRequest="false"

2、为什么执行插入代码之后,bookname种插入的是id而不是名字呢 

        protected void Page_Load(object sender, EventArgs e)
        {
            UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
            if (!IsPostBack)
            {
                myconnection.ConnectionString = sqlcon;

                // 编写 SQL 查询语句(示例)
                string query = "SELECT id, bookname FROM books";

                // 打开数据库连接并执行查询
                myconnection.Open();
                SqlCommand command = new SqlCommand(query, myconnection);
                SqlDataReader reader = command.ExecuteReader();

                // 将查询结果绑定到 DropDownList
                DropDownList1.DataSource = reader;
                DropDownList1.DataTextField = "bookname"; // 显示的文本字段
                DropDownList1.DataValueField = "id"; // 对应的值字段
                DropDownList1.DataBind();

                // 关闭连接
                reader.Close();
                
            }
        }

        protected void ImageButtonSave_Click(object sender, ImageClickEventArgs e)
        {
            myconnection.ConnectionString = sqlcon;
            myconnection.Open();
            string bookname = DropDownList1.Text;
            string bookid = TextBox3.Text;
            string chaptername = TextBox2.Text;
            int chapterid =Convert.ToInt32(TextBox1.Text);
            string chapterContent = TextBox4.Text;


            // 检查数据库中是否已存在相同的书籍名称和作者
            string checkDuplicateQuery = "SELECT COUNT(*) FROM bookChapter WHERE bookname = @bookname AND chapterid = @chapterid";
            SqlCommand checkDuplicateCommand = new SqlCommand(checkDuplicateQuery, myconnection);
            checkDuplicateCommand.Parameters.AddWithValue("@bookname", bookname);
            checkDuplicateCommand.Parameters.AddWithValue("@chapterid", chapterid);
            int existingBooksCount = (int)checkDuplicateCommand.ExecuteScalar();

            if (existingBooksCount > 0)
            {
                // 如果已存在相同的书籍,则提示用户并返回
                Response.Write("<script>alert('该章节已存在');</script>");
                myconnection.Close();
                return;
            }

            // 如果不存在相同的书籍,则执行插入操作
            string sqlcmd = "INSERT INTO bookChapter (bookname, chaptername, bookid, chapterid, chapterContent) VALUES (@bookname,@chaptername,@bookid, @chapterid, @chapterContent)";
            SqlCommand mycommand = new SqlCommand(sqlcmd, myconnection);
            mycommand.Parameters.AddWithValue("@bookname", bookname);
            mycommand.Parameters.AddWithValue("@chaptername", chaptername);
            mycommand.Parameters.AddWithValue("@bookid", bookid);
            mycommand.Parameters.AddWithValue("@chapterid", chapterid);
            mycommand.Parameters.AddWithValue("@chapterContent", chapterContent);
            
            mycommand.ExecuteNonQuery();
            Response.Write("<script>alert('添加成功');</script>");
            myconnection.Close();
        }

    }

在这个代码中,当绑定 DropDownList1 控件时,你使用了 DataSourceDataTextField/DataValueField 来指定数据源和显示值/实际值字段。在这里,DataSource 是一个 SqlDataReader 对象,而该对象的读取位置可能已经指向了结果集的末尾,或者没有正确地设置读取位置。这可能导致在绑定到 DropDownList1 控件时显示的是错误的字段。

为了解决这个问题,可以将查询结果存储在一个 DataTable 中,然后关闭 SqlDataReader,再将 DataTable 分配给 DropDownList1DataSource 属性。这样可以确保在绑定时能够正确地读取数据。

修改之后的代码:

        protected void Page_Load(object sender, EventArgs e)
        {
            UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
            if (!IsPostBack)
            {
                myconnection.ConnectionString = sqlcon;

                // 编写 SQL 查询语句(示例)
                string query = "SELECT id, bookname FROM books";

                // 打开数据库连接并执行查询
                myconnection.Open();
                SqlCommand command = new SqlCommand(query, myconnection);
                SqlDataReader reader = command.ExecuteReader();

                // 将查询结果存储在 DataTable 中
                DataTable dt = new DataTable();
                dt.Load(reader);
                reader.Close();  // 关闭 SqlDataReader

                // 将 DataTable 绑定到 DropDownList
                DropDownList1.DataSource = dt;
                DropDownList1.DataTextField = "bookname"; // 显示的文本字段
                DropDownList1.DataValueField = "id"; // 对应的值字段
                DropDownList1.DataBind();
            }
        }

        protected void ImageButtonSave_Click(object sender, ImageClickEventArgs e)
        {
            myconnection.ConnectionString = sqlcon;
            myconnection.Open();
            string bookname = DropDownList1.SelectedItem.Text; // 获取选中的书籍名称
            string bookid = TextBox3.Text;
            string chaptername = TextBox2.Text;
            int chapterid = Convert.ToInt32(TextBox1.Text);
            string chapterContent = TextBox4.Text;

            // 检查数据库中是否已存在相同的书籍名称和作者
            string checkDuplicateQuery = "SELECT COUNT(*) FROM bookChapter WHERE bookname = @bookname AND chapterid = @chapterid";
            SqlCommand checkDuplicateCommand = new SqlCommand(checkDuplicateQuery, myconnection);
            checkDuplicateCommand.Parameters.AddWithValue("@bookname", bookname);
            checkDuplicateCommand.Parameters.AddWithValue("@chapterid", chapterid);
            int existingBooksCount = (int)checkDuplicateCommand.ExecuteScalar();

            if (existingBooksCount > 0)
            {
                // 如果已存在相同的书籍,则提示用户并返回
                Response.Write("<script>alert('该章节已存在');</script>");
                myconnection.Close();
                return;
            }

            // 如果不存在相同的书籍,则执行插入操作
            string sqlcmd = "INSERT INTO bookChapter (bookname, chaptername, bookid, chapterid, chapterContent) VALUES (@bookname,@chaptername,@bookid, @chapterid, @chapterContent)";
            SqlCommand mycommand = new SqlCommand(sqlcmd, myconnection);
            mycommand.Parameters.AddWithValue("@bookname", bookname);
            mycommand.Parameters.AddWithValue("@chaptername", chaptername);
            mycommand.Parameters.AddWithValue("@bookid", bookid);
            mycommand.Parameters.AddWithValue("@chapterid", chapterid);
            mycommand.Parameters.AddWithValue("@chapterContent", chapterContent);

            mycommand.ExecuteNonQuery();
            Response.Write("<script>alert('添加成功');</script>");
            myconnection.Close();
        }
    }

 基本上实现了一些功能,但是后面还需要持续的修改才能发现更多的不足之处。

基本的功能就潦潦草草的实现了,现在就是完善系统,补充文档内容,引用参考文献的问题了。

待解决问题:

1、为什么引用了参考文献关闭电脑后再次打开就无法跳转了呢。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值