SqlDataSource UpdateCommand被调用两次
本文关键字:两次 UpdateCommand 调用 SqlDataSource | 更新日期: 2023-09-27 18:21:06
我有一个绑定到SqlDataSource的GridView。我在执行UpdateCommand时遇到了问题,它似乎执行了两次。当我逐步浏览gvChecklist_RowUpdating事件中的代码时,我会看到一切都正常运行,存储过程也被执行了。由于数据库表中有一条新记录,我已经验证了存储过程是否正常工作。当这个事件退出时,我得到以下错误:
过程或函数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
//}
}
EDIT:我使用SQL事件探查器来查看存储过程中发送的参数。即使发生了INSERT并创建了一条新记录,也会发送一个名为ID的额外参数。我从来没有显式地发送这个参数,但它似乎是创建的,因为我在GridView中有一个名为ID的BoundField。为了建立在最初的问题上,有没有一种方法可以让UpdateCommand忽略这个BoundField,这样它就不会自动将其用作参数?
使用SQL事件探查器后,我看到BoundField ID被隐式添加为UpdateCommand参数。将此字段更改为TemplateItem可修复此问题。
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbId" Text='<%#Eval("ID") %>' ReadOnly="true" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>