简体   繁体   English

从数据库中删除记录在 GridView 中不起作用

[英]Deleting record from database is not working in GridView

I am trying to delete a record from my database by selecting the tick box and then click on delete.我正在尝试通过选择复选框然后单击删除来从我的数据库中删除一条记录。 What seems to happen now is that the page refreshes but the entry is not being deleted.现在似乎发生的是页面刷新但条目没有被删除。 There are no error messages what so ever从来没有错误消息

I have followed this tutorial Delete Records From Gridview Using CheckBox in ASP.Net我已经按照本教程在 ASP.Net 中使用 CheckBox 从 Gridview 中删除记录

This is what my code looks like:这就是我的代码的样子:

AddIPAddress.aspx添加IP地址.aspx

<script type="text/javascript">  

    function DeleteConfirm() {
        var Ans = confirm("Do you want to Delete Selected Employee Record?");
        if (Ans) {
            return true;
        }
        else {
            return false;
        }
    }
</script>  

 <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
            RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
            runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkDel" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="EmpId" HeaderText="Device Id" />
                <asp:BoundField DataField="Device" HeaderText="Device Name" />
                <asp:BoundField DataField="IP" HeaderText="IP Address" />
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
    </div> 
        <br />
        <asp:Button ID="btnDeleteRecord" runat="server" onclick="btnDeleteRecord_Click" Text="Delete" CssClass="btn-success" />

Then in:然后在:

AddIPAddress.cs.aspx AddIPAddress.cs.aspx

string cs = ConfigurationManager.ConnectionStrings["IPAddress"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
    {
        this.BindGrid();
        btnDeleteRecord.Attributes.Add("onclick", "javascript:return DeleteConfirm()");
    }

private void BindGrid()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(cs);
        SqlDataAdapter adapt = new SqlDataAdapter("SELECT * FROM IPAddress", con);
        con.Open();
        adapt.Fill(dt);
        con.Close();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.BindGrid();
    }

    protected void DeleteRecord(int empid)
    {
        SqlConnection con = new SqlConnection(cs);
        SqlCommand com = new SqlCommand("DELETE FROM IPAddress WHERE EmpId=@ID", con);
        com.Parameters.AddWithValue("@ID", empid);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
    }

    protected void btnDeleteRecord_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow grow in GridView1.Rows)
        {
            //Searching CheckBox("chkDel") in an individual row of Grid  
            CheckBox chkdel = (CheckBox)grow.FindControl("chkDel");
            //If CheckBox is checked than delete the record with particular empid  
            if (chkdel.Checked)
            {
                int empid = Convert.ToInt32(grow.Cells[1].Text);
                DeleteRecord(empid);
            }
        }
        //Displaying the Data in GridView  
        BindGrid();
    }

Have I missed something?我错过了什么吗?

Thanks谢谢

You need to do following this steps:-您需要执行以下步骤:-

I am explain you all the Process Deleting record from database is not working in GridView我向您解释数据库中的所有进程删除记录在 GridView 中不起作用

 protected void btnDeleteRecord_Click(object sender, EventArgs e)  
   {    
    foreach (GridViewRow gvrow in GridView1.Rows)  
    {    
         var Label = gvrow.FindControl("Label1") as Label;  

           SqlCommand cmd = new SqlCommand("delete from tblname where id=@id",con);  
           cmd.Parameters.AddWithValue("id", int.Parse(Label.Text));  
            con.Open();  
            int id = cmd.ExecuteNonQuery();  
            con.Close();  
            refreshdata();  
        }  
    }

    public void refreshdata()  
    {   
      SqlCommand cmd = new SqlCommand("select * from tbl_data", con);  
      SqlDataAdapter sda = new SqlDataAdapter(cmd);  
      DataTable dt = new DataTable();  
      sda.Fill(dt);  
      GridView1.DataSource = dt;  
      GridView1.DataBind();
    }

Okay, I definitely missed something here:-)好吧,我肯定在这里错过了一些东西:-)

This code here should be changed from:此处的代码应更改为:

protected void Page_Load(object sender, EventArgs e)
{
    this.BindGrid();
    btnDeleteRecord.Attributes.Add("onclick", "javascript:return DeleteConfirm()");
}

To

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Displaying the Data  
            BindGrid();
            //Adding an Attribute to Server Control(i.e. btnDeleteRecord)  
            btnDeleteRecord.Attributes.Add("onclick", "javascript:return DeleteConfirm()");
        }
    }

All is working now.现在一切正常。 Thanks to everyone who assisted:-)感谢所有提供帮助的人:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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