我有一个绑定到SqlDataSource的GridView。我在执行UpdateCommand时遇到问题,它似乎正在执行两次。当我逐步浏览gvChecklist_RowUpdating事件中的代码时,我看到一切正常运行并且存储过程已执行。我已经验证了存储过程可以正常工作,因为db表中有一条新记录。当此事件退出时,出现以下错误:
过程或函数usp_TestLogInsert参数过多
指定。
我看到在RowUpdating事件之后调用了数据源上的Updating事件。我试图取消此事件以防止多次更新尝试,但是随后什么也没有发生,并且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事件探查器来查看在存储过程中正在发送什么参数。即使发生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>
关于c# - 两次调用SqlDataSource UpdateCommand,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17491739/