简体   繁体   中英

Delete multiple asp.net repeater Items with checkbox selection only

here it does not show any error only page loading what is the error?

C# code

protected void imgs_Click(object sender, ImageClickEventArgs e)  
        {        
            foreach (RepeaterItem ri in repeater.Items)
            {

            CheckBox item_check = (CheckBox)ri.FindControl("item_check");
            Label txt_id = (Label)ri.FindControl("txt_id");

                if (item_check.Checked)
                {
                    con = new SqlConnection(strcon);
                    SqlCommand cmd = new SqlCommand("ram", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();
                    cmd.Parameters.AddWithValue("@Action", "DELETE");
                    cmd.Parameters.AddWithValue("@eid", txt_id);
                    repeat();
               }                
        }     
}

asp.code

You Forgot To Write cmd.ExecuteNonQuery();

  1. Stop Using AddWithValue
  2. Always use using for SqlConnection and SqlCommand which implements IDisposable

    protected void imgs_Click(object sender, ImageClickEventArgs e)
    {

      foreach (RepeaterItem ri in repeater.Items) { CheckBox item_check = (CheckBox)ri.FindControl("item_check"); Label txt_id = (Label)ri.FindControl("txt_id"); if (item_check.Checked) { Using(SqlConnection con = new SqlConnection(strcon)) { Using(SqlCommand cmd = new SqlCommand("ram", con)) { cmd.CommandType = CommandType.StoredProcedure; con.Open(); cmd.Parameters.Add("@Action",SqlDbType.Varchar,50).Value="DELETE"; cmd.Parameters.Add("@eid",SqlDbType.Int).Value=Convert.ToInt16(txt_id.Text); cmd.ExecuteNonQuery(); repeat(); } } } } 

    }

    foreach (RepeaterItem ri in repeater.Items)
    {

        CheckBox item_check = (CheckBox)ri.FindControl("item_check");
        Label txt_id = (Label)ri.FindControl("txt_id");

        if (item_check.Checked)
        {

            con = new SqlConnection(strcon);
            cmd = new SqlCommand("ram", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Action", "DELETE");
            cmd.Parameters.AddWithValue("@eid", txt_id.Text);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
            finally
            {

                con.Close();
            }
        }
    }
    repeat();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM