为什么记录不是从网格中删除

时间:2014-05-02 09:51:39

标签: asp.net sql-server gridview stored-procedures

我在gridview中使用了复选框。

我试图在按钮点击时从gridview删除已检查的记录。但它不是删除记录。

我正在努力,但没有得到我做错的事。

这是我的代码 -

网格代码 -

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Name" 
        DataSourceID="SqlDataSource1" 
            style="padding: 0px; margin: 0px; border: thin solid #FF9933; width: 500px;" 
            CellPadding="0" EmptyDataText="No records found" ForeColor="#333333" 
            GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <HeaderStyle BackColor="Moccasin" Width="500px" Font-Bold="True" 
            ForeColor="Black" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="cbRows" runat="server"  />    
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chkdelete" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        </Columns>   
    </asp:GridView>
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:HRMSConnectionString %>" 
        SelectCommand="SELECT [Name] FROM [Languages]">
    </asp:SqlDataSource>

我的default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    tblAdd.Visible = false;
    Label1.Visible = false;
    GridView1.DataBind();
    if (!Page.IsPostBack)
        {
            fillLanguageGrid();
        }
}

public void fillLanguageGrid()
    {
        GridView1.DataSourceID = "SqlDataSource1";
        GridView1.DataBind();
        GridView1.Visible = true;
    }



protected void btnDelete_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvrow in GridView1.Rows)
    {
        CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkdelete");
        if (chkdelete.Checked)
        {
            string name= Convert.ToString(GridView1.DataKeys[gvrow.RowIndex].Values["Name"].ToString());
            deleteRecordByName(name);
        }
    }
    fillLanguageGrid();
}

public void deleteRecordByName(string Name)
{
    string strcon = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;
    SqlConnection sqlConnection = new SqlConnection(strcon);
    SqlDataAdapter da = new SqlDataAdapter("hrm_DeleteLanguages2", sqlConnection);
    da.SelectCommand.CommandType = CommandType.StoredProcedure;
    da.SelectCommand.Parameters.Add(new SqlParameter("@Name",SqlDbType.VarChar));
    da.SelectCommand.Parameters["@Name"].Value= Name;
    sqlConnection.Open();
    da.SelectCommand.ExecuteNonQuery();
    sqlConnection.Dispose();
}

存储过程代码 -

    CREATE PROCEDURE hrm_DeleteLanguages2
       @name varchar(120)        
AS 
BEGIN 


     DELETE 
     FROM   [HRMS].[dbo].[Languages]
     WHERE  
     Name = @name

END

这是网格的快照 -

enter image description here

3 个答案:

答案 0 :(得分:2)

为什么你在创建一个SqlDataAdapter并指定它SelectCommand来删除某些东西?!?!?完全没有意义....

尝试更轻松的事情:

public void deleteRecordByName(string Name)
{
    string strcon = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;

    // create a SqlConnection and a SqlCommand to delete the data in a using().... block
    using (SqlConnection sqlConnection = new SqlConnection(strcon))
    using (SqlCommand cmd = new SqlCommand("dbo.hrm_DeleteLanguages2", sqlConnection)) 
    {
         // define this to be a stored procedure
         cmd.CommandType = CommandType.StoredProcedure;

         // define the parameter and set its value
         cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar)).Value= Name;

         // open connection, execute DELETE query, close connection
         sqlConnection.Open();
         cmd.ExecuteNonQuery();
         sqlConnection.Dispose();
    }
}

如果您坚持使用SqlDataAdapter,那么 至少要使用相应的 DeleteCommand 来删除行 - 不要滥用SelectCommand来删除数据!

答案 1 :(得分:1)

不要在页面加载事件上绑定网格视图。

选择“复选框”并单击按钮时。首先执行页面加载事件。在页面上,所有复选框的加载选择都将被删除,然后执行您的点击事件代码。

从页面加载中删除网格视图绑定。将此代码保留在click事件中。

答案 2 :(得分:0)

protected void btnDelete_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvrow in GridView1.Rows)
    {
        CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkdelete");
        if (chkdelete.Checked)
        {
            string name= Convert.ToString(GridView1.DataKeys[gvrow.RowIndex]["Name"].ToString());
            deleteRecordByName(name);
        }
    }
    fillLanguageGrid();
}