Asp.net generates a wrong SQL request when GridView.AllowPaging="true"

Viewed 58

Here's the essential code

<asp:SqlDataSource runat="server"
    ID="StuffDataSource"
    SelectCommand="select employeeID,lastName, firstName, titleOfCourtesy, birthDate, hireDate, address, city, country, homePhone from Employees"
    UpdateCommand="update Employees set lastName=@lastName, firstName=@firstName, address=@address, city=@city, country=@country, homePhone=@homePhone where employeeID=@employeeID"
    DeleteCommand="delete from Employees where employeeID=@employeeID" 
    ConnectionString="<%$ connectionStrings:NorthwindSqlString %>"
    FilterExpression="(lastName like '%{0}%' or firstName like '%{0}%') and country like '%{1}%'"
    OnFiltering="StuffDataSource_Filtering">
    <FilterParameters>
        <asp:ControlParameter Name="Name" ControlId="keyword" PropertyName="Value" DefaultValue="%"/>
        <asp:ControlParameter Name="Country" ControlId="country" PropertyName="Value" DefaultValue="%"/>
    </FilterParameters>
</asp:SqlDataSource>

<asp:GridView runat="server" 
    ID="StuffGridView" 
    DataSourceID="StuffDataSource" 
    OnRowCommand="StufGridView_RowCommand"
    DataKeyNames="employeeID"
    AutoGenerateColumns="false" 
    AllowPaging="true" PageSize="20">
    <Columns>
        <asp:TemplateField HeaderText="序号">
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# (Container.DataItemIndex + 1).ToString() %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="employeeID" HeaderText="id" Visible="false" />
        <asp:BoundField DataField="lastName" HeaderText="名" />
        <asp:BoundField DataField="firstName" HeaderText="姓" />
        <asp:BoundField DataField="titleOfCourtesy" ReadOnly="true" HeaderText="性别" />
        <asp:BoundField DataField="birthDate" HeaderText="出生日期" ReadOnly="true" Visible="false" />
        <asp:BoundField DataField="hireDate" HeaderText="雇用日期" ReadOnly="true" />
        <asp:BoundField DataField="address" HeaderText="地址" />
        <asp:BoundField DataField="city" HeaderText="城市" />
        <asp:BoundField DataField="country" HeaderText="国籍" />
        <asp:BoundField DataField="homePhone" HeaderText="联系" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" ID="Edit" Text="编辑" CommandName="Edit" CommandArgument='<%# Container.DataItemIndex.ToString() %>' Visible='<%# StuffGridView.EditIndex != Container.DataItemIndex ? true : false %>' />
                <asp:Button runat="server" ID="Delete" Text="删除" CommandName="RequestDelete" CommandArgument='<%# Container.DataItemIndex.ToString() + "," + Eval("firstName").ToString() + " " + Eval("lastName").ToString() %>' Visible='<%# StuffGridView.EditIndex != Container.DataItemIndex ? true : false %>' />
                <asp:Button runat="server" ID="Update" Text="更新" CommandName="Update" CommandArgument='<%# Container.DataItemIndex.ToString() %>' Visible='<%# StuffGridView.EditIndex == Container.DataItemIndex ? true : false %>' />
                <asp:Button runat="server" ID="Cancel" Text="取消" CommandName="Cancel" CommandArgument='<%# Container.DataItemIndex.ToString() %>' Visible='<%# StuffGridView.EditIndex == Container.DataItemIndex ? true : false %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Let's focus on SqlDataSource.DeleteCommand and GridView.AllowPaging.

When the GridView.AllowPaging="false", I can delete record normally. But when I set GridView.AllowPaging="true", I can't delete record except the record be on the first page and get this error from SQL SERVER

Must declare the scalar variable “@employeeID” in delete statement

But I think everything has prepared. There are DataKeyNames set to employeeID, a BoundField include a DataField set to employeeID. And if there are some errors, why does it work when AllowPaging is set to false?

So I want to know what requests SQL SERVER actually receive. I started a Profiler on SQL SERVER management studio. Here's the result:

AllowPaging="false" -> delete records on any page

AllowPaging="true" -> delete records on the first page

exec sp_executesql N'delete from Employees where employeeID=@employeeID',N'@employeeID int',@employeeID=37

AllowPaging="true" -> delete records on any page except the first page

delete from Employees where employeeID=@employeeID

So why does Asp.Net can't generate the right SQL request when AllowPaging = "true" and delete records in any page except the first page? I can't understand, and how to avoid this issue?

1 Answers

In this line:

<asp:Button runat="server" ID="Delete" Text="删除" CommandName="RequestDelete" CommandArgument='<%# Container.DataItemIndex.ToString() + "," + Eval("firstName").ToString() + " " + Eval("lastName").ToString() %>' Visible='<%# StuffGridView.EditIndex != Container.DataItemIndex ? true : false %>' />

Problem could be due to DataItemIndex. According to this, DataTable don't know on which page you are. So you must set proper index by offset formula.

Try something like this:

<asp:Button runat="server" ID="Delete" Text="删除" CommandName="RequestDelete" CommandArgument='<%# Container.DataItemIndex.ToString() + ((StuffGridView.ActivePage - 1) * StuffGridView.PageSize) + "," + Eval("firstName").ToString() + " " + Eval("lastName").ToString() %>' />
Related