protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["username"] == "您还没有登录")
        {
            this.Page.RegisterStartupScript("ss", "<script>alert('您还没有登录,请您登录后再回帖')</script>");
        }
        else
        {
            if (TextBox2.Text == "" || TextBox4.Text == "")
            {
                this.Page.RegisterStartupScript("ss", "<script>alert('请输入回复评论的标题和内容')</script>");
                return;
            }
            else
            {
                //判断输入的验证码是否正确
                string num = this.TextBox3.Text.Trim();
                if (Session["ValidNums"].ToString() == num.ToUpper())
                {
                    // 连接数据库
                    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
                    sqlcon.Open();
                    //获得回复数量
                    string strselect1 = "select * from tb_forums where id='" + Request["id"]+ "'";
                    SqlCommand sqlcmd1 = new SqlCommand(strselect1, sqlcon);
                    SqlDataReader dr1 = sqlcmd1.ExecuteReader();
                    int hfnums = 0;
                    if (dr1.Read())    ///////这里出现错误//////////
                    {
                        hfnums = Convert.ToInt32(dr1["hf_nums"].ToString()) + 1;
                    }
                    dr1.Close();
                    //更改主表中的回复数量
                    string Strupdate = "update tb_forums set hf_nums=" + hfnums + " where id='" + Request["id"].ToString() + "'";
                    SqlCommand cmd = new SqlCommand(Strupdate, sqlcon);
                    cmd.ExecuteNonQuery();
                    //向子表中插入数据
                    string StrInserts = "insert into tb_Sub_forums(forumid,styleid,title,contents,dates,authorid,first_forum) values(@forumid,@styleid,@title,@contents,@dates,@authorid,0)";
                    SqlCommand cmds = new SqlCommand(StrInserts, sqlcon);
                    // 添加参数并且设置参数值
                    cmds.Parameters.Add("@forumid", SqlDbType.VarChar);
                    cmds.Parameters["@forumid"].Value = Request["id"].ToString();
                    cmds.Parameters.Add("@styleid", SqlDbType.VarChar);
                    cmds.Parameters["@styleid"].Value = GetStyleId(Session["forumstyle"].ToString()).ToString();
                    cmds.Parameters.Add("@title", SqlDbType.VarChar);
                    cmds.Parameters["@title"].Value = this.TextBox4.Text.ToString();
                    cmds.Parameters.Add("@contents", SqlDbType.VarChar);
                    cmds.Parameters["@contents"].Value = this.TextBox2.Text.ToString();
                    cmds.Parameters.Add("@dates", SqlDbType.VarChar, 50);
                    cmds.Parameters["@dates"].Value = DateTime.Now.ToString();
                    cmds.Parameters.Add("@authorid", SqlDbType.VarChar);
                    cmds.Parameters["@authorid"].Value = Session["userid"].ToString();
                    // 执行插入数据的操作
                    cmds.ExecuteNonQuery();
                    sqlcon.Close();
                    this.Page.RegisterStartupScript("ss", "<script>alert('恭喜您,成功回帖!')</script>");
                }
                else
                {
                    this.Page.RegisterStartupScript("ss", "<script>alert('您输入的验证码不正确!')</script>");
                }
            }
        }
    }

解决方案 »

  1.   

    string Strupdate = "update tb_forums set hf_nums=" + hfnums + " where id='" + Request["id"].ToString() + "'";你的id字段类型不是数字类型的吗?如果是是不能加引号的
    string Strupdate = "update tb_forums set hf_nums=" + hfnums + " where id=" + Request["id"].ToString() ;其他地方也是类似
      

  2.   

    cmds.Parameters.Add("@forumid", SqlDbType.VarChar);
    forumid是VarChar类型?
    如果你搞不懂类型,你及直接写
    cmds.Parameters.AddWithValue("@forumid", Request["id"].ToString());
    不要指定类型
      

  3.   

    数据库中的字段类型是整型,你传的参数却是字符型,cmds.Parameters.Add("@forumid", SqlDbType.Int32);检查数据库字段的类型,把相应的参数类型修改一下