Link to home
Start Free TrialLog in
Avatar of Juliafrazer
Juliafrazer

asked on

deleting rows from a gridview in asp.net, by obtaining the uniqueid.

I am trying to get the value of a bookingID from a asp.net gridview, so i can pass the value to a delete function, that will forward onto a sql stored procedure and delete the relevant row.

How can i obtain the bookingID as a selected row to pass it onto the function?
the grid view code is

<asp:GridView ID="SlotsBookedList" runat="server"  AutoGenerateColumns ="false"  AllowPaging ="true" DataKeyNames="BookingID">
          <columns>
         
               <asp:BoundField HeaderText = "student name" DataField = "username" />
               <asp:BoundField HeaderText = "Date" DataField = "BookingDate" />
               <asp:BoundField HeaderText = "Name of Machine" DataField = "MachineName" />
               <asp:BoundField HeaderText = "Slot Time Booked" DataField = "SlotTime" />
               <asp:BoundField HeaderText = "Slot Time Booked" DataField = "BookingID"  Visible="False"/>
               
                  <asp:CommandField HeaderText="Configure" DeleteImageUrl="/images/Delete.gif" ShowDeleteButton="True"
                        ButtonType="Image" />
           
            </columns>
           
        </asp:GridView>

.

The vb code to action thus far is:

Protected Sub SlotsBookedList_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles SlotsBookedList.RowDeleting

        Dim s As String

       'ctb.DeleteBookings("bookingID")

       
        Me.Label1.Text = s




    End Sub

I can return the rowIndex no problem using s = e.RowIndex.ToString()
But i want the actual bookingID that is bound to the gridview.

Any clues?

Julia
Avatar of surajguptha
surajguptha
Flag of United States of America image

Have you tried this?
grd.Rows[selInd].Cells["id"].Value
Avatar of Juliafrazer
Juliafrazer

ASKER

Ive got to this stage, and it works in terms of deleting the rows, but i cannot rebind to the table to refresh.  An error message comes up.
Ive seen something on forums saying i should use (int) in front of bookingID = Me.SlotsBookedList.DataKeys(e.RowIndex).Value
ie.bookingID = (int) Me.SlotsBookedList.DataKeys(e.RowIndex).Value.
But this doesnt work.

Any clues

  Protected Sub SlotsBookedList_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles SlotsBookedList.RowDeleting

        'finds the primary key id of the gridview selected row,so we can parse it to the procedure that deletes bookings
     

        Dim bookingID As Integer


        bookingID = Me.SlotsBookedList.DataKeys(e.RowIndex).Value

        ctb.DeleteBookings(bookingID)

     
     

        If Not Page.IsPostBack Then
            Me.SlotsBookedList.DataBind()

        End If

    End Sub
Ok ive found the solution.

Protected Sub SlotsBookedList_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles SlotsBookedList.RowDeleting

        'finds the primary key id of the gridview selected row,so we can parse it to the procedure that deletes bookings
        'Dim bookingID As Integer = Me.SlotsBookedList.DataKeys(e.RowIndex).Value

        Dim bookingID As Integer


        'gets the bookingid from the selected row
        bookingID = Me.SlotsBookedList.DataKeys(e.RowIndex).Value

        'passes the bookingid to the deleting storedprocedure in the database
        ctb.DeleteBookings(bookingID)

        'rebinds the data to the table, ie. with deleted row
        Me.SlotsBookedList.DataBind()

        'reloads the page, to show the newly updated table, without re-pulling from the database
        Response.Redirect("StudentUnbook.aspx")




    End Sub

The redirect to the page, reloads the first procedure which repopulates the table.
Cool. Best of luck :)
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial