Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从数据库中删除记录在GridView中不起作用_C#_Asp.net_Mayurpathak - Fatal编程技术网

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

C# 从数据库中删除记录在GridView中不起作用,c#,asp.net,mayurpathak,C#,Asp.net,Mayurpathak,我试图通过选择勾选框,然后单击删除,从数据库中删除记录。 现在出现的情况似乎是页面刷新,但条目没有被删除。没有任何错误消息 我遵循了这个教程 这就是我的代码的样子: AddIPAddress.aspx <script type="text/javascript"> function DeleteConfirm() { var Ans = confirm("Do you want to Delete Selected Empl

我试图通过选择勾选框,然后单击删除,从数据库中删除记录。 现在出现的情况似乎是页面刷新,但条目没有被删除。没有任何错误消息

我遵循了这个教程

这就是我的代码的样子:

AddIPAddress.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" />
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();
    }
我错过什么了吗


谢谢

您需要执行以下步骤:-

我将向您解释从数据库中删除记录的所有过程在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();
    }

好吧,我肯定错过了一些东西:-)

此处的代码应更改为:

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


现在一切都好了。感谢所有帮助的人:-)

如果您在删除代码上设置断点,它会被调用吗?我只得到以下
>Monitor.dll!Monitor.Settings.AddIPAddress.btndeletecrecord\单击(对象发送器,System.EventArgs e)第80行\
此代码帮助您@艾伦
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()");
        }
    }