SQLDataStore带有输出参数,获取的ParameterName不包含在此SqlParameterCollection错误中

时间:2017-12-05 22:52:16

标签: c# asp.net sql-server

我想要完成的最终目标是: 物料订单表格底部的按钮使用ProjectID和当前日期在物料订单表中插入新记录。插入记录后,将页面重定向到新的订单ID,以便进行修改。

如果我取出应该抓取插入的SCOPE_IDENTITY()输出的OnInserted方法,记录插入正常,但是通过调用,我不断获得" SQLParameter with ParameterName' NewOrderID&# 39;不包含在此SqlParameterCollection"

ASPX Formview

<asp:FormView ID="FvFooter" runat="server" DataKeyNames="OrderID" 
    DataSourceID="FvMatOrdersSQL" ForeColor="#333333" OnItemCommand="FvFooter_OnItemCommand">
    <ItemTemplate>
        <br/>
        <b>Notes</b>
        <br/>
        <asp:Label ID="TxtNotes" runat="server" BorderStyle="Solid" BorderWidth="1" 
        Text='<%# Eval("Notes").ToString().Replace(Environment.NewLine, "<br />") %>'/>
        <br/>
        <asp:LinkButton ID="LbEditNotes" runat="server" CssClass="NoPrint" 
            CausesValidation="False" CommandName="Edit" Text="Edit"/>
        <br/>
        <br/>
        <asp:Button ID="DeleteButton" runat="server" CausesValidation="False" 
            CssClass="NoPrint" CommandName="DeleteOrder" Text="Delete"/>
        &nbsp;<asp:Button ID="NewButton" runat="server" CausesValidation="True" 
            CssClass="NoPrint" CommandName="NewOrder" Text="New Order"/>
        &nbsp;<asp:Button ID="EmailButton" runat="server" CausesValidation="True" 
            CssClass="NoPrint" CommandName="Email" Text="Email"/>
    </ItemTemplate>
</asp:FormView>

SQL DataSource

<asp:SqlDataSource ID="FvMatOrdersSQL" runat="server" ConnectionString="<%$ ConnectionStrings:ProjectLogicTestConnectionString %>"
    SelectCommand="SELECT mo.OrderID, mo.ProjectID, p.ProjectName, mo.OrderedByEmpID, emp.Name, 
        mo.OrderDate, mo.DateNeeded, mo.ReasonID, mor.Description, mo.Notes 
            FROM tblMatOrder AS mo 
            LEFT OUTER JOIN tblMatOrderReason AS mor ON mo.ReasonID = mor.ReasonID 
            INNER JOIN tblProject AS p ON mo.ProjectID = p.ProjectID 
            LEFT OUTER JOIN tblEmployee AS emp ON mo.OrderedByEmpID = emp.EmployeeID
            WHERE mo.OrderID = @OrderID"
DeleteCommand="DELETE FROM tblMatOrder WHERE OrderID = @OrderID"
InsertCommand="INSERT INTO tblMatOrder (OrderDate, ProjectID) VALUES (@OrderDate, @ProjectID); 
    SELECT @NewOrderID = SCOPE_IDENTITY()"
UpdateCommand="UPDATE tblMatOrder SET OrderDate = @OrderDate, OrderedByEmpID = @OrderedByEmpID, 
 DateNeeded = @DateNeeded, ReasonID = @ReasonID, Notes = @Notes
 WHERE OrderID = @OrderID"
 OnInserted="FvMatOrdersSQL_OnInserted">
    <SelectParameters>
        <asp:RouteParameter Name="OrderID" Type="Int32" RouteKey="OrderID"/>
    </SelectParameters>
    <DeleteParameters>
        <asp:Parameter Name="OrderID" Type="Int32" DefaultValue="0"/>
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="OrderDate" Type="DateTime"/>
        <asp:Parameter Name="ProjectID" Type="Int32"/>
        <asp:Parameter Direction ="Output" Name="NewOrderID" Type="Int32" DefaultValue="0"/>
    </InsertParameters>
    <UpdateParameters>
        <asp:RouteParameter Name="OrderID" Type="Int32" RouteKey="OrderID"/>
        <asp:Parameter Name="OrderDate" Type="DateTime"/>
        <asp:Parameter Name="OrderedByEmpID" Type="Int32"/>
        <asp:Parameter Name="DateNeeded" Type="DateTime"/>
        <asp:Parameter Name="ReasonID" Type="Int32"/>
        <asp:Parameter Name="Notes" Type="String"/>
    </UpdateParameters>
</asp:SqlDataSource>

按钮点击代码隐藏

protected void FvFooter_OnItemCommand(object sender, FormViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DeleteOrder":
                    // Confirmation stuff here
                    break;
                case "NewOrder" when Page.IsValid:
                    Label lblProjectId = (Label)FvHeader.FindControl("LblProjectID");
                    String strOrderDate = DateTime.Now.ToShortDateString();
                    FvMatOrdersSQL.InsertParameters.Clear();
                    FvMatOrdersSQL.InsertParameters.Add("ProjectID", lblProjectId.Text);
                    FvMatOrdersSQL.InsertParameters.Add("OrderDate", strOrderDate);
                    FvMatOrdersSQL.Insert();
                    break;
                case "Email" when Page.IsValid:

                    break;
            }
        }

OnInserted codebehind

protected void FvMatOrdersSQL_OnInserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            String strOrderId = e.Command.Parameters["@NewOrderID"].Value.ToString();
            Response.Redirect("MatOrderDetails.aspx?OrderID=" + strOrderId);
        }

我试图将我的代码尽可能多地放在https://msdn.microsoft.com/en-us/library/ms228051.aspx的示例上,但显然我错过了一些东西。

1 个答案:

答案 0 :(得分:0)

我已经放了你的代码并创建了一个接近你的数据库选择以找出你的问题,我不得不承认这不容易:)请

更改此

 FvMatOrdersSQL.InsertParameters.Clear();
 FvMatOrdersSQL.InsertParameters.Add("ProjectID", lblProjectId.Text);
 FvMatOrdersSQL.InsertParameters.Add("OrderDate", strOrderDate);

要:

FvMatOrdersSQL.InsertParameters["ProjectID"].DefaultValue = lblProjectId.Text;
FvMatOrdersSQL.InsertParameters["OrderDate"].DefaultValue = strOrderDate;

请勿删除您已在界面中创建的参数!