I am trying to delete a record from my Gridview and I get a InvalidCastException with the message "Conversion from string "RefYear" to type 'Integer' is not valid." I am also using Data Keys.
Gridview Code section for column in question
<asp:TemplateField HeaderText="Ref. Year" SortExpression="ReferenceYear" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RefYear") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("RefYear") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsertRecord" runat="server" CommandName="Insert" Text="Insert Record"
ValidationGroup="Insert" CssClass="buttonBlue" />
</FooterTemplate>
</asp:TemplateField>
Code Behind
Protected Friend Sub dgvBusinessCompetitions_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) Handles dgvBusinessCompetitions.RowDeleting
' Read the keys from data keys
Dim _rewardDescription As String = dgvBusinessCompetitions.DataKeys(e.RowIndex)("RewardDescription")
Dim _referenceYear As Integer = dgvBusinessCompetitions.DataKeyNames(1)
Company.Applications.ProductionEngine.BusinessAccess.BusinessCompetitionBusinessAccess.DeleteBusinessCompetitionReward(_rewardDescription,
_referenceYear,
_groupType)
InformationBox.ShowSuccessMessage("Record deleted successfully.")
loadBusinessCompetitionsConditions()
End Sub
Stored Procedure
ALTER PROCEDURE [dbo].[my_procedure]
@rewardDescription VARCHAR(100),
@groupType VARCHAR (50),
@refYear INT
AS
BEGIN
DELETE FROM myTable
WHERE RewardDescription = @rewardDescription
AND GroupType = @groupType
AND RefYear = @refYear
END
What I need to be done, is upon clicking delete, I need the particular record with X reward description, X Group type and X RefYear to be deleted as the stored procedure displays.
I've tried converting RefYear but it didn't work.
Any ideas?
Changed
Dim _referenceYear As Integer = dgvBusinessCompetitions.DataKeyNames(1)
To
Dim _referenceYear As Integer = dgvBusinessCompetitions.DataKeys(e.RowIndex)("RefYear")