GridView中的更新命令更新所有记录

时间:2016-05-10 14:08:47

标签: c# asp.net sql-server gridview

我怀疑这很可能是由于错误形成的SQL,但我在ASP.NET Web窗体中连接到SQL数据源的GridView。当我单击“编辑命令”按钮时,它会更改为“编辑模板”。但是,当我单击“更新”按钮时,更改将写入所有行,而不仅仅是单击按钮的行。该页面有一个员工下拉列表,一旦选择了员工,GridView就会填充该员工当前的任务。

ASPX

<asp:DropDownList ID="ddlEmployee" runat="server" AutoPostBack="True" DataSourceID="ddlEmployeeSQL" DataTextField="Name" DataValueField="EmployeeID">
        </asp:DropDownList>


<asp:GridView ID="gvTaskDashboard" runat="server" AutoGenerateColumns="False" DataSourceID="TaskDashboardSQL">
            <Columns>
                <asp:CommandField HeaderText="Edit" ShowEditButton="true" />
                <asp:TemplateField HeaderText="Project #" SortExpression="ProjectID">
                    <EditItemTemplate>
                        <asp:Label ID="lblProjectID" runat="server" Text='<%# Bind("ProjectID") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProjectID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Project Name" SortExpression="ProjectName">
                    <EditItemTemplate>
                        <asp:Label ID="lblProjectName" runat="server" Text='<%# Bind("ProjectName") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label10" runat="server" Text='<%# Bind("ProjectName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Release #" SortExpression="ReleaseID">
                    <EditItemTemplate>
                        <asp:Label ID="lblReleaseID" runat="server" Text='<%# Bind("ReleaseID") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("ReleaseID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Task Code" SortExpression="TaskCodeAbbr">
                    <EditItemTemplate>
                        <asp:Label ID="lblTaskCode" runat="server" Text='<%# Bind("TaskCodeAbbr") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label11" runat="server" Text='<%# Bind("TaskCodeAbbr") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Status" SortExpression="TaskStatusID">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="True" DataSourceID="ddlStatusSQL" 
                            DataTextField="TaskStatusName" DataValueField="TaskStatusID" SelectedValue='<%# Bind("TaskStatusID") %>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="ddlStatusSQL" enabled="false"
                            DataTextField="TaskStatusName" DataValueField="TaskStatusID" SelectedValue='<%# Bind("TaskStatusID") %>'>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Priority" SortExpression="TaskPriorityID">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlPriority" runat="server" AutoPostBack="True" DataSourceID="ddlPrioritySQL" 
                            DataTextField="TaskPriorityName" DataValueField="TaskPriorityID" SelectedValue='<%# Bind("TaskPriorityID") %>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" DataSourceID="ddlPrioritySQL" enabled="false"
                            DataTextField="TaskPriorityName" DataValueField="TaskPriorityID" SelectedValue='<%# Bind("TaskPriorityID") %>'>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Due Date" SortExpression="DueDate">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDueDate" runat="server" Text='<%# Bind("DueDate", "{0:MM/dd/yyyy}") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("DueDate", "{0:MM/dd/yyyy}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Notes" SortExpression="Notes">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtNotes" runat="server" Text='<%# Bind("Notes") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("Notes") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date Complete" SortExpression="CompDate">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtCompDate" runat="server" Text='<%# Bind("CompDate", "{0:MM/dd/yyyy}") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label7" runat="server" Text='<%# Bind("CompDate", "{0:MM/dd/yyyy}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            </asp:GridView>

SQL DataSource

<asp:SqlDataSource ID="ddlEmployeeSQL" runat="server" ConnectionString="<%$ ConnectionStrings:ProWorxConnectionString %>" 
            SelectCommand="SELECT [EmployeeID], [Name] FROM [tblEmployee] WHERE ([Status] = 'A') ORDER BY [Name]">
        </asp:SqlDataSource>

<asp:SqlDataSource ID="TaskDashboardSQL" runat="server" ConnectionString="<%$ ConnectionStrings:ProWorxConnectionString %>" 
                SelectCommand="SELECT tt.TaskID
                    , tt.EmployeeID
                    , tt.ProjectID
                    , tt.ReleaseID
                    , tt.TaskStatusID
                    , tt.TaskPriorityID
                    , tt.DueDate
                    , tt.Notes
                    , tt.CompDate
                    , p.ProjectName
                    , tc.TaskCodeAbbr 
                    FROM tblTaskTracker tt INNER JOIN
                        tblProject p ON tt.ProjectID = p.ProjectID INNER JOIN
                        tblTaskCode tc ON tt.TaskCodeID = tc.TaskCodeID INNER JOIN
                        tblTaskStatus ts on tt.TaskStatusID = ts.TaskStatusID
                    WHERE (tt.EmployeeID = @EmployeeID) 
                    ORDER BY ts.TaskStatusType, tt.TaskPriorityID, tt.DueDate"
                InsertCommand="INSERT INTO [tblTaskTracker] 
                    ([EmployeeID], [ProjectID], [ReleaseID], [TaskCodeID], [TaskStatusID], [TaskPriorityID], [DueDate], [Notes], [CompDate])
                    VALUES (@EmployeeID, @ProjectID, @ReleaseID, @TaskCodeID, @TaskStatusID, @TaskPriorityID, @DueDate, @Notes, @CompDate)"
                UpdateCommand="UPDATE [tblTaskTracker] SET  [TaskStatusID] = @TaskStatusID, [TaskPriorityID] = @TaskPriorityID, 
                                [DueDate] = @DueDate, [Notes] = @Notes, [CompDate] = @CompDate"
                DeleteCommand="DELETE FROM [tblTaskTracker] WHERE [TaskID] = @TaskID">

                <SelectParameters>
                    <asp:ControlParameter ControlID="ddlEmployee" Name="EmployeeID" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
                <InsertParameters>
                    <asp:Parameter Name="EmployeeID" Type="Int32" />
                    <asp:Parameter Name="ProjectID" Type="Int32" />
                    <asp:Parameter Name="ReleaseID" Type="Int32" />
                    <asp:Parameter Name="TaskCodeID" Type="Int32" />
                    <asp:Parameter Name="TaskStatusID" Type="Int32" />
                    <asp:Parameter Name="TaskPriorityID" Type="Int32" />
                    <asp:Parameter Name="DueDate" Type="DateTime" />
                    <asp:Parameter Name="Notes" Type="String" />
                    <asp:Parameter Name="CompDate" Type="DateTime" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="TaskStatusID" Type="Int32" />
                    <asp:Parameter Name="TaskPriorityID" Type="Int32" />
                    <asp:Parameter Name="DueDate" Type="DateTime" />
                    <asp:Parameter Name="Notes" Type="String" />
                    <asp:Parameter Name="CompDate" Type="DateTime" />
                </UpdateParameters>
                <DeleteParameters>
                    <asp:Parameter Name="TaskID" Type="Int32" />
                </DeleteParameters>
            </asp:SqlDataSource>

2 个答案:

答案 0 :(得分:1)

在更新中没有看到WHERE语句。

答案 1 :(得分:1)

您的更新条款:

UpdateCommand="UPDATE [tblTaskTracker] SET  [TaskStatusID] = @TaskStatusID, 
[TaskPriorityID] = @TaskPriorityID, [DueDate] = @DueDate, [Notes] = @Notes, 
[CompDate] = @CompDate"

它没有WHERE过滤器。