Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

update a row in gridview when checkbox is checking (using button)

$
0
0
all is clear in the title. i have a code but i don't know why it's not working :
 

protectedvoid btnUpdate_Click(object sender, EventArgs e)
        {
            #region  @
            try
            {
                string myid = string.Empty;
                for (int i = 0; i < gv_enfant.Rows.Count; i++)
                {
                    var chbox = gv_enfant.Rows[i].Cells[0].FindControl("CheckBoxenfant") as CheckBox;
                    var codeenfant = gv_enfant.Rows[i].Cells[0].FindControl("codeenfant") as HiddenField;
                    if (chbox != null&& codeenfant != null)
                    {
                        if (chbox.Checked)
                        {
                            myid = codeenfant.Value;
                        }
                    }
                }
                Response.Write("myid = " + myid);
                c.cmd = c.cn.CreateCommand();
                c.cmd.CommandText = "update Enfants set prenom =@prenom,  DateNaissance=@dateNaissance, Scolarise=@scolarise, Activite=@activite where codeEnfants=@codeEnfants";
                if (c.cn.State == ConnectionState.Closed)
                {
                    c.cn.Open();
                }
                c.cmd.Parameters.Add("@prenom", SqlDbType.VarChar);
                c.cmd.Parameters.Add("@dateNaissance", SqlDbType.Date);
                c.cmd.Parameters.Add("@scolarise", SqlDbType.VarChar);
                c.cmd.Parameters.Add("@activite", SqlDbType.VarChar);
                c.cmd.Parameters.Add("@codeEnfants", SqlDbType.Int);
 
                c.cmd.Parameters["@prenom"].Value = TextBox_NPmodif.Text;
                c.cmd.Parameters["@dateNaissance"].Value = TextBox_DNmodif.Text;
                c.cmd.Parameters["@scolarise"].Value = TextBox_Scolarisemodif.Text;
                c.cmd.Parameters["@activite"].Value = TextBox_Activitemodif.Text;
                c.cmd.Parameters["@codeEnfants"].Value = myid;
                c.cmd.ExecuteNonQuery();
                gv_enfant.DataBind();
 
                //success!
                Response.Write("<script>alert('Opération reussie')</script>");
               
            }
            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>");
            }
            finally
            {
                if (c.cn.State == ConnectionState.Open)
                {
                    c.cn.Close();
                }
            }
            #endregion
}
 
thank you

Viewing all articles
Browse latest Browse all 3938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>