locked
Delete Command of Gridview wont work ! RRS feed

  • Question

  • User1700504264 posted

    Hello everybody

    I added a row command to a Gridview in order to perform a deletion of the row

    this is the markup

    <asp:GridView ID="SessionsGridView" runat="server" CssClass="table table-bordered table-hover table-striped table-responsive " DataSourceID="sessionsSqlDataSource" Width="100%"
                                DataKeyNames="ID" AutoGenerateDeleteButton="true" AllowPaging="true" PageSize="10" EmptyDataText="No sessions found!" OnPageIndexChanging="sessionsGridView_PageIndexChanging">
                                <Columns>
                                    <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ReadOnly="true" />
                                    <asp:BoundField DataField="dayName" HeaderText="<%$ Resources:day %>" SortExpression="dayName" ReadOnly="true" />
                                    <asp:BoundField DataField="sessionDate" HeaderText="<%$ Resources:date %>" SortExpression="sessionDate" DataFormatString="{0:MM/dd/yyyy}" ReadOnly="true" />
                                    <asp:BoundField DataField="sessionStart" HeaderText="<%$ Resources:start %>" SortExpression="sessionStart" ReadOnly="true" visible="false"/>
                                    <asp:BoundField DataField="sessionStartFormated" HeaderText="<%$ Resources:start %>" SortExpression="sessionStart" ReadOnly="true" />
                                    <asp:BoundField DataField="sessionEnd" HeaderText="<%$ Resources:end %>" SortExpression="sessionEnd" ReadOnly="true" visible="false" />
                                    <asp:BoundField DataField="sessionEndFormated" HeaderText="<%$ Resources:end %>" SortExpression="sessionEnd" ReadOnly="true" />
                                    <asp:BoundField DataField="Instructor" HeaderText="<%$ Resources:instructor %>" SortExpression="Instructor" ReadOnly="true" Visible="false" />
                                    <asp:BoundField DataField="Classroom" HeaderText="<%$ Resources:classroom %>" SortExpression="Classroom" ReadOnly="true" Visible="false" />
                                    <asp:BoundField DataField="iAName" HeaderText="<%$ Resources:instructor %>" SortExpression="iAName" ReadOnly="true" />
                                    <asp:BoundField DataField="cAName" HeaderText="<%$ Resources:classroom %>" SortExpression="cAName" ReadOnly="true" />
                                    <asp:TemplateField HeaderText="">
                                    <ItemTemplate>
                                        <asp:LinkButton runat="server" ID="cmdExclude" CausesValidation="false" CssClass="btn btn-danger btn-xs" Text="Exclude" CommandName="Exclude" CommandArgument='<%# Bind("ID") %>' ><i class="fa fa-trash"></i></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                </Columns>
                        </asp:GridView>

    this is the markup of the SqlDataSource 

    <asp:SqlDataSource ID="SessionsSqlDataSource" runat="server"
                            ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
                            DeleteCommand="DELETE FROM Session Where ID=@ID"
                            
                                <DeleteParameters>
                                    <asp:Parameter Name="ID" Type="Int64" />
                                </DeleteParameters>
                        </asp:SqlDataSource>

    and this is the code behind to perform the delete

    Private Sub SessionsGridView_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles SessionsGridView.RowCommand
            Try
                ltrlClass.Text = ""
                If e.CommandName = "Exclude" Then
                    SessionSqlDataSource.DeleteParameters.Add("ID", e.CommandArgument)
                    Dim resutl As Integer = SessionsSqlDataSource.Delete()
                    If resutl > 0 Then
                        SessionsGridView.DataBind()
                        ltrlClass.Text = "<div class='alert alert-success alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='True'>&times;</span></button><strong>Success!</strong> session was excluded.</div>"
                    End If
    
                End If
            Catch ex As Exception
                ltrlClass.Text = "<div class='alert alert-warning alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='True'>&times;</span></button><strong>Sorry!</strong> Something went wrong.</div>"
            End Try
        End Sub

    it runs without any exceptions

    but it do not delete any row , the value of (result) is 0 all the time 

    may you help me fixing this please?

    thank you

    Saturday, April 7, 2018 7:54 AM

Answers

  • User-1716253493 posted
    SessionSqlDataSource.DeleteParameters("ID").DefaultValue = e.CommandArgument
    SessionSqlDataSource.Delete()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 7, 2018 10:53 AM