0

I have the following VB code in the ASP.net code page, when I click on the delete link button it deletes every row in the GridView. Is it possible to delete the selected row only?

    Protected Sub grvPos_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles grvPos.RowDeleting
    Dim cs As String
    Dim con As iDB2Connection
    Dim sql As String
    Dim cmd As iDB2Command
    Dim valDate As Integer
    Dim portCode As String
    Dim secCode As String

    For Each gr As GridViewRow In grvPos.Rows

        valDate = grvPos.DataKeys(gr.RowIndex).Values("VALN_DATE")
        portCode = grvPos.DataKeys(gr.RowIndex).Values("PORT_CODE").ToString()
        secCode = grvPos.DataKeys(gr.RowIndex).Values("SEC_CODE").ToString()

        cs = ConfigurationManager.ConnectionStrings("ConnectionStringDB2").ConnectionString
        con = New iDB2Connection(cs)
        sql = "DELETE FROM OPTR_POS_FIX WHERE (VALN_DATE = @valDate) AND (PORT_CODE = @portCode) AND (SEC_CODE = @secCode)"
        cmd = New iDB2Command(sql, con)
        cmd.Parameters.AddWithValue("@valDate", valDate)
        cmd.Parameters.AddWithValue("@portCode", portCode)
        cmd.Parameters.AddWithValue("@secCode", secCode)

        Try
            Using con
                con.Open()
                cmd.ExecuteNonQuery()

            End Using

        Catch ex As Exception
            Throw

        End Try
    Next
End Sub

I appreciate any help Thanks in advance

2 Answers 2

1

The For Each operator in this code iterates by all rows of the grid. Instead of that you have to get only selected row.

So just remove For Each - Next loop, and add the line

GridViewRow gr = grvPos.Rows[e.RowIndex];

UPD. You're right, in vb.net syntax it will be:

Dim gr As GridViewRow
gr = grvPos.Rows(e.RowIndex)
Sign up to request clarification or add additional context in comments.

2 Comments

It says GridViewRow is a class type and connot be used as an expression.
@MattJames, sorry, I wrote the line in c# syntax by force of habit. Your correction is well. If it solved your problem, please, mark the answer as correct.
0

Thanks I have changed the code in order to function.

    Dim gr As GridViewRow

    gr = grvPos.Rows(e.RowIndex)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.