在gridview中更新和显示(ASP c#)

在gridview中更新和显示(ASP c#),c#,sql,C#,Sql,有人能告诉我代码中的错误在哪里吗: protected void btnValiderModifier_Click(object sender, EventArgs e) { try { string myid; for (int i = 0; i < gv_enfant.Rows.Count; i++) {

有人能告诉我代码中的错误在哪里吗:

 protected void btnValiderModifier_Click(object sender, EventArgs e)
        {
            try
            {
                string myid;
                for (int i = 0; i < gv_enfant.Rows.Count; i++)
                {
                    CheckBox chbox = (CheckBox)gv_enfant.Rows[i].Cells[0].FindControl("CheckBoxenfant");
                    if (chbox.Checked)
                    {
                        myid = ((HiddenField)gv_enfant.Rows[i].Cells[0].FindControl("codeenfant")).Value;

                }
            }

            c.cmd = c.cn.CreateCommand();
            c.cmd.CommandText = "update Enfants set prenom ='" + TextBox_NPmodif.Text + "',DateNaissance='" + TextBox_DNmodif.Text + "', Scolarise='" + TextBox_Scolarisemodif.Text + "',Activite= '" + TextBox_Activitemodif.Text +"' where codeEnfants =" + myid;
            if (c.cn.State == ConnectionState.Closed)
            {
                c.cn.Open();
            }
            c.cmd.ExecuteNonQuery();
            Response.Write("<script>alert('Opération reussie')</script>");
            gv_enfant.DataBind();

        }
        catch(Exception ex)
        {
            Response.Write("<script>alert ('Erreur lors de la modif!')</script>");
        }
        finally
        {
            if (c.cn.State == ConnectionState.Open)
            {
                c.cn.Close();
            }
        }
    }
protectedvoid btnValideModifier\u单击(对象发送方,事件参数e)
{
尝试
{
字符串myid;
对于(int i=0;i
此代码在表“Enfants”中进行更新,然后显示iu gridview,当我调试时,出现以下错误=“靠近“=”的语法不正确”
谢谢你

我的声誉不足以添加评论,但我不得不问:“myid”绝对是整数/双精度吗?如果没有,您也必须用单引号将其括起来。

您将myid标记为字符串,然后忘记引用它
,其中codeEnfants=“+myid;”

无论如何,不应以这种方式添加参数,请尝试以下操作:

protected void btnValiderModifier_Click(object sender, EventArgs e)
{
    using(SqlConnection conn = new SqlConnection(connString))
    {
            string myid;
            for (int i = 0; i < gv_enfant.Rows.Count; i++)
            {
                CheckBox chbox = (CheckBox)gv_enfant.Rows[i].Cells[0].FindControl("CheckBoxenfant");
                if (chbox.Checked)
                {
                    myid = ((HiddenField)gv_enfant.Rows[i].Cells[0].FindControl("codeenfant")).Value;

                }
            }
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("update Enfants set prenom =@prenom,  DateNaissance=@dateNaissance, Scolarise=@scolarise, Activite=@activite where codeEnfants=@codeEnfants", conn);
            cmd.Parameters.AddWithValue("@prenom", TextBox_NPmodif.Text);
            cmd.Parameters.AddWithValue("@DateNaissance", TextBox_DNmodif.Text);
            cmd.Parameters.AddWithValue("@Scolarise", TextBox_Scolarisemodif.Text);
            cmd.Parameters.AddWithValue("@Activite", TextBox_Activitemodif.Text);
            cmd.Parameters.AddWithValue("@codeEnfants", myid);
            cmd.ExecuteNonQuery();

            //success!
            Response.Write("<script>alert('Opération reussie')</script>");
            gv_enfant.DataBind();
        }
        catch(SqlException sqlEx)
        {
            //fail!
            //what is the point of cacthing if you do use the exception?
            Response.Write("error" + sqlEx.Message);
            Response.Write("<script>alert ('Erreur lors de la modif!')</script>");
        }
    }
}
protectedvoid btnValideModifier\u单击(对象发送方,事件参数e)
{
使用(SqlConnection conn=newsqlconnection(connString))
{
字符串myid;
对于(int i=0;i
哪一行抛出错误?当您得到错误时,实际执行的SQL查询是什么?还要注意,您的代码对SQL注入是完全开放的。语法错误很可能是该漏洞造成的。您至少应该使用参数化查询。此外,您的错误处理显式忽略了异常往往包含关于他们遇到的错误的有意义的信息,你至少应该记录这些信息
@Meryem我试图在一个单独的函数中提取该方法,请参见我的编辑,并发布错误,不要只是说它不工作,我无法将此字符串置于未分配的位置。此行错误:
cmd.Parameters.AddWithValue(“@codeenfans”,myid)什么?对不起,我听不懂你写的
字符串myid;
它没有赋值,我想它应该像
字符串myid=“”;
但它不是working@Meryem哦,好的,我明白了,在for循环之后,确保您有code-enfant的Id,显示它
Response.Write(“myid=“+myid”);