Hello friends,
I'd like to retrieve the primary key of the row being deleted so i can use it as a parameter in my stored procedure: Here's how far i have come with my code. I need help with line 6.
Thanks in advance :)
1 Protected Sub DependentsObjectDataSource_Deleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles DependentsObjectDataSource.Deleting
2 3 'get value of deleting depednent id
4 'Delete (Unenroll) all benefits dependent enrolled in before attempting to delete dependent 5 Dim empIdString As String = GetEmpId()
6 Dim dependentIdString As String = me.DependentsGridView. 'Me.DependentsGridView.DeleteRow.SelectedValue.ToString() 'Me.DependentsGridView.SelectedRow.Cells(0).Text 'Me.DependentsGridView.SelectedValue.ToString() 'get dependentId from selected congtrol 7 8 If Not empIdString Is Nothing Then 9 'empIdString = querystring
10 '1 - Import sql types at top of code
11 '2 - declare variables 12 Dim conn As SqlConnection
13 Dim cmd As SqlCommand
14 15 '3 - create connection 16 conn = New SqlConnection
17 conn.ConnectionString = ConfigurationManager.ConnectionStrings("JMMConnectionString").ConnectionString
18 conn.Open()
19 20 '4 - create command 21 cmd = New SqlCommand
22 cmd.CommandType = CommandType.StoredProcedure
23 cmd.CommandText = "UnenrollDependentsFromBenefits" 24 '4.2 -Declare a parameter 25 Dim param As SqlParameter
26 param = New SqlParameter("@empId", SqlDbType.VarChar, 20)
27 param.Direction = ParameterDirection.Input
28 param.Value = empIdString
29 30 '5-Link command to connection 31 cmd.Connection = conn
32 cmd.Parameters.Add(param)
33 cmd.Parameters.Add(New SqlParameter("@dependentId", SqlDbType.VarChar, 20))
34 cmd.ExecuteNonQuery()
35 conn.Close()
36 End If 37 38 'Refresh benefits views to reflect deletions 39 Me.BenefitEnrollmentsGridView.DataBind()
40 Me.BenefitEnrollmentsDetailsView.DataBind()
41 42 End Sub
ASP.NET Newbie
![]() |
0 |
![]() |
DependentsGridView.SelectedValue.ToString() should work, so long as you have the DataKeyNames property set the the primary key field.
Regards Mike
[MVP - ASP/ASP.NET]
My site
![]() |
0 |
![]() |
Thanks Mike,
I get the error "Object reference not set to an instance of an object." When i use this in line 6: What do you think?
Dim dependentIdString As String = Me.DependentsGridView.SelectedValue.ToString
ASP.NET Newbie
![]() |
0 |
![]() |
I might be having problem with a null reference since i haven't selected any value in my gridview. I have simply clicked on a delete button to delete.
Please help me on how to retrieve my pk value on deleting....thanks :)
ASP.NET Newbie
![]() |
0 |
![]() |
anybody?
ASP.NET Newbie
![]() |
0 |
![]() |
Can you show the code for your GridView?
Regards Mike
[MVP - ASP/ASP.NET]
My site
![]() |
0 |
![]() |
Hi Mike, thanks for your patience.
I am just trying to tell the method to run a delete storedprocedure, while getting the parameter from the primary key of the current record being deleted. ie. i want it to delete child records before attempting to delete the child itself.
gridview code here: Hope to hear from you!
1 <asp:GridView ID="DependentsGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="dependentId" 2 DataSourceID="DependentsObjectDataSource" OnDataBound="DependentsFormView_DataBound" Width="70%" HorizontalAlign="Center"> 3 <Columns> 4 <asp:TemplateField ShowHeader="False"> 5 <EditItemTemplate> 6 <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" 7 Text=""></asp:LinkButton> 8 </EditItemTemplate> 9 <ItemTemplate> 10 <asp:ImageButton ID="SelectImageButton" runat="server" CausesValidation="False" CommandName="Select" 11 ImageUrl="~/Images/selectbutton.gif" /> 12 </ItemTemplate> 13 </asp:TemplateField> 14 <asp:BoundField DataField="dependentId" HeaderText="dependentId" InsertVisible="False" 15 ReadOnly="True" SortExpression="dependentId" Visible="False" /> 16 <asp:BoundField DataField="empId" HeaderText="empId" SortExpression="empId" Visible="False" /> 17 <asp:BoundField DataField="dFirstName" HeaderText="FirstName" SortExpression="dFirstName" /> 18 <asp:BoundField DataField="dLastName" HeaderText="LastName" SortExpression="dLastName" /> 19 <asp:BoundField DataField="dRelationship" HeaderText="Relationship" SortExpression="dRelationship" /> 20 <asp:TemplateField ShowHeader="False"> 21 <EditItemTemplate> 22 <asp:LinkButton ID="UpdateLinkButton" runat="server" CausesValidation="True" CommandName="Update" 23 Text=""></asp:LinkButton> 24 <asp:LinkButton ID="CancelLinkButton" runat="server" CausesValidation="False" CommandName="Cancel" 25 Text=""></asp:LinkButton> 26 </EditItemTemplate> 27 <ItemTemplate> 28 <asp:ImageButton ID="DeleteImageButton" runat="server" CausesValidation="False" 29 CommandName="Delete" ImageUrl="~/Images/deleteperson.bmp" OnClientClick="return confirm('Are you certain you want to delete this dependant? \nThis action will unenroll(delete) dependent from all benefits!');" 30 ToolTip="Click to delete this dependant" /> 31 </ItemTemplate> 32 </asp:TemplateField> 33 </Columns> 34 <EmptyDataTemplate> 35 <center> No dependants entered</center> 36 </EmptyDataTemplate> 37 </asp:GridView>
ASP.NET Newbie
![]() |
0 |
![]() |
I thought, it would maybe better to do this from the OnRowDeleting event of the GridView?
In the handler, use GridViewDeleteEventArgs, then you can get the SelectedValue of the key easily.
Then I thought, wait a minute, you don't have to do any of this stuff.
Just click delete, ASP takes care of passing the key in the deleteparameter, and the deletemethod specified in the objectdatasource takes care of it all without a single line of code in your page.
Yeah OK, you have some code in the objectdatasource/business layer instead, but less, and it is tucked away separate from the presentation layer.
Unless I am missing something, of course.
If a post helps to solve your problem, please click the Answer button on that post.
I'm still confused, but now I'm confused on a higher plane.
![]() |
0 |
![]() |
Hi LockH,
Thanks for your input.
This code i found from the msdn site did the charm in helping me get the deleting key.1 Dim paramsFromPage As IDictionary = e.InputParameters 2 Dim dependentIdString As String = paramsFromPage("dependentId").ToString() 3Thanks all!
ASP.NET Newbie
![]() |
0 |
![]() |