我有一个绑定到 SqlDataSource 的 GridView。我遇到了 UpdateCommand 执行的问题,它似乎执行了两次。当我单步执行 gvChecklist_RowUpdating 事件中的代码时,我看到一切运行正常并且存储过程已执行。我已经验证了存储过程可以正常工作,因为 db 表中有一条新记录。但是,当此事件退出时,我收到以下错误:
过程或函数 usp_TestLogInsert 指定的参数过多。
我看到数据源上的 Updating 事件在 RowUpdating 事件之后被调用。我试图取消此事件以防止多次更新尝试,但随后什么也没有发生,并且 GridView 保持在编辑模式。
ASP 代码
<asp:UpdatePanel ID="upGridView" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvChecklist" runat="server"
AutoGenerateColumns="false" DataSourceID="dsChecklist"
AutoGenerateEditButton="true"
onrowupdating="gvChecklist_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"
Text='<%#Eval("Status") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddStatus" runat="server"
DataTextField="Status"
DataValueField="ID"
DataSourceID="dsStatus" />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Division"
HeaderText="Division"
readonly="true" />
<asp:BoundField DataField="Application"
HeaderText="Application"
readonly="true" />
<asp:BoundField DataField="Task"
HeaderText="Task"
readonly="true" />
<asp:BoundField DataField="TestedBy"
HeaderText="Tested By"
readonly="true"/>
<asp:BoundField DataField="Notes"
HeaderText="Notes"
ReadOnly="false"/>
<asp:BoundField DataField="JiraTicket"
HeaderText="JIRA Ticket"
readonly="false" />
<asp:BoundField DataField="ID" />
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="gvChecklist" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
</table>
<asp:SqlDataSource ID="dsChecklist"
SelectCommand=" SELECT l.ID, d.division, c.Context AS Application, t.Task, l.TestedBy, l.Notes, l.JiraTicket, s.Status
FROM Automation.manual.Tests t
OUTER APPLY
(SELECT TOP 1 *
FROM Automation.manual.TestLog l
WHERE l.TestID = t.ID
ORDER BY l.Date DESC) l
INNER JOIN Automation.dbo.Context c ON c.ID = t.Context
INNER JOIN Automation.dbo.Division d ON d.ID = t.Division
LEFT OUTER JOIN Automation.manual.Status s ON s.ID = l.Status"
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"
onupdating="dsChecklist_Updating" >
<UpdateParameters>
<asp:Parameter Name="Status" DbType="Int32" />
<asp:Parameter Name="TestID" DbType="Int32" />
<asp:Parameter Name="TestedBy" DbType="String" />
<asp:Parameter Name="Notes" DbType="String" />
<asp:Parameter Name="JiraTicket" DbType="String" />
<asp:Parameter Name="Build" DbType="String" />
</UpdateParameters>
</asp:SqlDataSource>
C#
protected void gvChecklist_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SQLConnections sql = new SQLConnections();
SqlDataSource dsChecklist = (SqlDataSource)LoginView1.FindControl("dsChecklist");
var dd = (DropDownList)gvChecklist.Rows[e.RowIndex].FindControl("ddStatus");
var status = dd.SelectedValue;
var testID = sql.SQLSelectSingle(String.Format("SELECT ID FROM Automation.manual.Tests WHERE Task = '{0}'", (String)e.OldValues["Task"]), "pwautosql01");
string user = Page.User.Identity.Name;
string notes = (String)e.NewValues["Notes"];
string jira = (String)e.NewValues["JiraTicket"];
var dbID = e.NewValues["ID"];
string build = "TODO";
if (dbID == null) //Record does not exist in TestLog, INSERT a new one
{
dsChecklist.UpdateCommand = "[Automation].[manual].[usp_TestLogInsert]";
dsChecklist.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
dsChecklist.UpdateParameters["Status"].DefaultValue = status;
dsChecklist.UpdateParameters["TestID"].DefaultValue = testID;
dsChecklist.UpdateParameters["TestedBy"].DefaultValue = user;
dsChecklist.UpdateParameters["Notes"].DefaultValue = notes;
dsChecklist.UpdateParameters["JiraTicket"].DefaultValue = jira;
dsChecklist.UpdateParameters["Build"].DefaultValue = build;
gvChecklist.DataBind();
}
//else //Record already exists in TestLog. UPDATE
//{
//TODO
//}
}
编辑:我使用 SQL Profiler 查看存储过程中发送了哪些参数。即使发生 INSERT 并创建了新记录,也会发送一个名为 ID 的额外参数。我从来没有明确发送过这个参数,但它似乎是因为我的 GridView 中有一个名为 ID 的 BoundField 才创建的。为了建立在原始问题的基础上,有没有办法让 UpdateCommand 忽略这个 BoundField 所以它不会自动将它用作参数?