过程期望参数不是提供错误

时间:2013-10-08 14:50:57

标签: c# asp.net sql-server stored-procedures

我在DB中存储了接受4个参数的过程。当我从SQLDatasource调用此过程并通过sqldatasource向导传递其参数时,我得到以下错误

  

错误[42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]过程或函数'director_proc'需要参数'@Department',它未提供。

下面是sqldatasource标记和存储过程的代码

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
SelectCommand="director_proc" SelectCommandType="StoredProcedure">
 <SelectParameters>
<asp:ControlParameter ControlID="ASPxComboBox1" DbType="String" 
DefaultValue="Enterprise solution" Name="Department" PropertyName="Value" 
Type="String" />
 <asp:ControlParameter ControlID="ASPxComboBox2" DbType="String" 
DefaultValue="Enterprise operations" Name="Section" PropertyName="Value" 
Type="String" />
<asp:ControlParameter ControlID="MonthEdit1" DbType="Int16" DefaultValue="7" 
Name="Month" PropertyName="Month" Type="Decimal" />
<asp:ControlParameter ControlID="ASPxComboBox3" DbType="Int16" 
 DefaultValue="2013" Name="Year" PropertyName="Value" Type="Decimal" />
 </SelectParameters>
</asp:SqlDataSource>

存储过程

    USE [AccessmgmtDB]
GO
/****** Object:  StoredProcedure [dbo].[director_proc]    Script Date: 10/08/2013 14:22:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[director_proc]
    -- Add the parameters for the stored procedure here
@Department nchar(50),
@Section nchar(50),
@Month numeric,
@Year numeric
AS
BEGIN
    if(@Section is null)
    SELECT SSNO ,Full_Name,152.5 [Working Hours],sum(diff) Actual_Hours,MONTH(date) as month,Year(date) Year
         ,(case when 152.5-sum([diff])<0 then 0 else 152.5-sum([diff])end) as [Missing Hours]
         ,(case when 152.5-sum([diff])<0 then 0 else (152.5-sum([diff]))/8.5 end) as [wanted days]
         ,section
         FROM attendance2
         where Department=@Department and MONTH ( date )=@Month and Year(date)=@Year
         group by SSNO,Full_Name,section,MONTH(date),Year(date) order by full_name;
         else
         SELECT SSNO ,Full_Name,152.5 [Working Hours],sum(diff) Actual_Hours,MONTH(date) as month,Year(date) Year
         ,(case when 152.5-sum([diff])<0 then 0 else 152.5-sum([diff])end) as [Missing Hours]
         ,(case when 152.5-sum([diff])<0 then 0 else (152.5-sum([diff]))/8.5 end) as [wanted days]
         ,section
         FROM attendance2
         where Department=@Department and MONTH ( date )=@Month and Year(date)=@Year and section=@Section
         group by SSNO,Full_Name,section,MONTH(date),Year(date) order by full_name;
END

4 个答案:

答案 0 :(得分:0)

而不是:

        <asp:ControlParameter ControlID="ASPxComboBox1" DbType="String" DefaultValue="Enterprise solution" Name="Department" PropertyName="Value" Type="String" />

尝试:

        <asp:ControlParameter ControlID="ASPxComboBox1" DbType="String" DefaultValue="Enterprise solution" Name="Department" PropertyName="SelectedValue" Type="String" />

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.controlparameter.propertyname.aspx

答案 1 :(得分:0)

我认为问题在于你绑定了一个DevExpress组合框。尝试在ASPxComboBox控件上设置AutoPostback="true"。另外,确保你的SqlDataSource与DevExpress控件位于同一个命名容器中...他们过去曾遇到过这个问题,但我多年没有使用它们所以我不知道它是否还是一个问题。

答案 2 :(得分:0)

尝试使用SelectedItem.Value代替组合框的控制参数值。

 <SelectParameters>
      <asp:ControlParameter ControlID="ASPxComboBox1" DbType="String" 
       DefaultValue="Enterprise solution" Name="Department"  
       PropertyName="SelectedItem.Value" 
       Type="String" />
    <asp:ControlParameter ControlID="ASPxComboBox2" DbType="String" 
       DefaultValue="Enterprise operations" Name="Section"     
       PropertyName="SelectedItem.Value" 
       Type="String" />
   <asp:ControlParameter ControlID="MonthEdit1" DbType="Int16" DefaultValue="7" 
       Name="Month" PropertyName="Month" Type="Decimal" />
   <asp:ControlParameter ControlID="ASPxComboBox3" DbType="Int16" 
       DefaultValue="2013" Name="Year" PropertyName="SelectedItem.Value" Type="Decimal" />
    </SelectParameters>

答案 3 :(得分:0)

好消息是我成功解决了这个问题。 我正在使用odbc连接,似乎通过ODBC连接传递参数时发生错误。所以我删除odbc并用常规的sql连接替换它,一切都很顺利。

感谢所有人的帮助。