有人可以告诉我这段代码中的错误在哪里:
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();
}
}
}
此代码在表格中进行更新&#34; Enfants&#34;然后它显示iu gridview,当我调试我有这个错误=&#34; &#39; =&#39;附近的语法不正确&#34; 谢谢
答案 0 :(得分:0)
我的声誉不足以添加评论,但我不得不问:“myid”肯定是整数/双?如果没有,你也必须用单引号括起来。
答案 1 :(得分:0)
您将id声明为字符串,然后您忘记引用它where 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>");
}
}
}