存储过程需要未提供的参数

时间:2014-10-24 17:58:17

标签: c# asp.net stored-procedures

我有一个SqlDataSource连接到GridView,我正在尝试使编辑正常工作,但是我尝试的所有内容仍然没有提供参数。

我尝试使用和不使用'@'命名参数,似乎没有区别。正如您在下图中所看到的,更新参数存在,甚至还有值!

示例图片:

Image of error

ASPX标记:

<asp:GridView runat="server" ID="JobGV" DataSourceID="JobApprovalsDS" 
     AutoGenerateColumns="False" AutoGenerateEditButton="true" 
     OnRowCommand="JobGV_OnRowCommand" OnRowUpdating="JobGV_OnRowUpdating">
    <Columns>
        <asp:BoundField HeaderText="Function" DataField="FunDesc" ReadOnly="true"/>
        <asp:BoundField HeaderText="Employee" DataField="EmpName" ReadOnly="true"/>
        <asp:TemplateField>
            <EditItemTemplate>
                <asp:DropDownList runat="server" ID="ddlEmps" SelectedValue='<%# Eval("appEmpID")%>' DataSourceID="EmpDS" DataTextField="EmpName" DataValueField="EmpID" />
                <asp:Label runat="server" ID="data" Text='<%# Eval("appBusinessUnit") +";" + Eval("appFunctID") %>' Visible="False"></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# Eval("EmpName") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="JobApprovalsDS" runat="server" 
     ConnectionString="<%$ ConnectionStrings:JobClose %>" 
     SelectCommand="up_JobApprovalsSelect" 
     SelectCommandType="StoredProcedure" 
     UpdateCommand="up_JobApprovalsUpdate">
    <SelectParameters>
        <asp:Parameter Name="ShowAll" DefaultValue="1" />
        <asp:Parameter Name="AllPhases" DefaultValue="1" />
        <asp:ControlParameter Name="BusinessUnit" ControlID="ddlEditJob" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="@BusinessUnit" Type="String"/>
        <asp:Parameter Name="@FunctID" Type="Int32"/>
        <asp:Parameter Name="@EmpID" Type="Int32"/>
        <asp:Parameter Name="@UpdateDate" Type="DateTime"/>
    </UpdateParameters>
</asp:SqlDataSource>

C#:

protected void JobGV_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
            if (e.CommandName.Equals("Update"))
            {
                int empid = 0;
                string[] data;
                int index = int.Parse(e.CommandArgument.ToString());
                GridViewRow row = JobGV.Rows[index];
                DropDownList ddlemp = (DropDownList) row.FindControl("ddlEmps");
                empid = int.Parse(ddlemp.SelectedValue);
                Label lbl = (Label) row.FindControl("data");
                data = lbl.Text.Split(';');

                JobApprovalsDS.UpdateParameters["@BusinessUnit"].DefaultValue = data[0];
                JobApprovalsDS.UpdateParameters["@FunctID"].DefaultValue = data[1];
                JobApprovalsDS.UpdateParameters["@EmpID"].DefaultValue = empid.ToString();
                //JobApprovalsDS.UpdateParameters.Add("BusinessUnit", data[0]);
                //JobApprovalsDS.UpdateParameters.Add("FunctID", data[1]);
                //JobApprovalsDS.UpdateParameters.Add("EmpID", empid.ToString());
            }
}

protected void JobGV_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
    JobApprovalsDS.Update();
}

1 个答案:

答案 0 :(得分:2)

您是在谈论更新流程吗?

好吧,在您的SqlDataSource中,您指定UpdateCommand正在与存储过程进行通信...您需要指定UpdateCommandType也是! (不只是SelectCommandType

<asp:SqlDataSource ID="JobApprovalsDS" runat="server" 
     ConnectionString="<%$ ConnectionStrings:JobClose %>" 
     SelectCommand="up_JobApprovalsSelect" 
     SelectCommandType="StoredProcedure" 
     UpdateCommand="up_JobApprovalsUpdate"
     UpdateCommandType="StoredProcedure" >   **** this is missing from your code!