2

我一直在研究这个gridview,似乎一切都应该工作,但是当我执行删除命令时出现错误“必须声明标量变量”。真正奇怪的是,如果我删除现有的更新命令并将删除命令转换为更新命令,它会起作用并删除记录。知道为什么它可以作为更新而不是删除吗?我正在使用.net 3.5

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="Project_List" 
        Width="800px" EditRowStyle-Width="800px" RowStyle-BackColor="#D8D8D8" AlternatingRowStyle-BackColor="#ffffff">
        <Columns>
            <asp:TemplateField HeaderText="Updates">
                <ItemTemplate>
                    <asp:ImageButton ID="gridEditButton" runat="server" CommandName="Edit" ImageUrl="~/images/edit.png" />
                    <asp:ImageButton ID="gridDeleteButton" runat="server" CommandName="Delete" ImageUrl="~/images/cancel.png" OnClientClick="return confirm('Are you sure you want to delete this record?');" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:ImageButton ID="gridUpdateButton" runat="server" CommandName="Update" ImageUrl="~/images/save.png" />
                    <asp:ImageButton ID="gridCancelButton" runat="server" CommandName="Cancel" ImageUrl="~/images/cancel.png" />
                </EditItemTemplate>
            </asp:TemplateField>

            <asp:BoundField DataField="project_id" ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol" />
            <asp:BoundField DataField="functional_sponsor" HeaderText="Functional Sponsor" SortExpression="functional_sponsor" />
            <asp:BoundField DataField="program_name" HeaderText="Program" SortExpression="program_name" />
            <asp:BoundField DataField="project_name" HeaderText="Project" SortExpression="project_name" />
            <asp:BoundField DataField="approval_status" HeaderText="Approval Status" SortExpression="approval_status" />
            <asp:BoundField DataField="approved_date" HeaderText="Approved Date" SortExpression="approved_date" />
            <asp:BoundField DataField="approved_by" HeaderText="Approved By" SortExpression="approved_by" />
            <asp:BoundField DataField="charge_code" HeaderText="Charge Code" SortExpression="charge_code" />
            <asp:BoundField DataField="open_date" HeaderText="Open Date" SortExpression="open_date" />
            <asp:BoundField DataField="close_date" HeaderText="Close Date" SortExpression="close_date" />
            <asp:BoundField DataField="project_sponsor" HeaderText="Sponsor" SortExpression="project_sponsor" />
            <asp:BoundField DataField="project_manager" HeaderText="Project Manager" SortExpression="project_manager" />
            <asp:BoundField DataField="technical_lead" HeaderText="Technical Lead" SortExpression="technical_lead" />
            <asp:BoundField DataField="approved_amount" HeaderText="Approved Amount ($K)" SortExpression="approved_amount" />
            <asp:BoundField DataField="requested_amount" HeaderText="Requested Amount ($K)" SortExpression="requested_amount" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="Project_List" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ProjectDB %>" 
        SelectCommand="SELECT * FROM [vw_IT_Portfolio] ORDER BY [functional_sponsor], [program_name], [project_name]"
        DeleteCommand="EXEC [Utilities_SN].[dbo].[sp_Delete_ITPortfolio_Record] @projectID = @project_id"
        UpdateCommand="EXEC [Utilities_SN].[dbo].[sp_Update_ITPortfolio_Record]
                        @projectID = @project_id
                       ,@functionalSponsor = @functional_sponsor
                       ,@program = @program_name
                       ,@projectName = @project_name
                       ,@approvalStatus = @approval_status
                       ,@approvedDate = @approved_date
                       ,@approvedBy = @approved_by
                       ,@chargeCode = @charge_code
                       ,@openDate = @open_date
                       ,@closeDate = @close_date
                       ,@sponsor = @project_sponsor
                       ,@projectManager = @project_manager
                       ,@technicalLead = @technical_lead
                       ,@approvedAmount = @approved_amount">
        <DeleteParameters>
            <asp:Parameter Type="Int32" Name="project_id"></asp:Parameter>
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Type="Int32" Name="project_id"></asp:Parameter>
            <asp:Parameter Type="String" Name="functional_sponsor"></asp:Parameter>
            <asp:Parameter Type="String" Name="program_name"></asp:Parameter>
            <asp:Parameter Type="String" Name="project_name"></asp:Parameter>
            <asp:Parameter Type="String" Name="approval_status"></asp:Parameter>
            <asp:Parameter Type="String" Name="approved_date"></asp:Parameter>
            <asp:Parameter Type="String" Name="approved_by"></asp:Parameter>
            <asp:Parameter Type="String" Name="charge_code"></asp:Parameter>
            <asp:Parameter Type="String" Name="open_date"></asp:Parameter>
            <asp:Parameter Type="String" Name="close_date"></asp:Parameter>
            <asp:Parameter Type="String" Name="project_sponsor"></asp:Parameter>
            <asp:Parameter Type="String" Name="project_manager"></asp:Parameter>
            <asp:Parameter Type="String" Name="technical_lead"></asp:Parameter>
            <asp:Parameter Type="Decimal" Name="approved_amount"></asp:Parameter>
        </UpdateParameters>
    </asp:SqlDataSource>
4

1 回答 1

1

在 GridView 中设置 DataKeyNames = project_id

ASPX 页面:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="Project_List" 
    Width="800px" EditRowStyle-Width="800px" RowStyle-BackColor="#D8D8D8" 
    AlternatingRowStyle-BackColor="#ffffff" DataKeyNames="project_id">
    <Columns>
        <asp:TemplateField HeaderText="Updates">
            <ItemTemplate>
                <asp:ImageButton ID="gridEditButton" runat="server" CommandName="Edit" ImageUrl="~/images/edit.png" ToolTip="Edit" />
                <asp:ImageButton ID="gridDeleteButton" runat="server" CommandArgument="Delete" CommandName="Delete" ImageUrl="~/images/cancel.png" ToolTip="Delete"  OnClientClick="return confirm('Are you sure you want to delete this record?');" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:ImageButton ID="gridUpdateButton" runat="server" CommandName="Update" ImageUrl="~/images/save.png" ToolTip="Update"/>
                <asp:ImageButton ID="gridCancelButton" runat="server" CommandName="Cancel" ImageUrl="~/images/cancel.png" ToolTip="Cancel"/>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="project_id" ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol" />
        <asp:BoundField DataField="functional_sponsor" HeaderText="Functional Sponsor" SortExpression="functional_sponsor" />
        <asp:BoundField DataField="program_name" HeaderText="Program" SortExpression="program_name" />
        <asp:BoundField DataField="project_name" HeaderText="Project" SortExpression="project_name" />
        <asp:BoundField DataField="approval_status" HeaderText="Approval Status" SortExpression="approval_status" />
        <asp:BoundField DataField="approved_date" HeaderText="Approved Date" SortExpression="approved_date" />
        <asp:BoundField DataField="approved_by" HeaderText="Approved By" SortExpression="approved_by" />
        <asp:BoundField DataField="charge_code" HeaderText="Charge Code" SortExpression="charge_code" />
        <asp:BoundField DataField="open_date" HeaderText="Open Date" SortExpression="open_date" />
        <asp:BoundField DataField="close_date" HeaderText="Close Date" SortExpression="close_date" />
        <asp:BoundField DataField="project_sponsor" HeaderText="Sponsor" SortExpression="project_sponsor" />
        <asp:BoundField DataField="project_manager" HeaderText="Project Manager" SortExpression="project_manager" />
        <asp:BoundField DataField="technical_lead" HeaderText="Technical Lead" SortExpression="technical_lead" />
        <asp:BoundField DataField="approved_amount" HeaderText="Approved Amount ($K)" SortExpression="approved_amount" />
        <asp:BoundField DataField="requested_amount" HeaderText="Requested Amount ($K)" SortExpression="requested_amount" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="Project_List" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProjectDB %>" 
    SelectCommand="SELECT * FROM [vw_IT_Portfolio] ORDER BY [functional_sponsor], [program_name], [project_name]"
    DeleteCommand="DELETE FROM [vw_IT_Portfolio] WHERE [project_id] = @project_id" 

    UpdateCommand="UPDATE [vw_IT_Portfolio] SET [functional_sponsor] = @functional_sponsor, [program_name] = @program_name, [project_name] = @project_name, [approval_status] = @approval_status, [approved_date] = @approved_date, [approved_by] = @approved_by, [charge_code] = @charge_code, [open_date] = @open_date, [close_date] = @close_date, [project_sponsor] = @project_sponsor, [project_manager] = @project_manager, [technical_lead] = @technical_lead, [approved_amount] = @approved_amount, [requested_amount] = @requested_amount WHERE [project_id] = @project_id">
    <DeleteParameters>
         <asp:Parameter Name="project_id" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Type="Int32" Name="project_id"></asp:Parameter>
        <asp:Parameter Type="String" Name="functional_sponsor"></asp:Parameter>
        <asp:Parameter Type="String" Name="program_name"></asp:Parameter>
        <asp:Parameter Type="String" Name="project_name"></asp:Parameter>
        <asp:Parameter Type="String" Name="approval_status"></asp:Parameter>
        <asp:Parameter Type="String" Name="approved_date"></asp:Parameter>
        <asp:Parameter Type="String" Name="approved_by"></asp:Parameter>
        <asp:Parameter Type="String" Name="charge_code"></asp:Parameter>
        <asp:Parameter Type="String" Name="open_date"></asp:Parameter>
        <asp:Parameter Type="String" Name="close_date"></asp:Parameter>
        <asp:Parameter Type="String" Name="project_sponsor"></asp:Parameter>
        <asp:Parameter Type="String" Name="project_manager"></asp:Parameter>
        <asp:Parameter Type="String" Name="technical_lead"></asp:Parameter>
        <asp:Parameter Type="Decimal" Name="approved_amount"></asp:Parameter>
        <asp:Parameter Name="requested_amount" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>
于 2013-08-18T14:21:03.723 回答