[英]update and display in gridview (ASP c#)
can someone tell me where is the error in this code : 有人可以告诉我此代码中的错误在哪里:
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();
}
}
}
this code make an update in table "Enfants" then it display iu gridview , when i debug i have this error = " Incorrect syntax near '=' " thank you 此代码在“ Enfants”表中进行更新,然后显示iu gridview,当我调试时出现此错误=“'='附近的语法不正确” 谢谢
My reputation isn't high enough to add a comment, but I have to ask: is "myid" definitely an integer/double? 我的声誉不够高,无法添加评论,但是我不得不问:“ myid”是否肯定是整数/双精度? If not, you'll have to enclose it in single quotes as well.
如果没有,您也必须将其用单引号引起来。
You decalre myid as a string then you forgot to quote it where codeEnfants =" + myid;
您将myid贴上字符串,然后忘了
where codeEnfants =" + myid;
引用它where codeEnfants =" + myid;
You should not add the parameters this way anyway, try like this: 无论如何,您都不应以这种方式添加参数,请尝试如下操作:
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>");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.