gpt4 book ai didi

c# - SqlDataSource 过程或函数需要未指定的参数

转载 作者:行者123 更新时间:2023-11-30 20:56:34 25 4
gpt4 key购买 nike

我有一个 SqlDataSource,我正在尝试向其添加 UpdateCommand,以便在选择 GridView 的更新按钮时调用存储过程。我在后面的代码中执行此操作,因为我需要调用两个过程之一,具体取决于记录是否已存在于数据库表中(INSERT 用于新记录,UPDATE 用于现有记录)。当我在 RowUpdating 事件中设置 Updateparameters 时,出现以下异常:

Procedure or function 'usp_TestLogInsert' expects parameter '@Status', which was not supplied.

这是 ASP 代码和背后的代码。我在后面的代码中明确创建了参数,所以我不明白为什么会出现此异常。我已经验证这些是存储过程所需的相同参数。

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.Add("@Status", TypeCode.Int32, status);
dsChecklist.UpdateParameters.Add("@TestID", TypeCode.Int32, testID);
dsChecklist.UpdateParameters.Add("@TestedBy", TypeCode.String, user);
dsChecklist.UpdateParameters.Add("@Notes", TypeCode.String, notes);
dsChecklist.UpdateParameters.Add("@JiraTicket", TypeCode.String, jira);
dsChecklist.UpdateParameters.Add("@Build", TypeCode.String, build);

dsChecklist.Update();
}
//else //Record already exists in TestLog. UPDATE
//{
//TODO
//}
}

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"
UpdateCommand=""
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>" />

<asp:SqlDataSource ID="dsStatus" SelectCommand="SELECT ID, Status FROM Automation.manual.Status"
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>">
</asp:SqlDataSource>

最佳答案

您需要在参数名称前省略@,SqlDataSource 会为您添加它。参见 here

还可以尝试在实例化数据源时添加参数,然后在后面的代码中设置它们的值:

<UpdateParameters>
<asp:Parameter Name="Status" Type="Int32" />
<asp:Parameter Name="TestID" Type="Int32" />
<asp:Parameter Name="TestedBy" Type="String" />
<asp:Parameter Name="Notes" Type="String" />
<asp:Parameter Name="JiraTicket" Type="String" />
<asp:Parameter Name="Build" Type="String" />
</UpdateParameters>

然后:

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"].Value = status;
dsChecklist.UpdateParameters["@TestID"].Value = testID;
dsChecklist.UpdateParameters["@TestedBy"].Value = user;
dsChecklist.UpdateParameters["@Notes"].Value = notes;
dsChecklist.UpdateParameters["@JiraTicket"].Value = jira;
dsChecklist.UpdateParameters["@Build"].Value = build;

dsChecklist.Update();
}

关于c# - SqlDataSource 过程或函数需要未指定的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17456470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com