nikhil
nikhil

Reputation: 211

Delete a row from gridview

I am trying to delete a row from gridview on rowdataBound() event but get Procedure or function delete_row has too many arguments specified. Below is the code

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if ((e.Row != null) && (e.Row.RowType == DataControlRowType.DataRow))
    {
        for (int i = 0; i < GridView1.Rows.Count; i++ )
        {
            GridView1.Rows[i].Attributes["style"] += "cursor: pointer; cursor: hand;";
            if (GridView1.DataKeys[i].Values[1].ToString() != "broken")
                GridView1.Rows[i].Attributes["onclick"] =
                    "window.open('" + GridView1.DataKeys[i].Values[0].ToString() + "','open_window', 'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0')";
            else
            {
                GridView1.DeleteRow(i);
            }

        }

    }

HTML mark up is below, I have 3 DataKeyNames declared is that the problem

<asp:HiddenField ID="hiddenField1" runat="server" Value="" />

        <br />

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:User42ConnectionString %>" 
            SelectCommand="lsp_show_by_letter" onselecting="SqlDataSource1_Selecting" 
            SelectCommandType="StoredProcedure" DeleteCommand="delete_row" 
            DeleteCommandType="StoredProcedure">
            <SelectParameters>
                <asp:ControlParameter ControlID="hiddenField1" DefaultValue=" " 
                    Name="letter" PropertyName="Value" Type="String" />
            </SelectParameters>
            <DeleteParameters>
                <asp:ControlParameter Name="link_Id" ControlID="hiddenField1" PropertyName="Value" Type="Int32" />
            </DeleteParameters>
        </asp:SqlDataSource>

    </div>

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound" 
        DataKeyNames="link_url,link_description,link_id">
        <Columns>
            <asp:BoundField DataField="link_display_string" 
                HeaderText="link_display_string" SortExpression="link_display_string" />
            <asp:BoundField DataField="link_url"Visible="False" />
           <asp:BoundField DataField="link_description" Visible="False" />
           <asp:BoundField DataField="link_id" ReadOnly="true" Visible="False" />
        </Columns>
    </asp:GridView>

Delete row stored procedure is

  `ALTER PROCEDURE dbo.delete_row @link_Id int AS BEGIN DELETE FROM [links] WHERE ([link_id] = @link_Id) END` 

Upvotes: 0

Views: 961

Answers (1)

Priyank
Priyank

Reputation: 10623

It's because your hiddenfiled is not bound to SqlDataSource and so it passes empty or null value to delete_row procedure. Since you are bounding your SQLDataSource directly to datagridview, you need to bound hideenfiled with selected row value of datagridview. This should help you trying few options but this is your base problem.

Upvotes: 2

Related Questions