Dave Mackey
Dave Mackey

Reputation: 4432

No results returning from GridView.SelectedRow.Cells(x).Text?

I have a GridView which is showing results and from which I am deleting a result using the auto-created delete link. My code behind to remove the row and associated info. is:

  Private Sub GridView1_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
    ' The deletion of the individual row is automatically handled by the GridView.
    Dim dbDelete As New pbu_housingEntities
    ' Remove individual from the bed.
    Dim remove_bed = From p In dbDelete.Beds _
                     Where p.occupant = GridView1.SelectedRow.Cells(3).Text _
                     Where p.room = GridView1.SelectedRow.Cells(6).Text _
                     Where p.building = GridView1.SelectedRow.Cells(5).Text _
                     Order By p.id Descending _
                     Select p

    remove_bed.First.occupant = ""
    dbDelete.SaveChanges()

    ' Increase number of open spaces in room.
    Dim update_occupancy = From p In dbDelete.Rooms _
                           Where p.room1 = GridView1.SelectedRow.Cells(6).Text
                           Where p.building = GridView1.SelectedRow.Cells(5).Text _
                           Select p

    update_occupancy.First.current_occupancy = update_occupancy.First.current_occupancy - 1
    dbDelete.SaveChanges()


End Sub

It seems that it is not able to grab the row that is being deleted, so it is always giving me a "Object reference not set to an instance of an object" error.

Upvotes: 0

Views: 1627

Answers (1)

NoAlias
NoAlias

Reputation: 9193

Use GridView1.Rows(e.RowIndex).Cells(.......

Upvotes: 1

Related Questions