在代码隐藏中找不到存储过程参数

时间:2014-01-08 14:36:59

标签: c# code-behind

概述:我有一个下拉列表,其中包含用户可以运行的报告列表。在包含此列表的表中,我有ReportID,ReportName,SProc和SQLView字段。这里的想法是,用户选择一个报告名称,并根据该名称运行特定的存储过程,然后将特定视图绑定到数据网格以显示该报告。对于某些报告,您需要输入日期,而其他报告则不需要输入日期。

守则:这是我写的;

    protected void btnSubmit_OnClick(object sender, EventArgs e)
    {
        List<ReportData> myReportData = new List<ReportData>();
        using (SqlConnection connection1 = new SqlConnection(str2))
        {
            //Query the Reports table to find the record associated with the selected report
            using (SqlCommand cmd = new SqlCommand("SELECT * from tblManagerReports WHERE ReportID =  " + cboFilterOption.SelectedValue + "", connection1))
            {
                connection1.Open();
                using (SqlDataReader DT1 = cmd.ExecuteReader())
                {
                    while (DT1.Read())
                    {
                        //Read the record into an "array", so you can find the SProc and View names
                        int MyRptID = Convert.ToInt32(DT1[0]);
                        string MyRptName = DT1[1].ToString();
                        string MyRptSproc = DT1[2].ToString();
                        string MySQLView = DT1[3].ToString();

                        //Run the Stored Procedure first
                        SqlConnection connection2 = new SqlConnection(str2);
                        SqlCommand cmd2 = new SqlCommand("" + MyRptSproc + "", connection2);

                        //Set up the parameters, if they exist
                        if (string.IsNullOrEmpty(this.txtStartDate.Text))
                        {
                        }
                        else
                        {
                            cmd2.Parameters.Add("@StDate", SqlDbType.Date).Value = txtStartDate.Text;
                        }

                        if (string.IsNullOrEmpty(this.txtEndDate.Text))
                        {
                        }
                        else
                        {
                            cmd2.Parameters.Add("@EnDate", SqlDbType.Date).Value = txtEndDate.Text;
                        }

                            if (MyRptSproc != "")
                            {
                                connection2.Open();
                                cmd2.ExecuteNonQuery();
                            }

                                try
                                {
                                    //Now open the View and bind it to the GridView
                                    string SelectView = "SELECT * FROM " + MySQLView + "";
                                    SqlConnection con = new SqlConnection(str2);
                                    SqlCommand SelectCmd = new SqlCommand(SelectView, con);
                                    SqlDataAdapter SelectAdapter = new SqlDataAdapter(SelectCmd);

                                    //Fill the dataset
                                    DataSet RunReport = new DataSet();
                                    SelectAdapter.Fill(RunReport);

                                    GridView_Reports.DataSource = RunReport;
                                    GridView_Reports.DataBind();
                                }
                                catch
                                {
                                    ScriptManager.RegisterStartupScript(btnSubmit, typeof(Button), "Report Menu", "alert('There is no View associated with this report.\\nPlease contact the developers and let them know of this issue.')", true);
                                    return;
                                }        

                    }

                }
            }

        }

问题:当代码命中行

  

cmd2.ExecuteNonQuery();

并且输入了一个开始和结束日期,它告诉我“过程或函数需要参数'@StDate',这是未提供的。”我已经逐步完成了代码,看到cmd2有2个参数,为什么函数看不到呢?

此外,这是导致snafu的特定存储过程(我有另外两个运行正常,但他们都没有尝试将参数传递给存储过程:

ALTER procedure [dbo].[usp_DailyProc]
    @StDate smalldatetime,
    @EnDate smalldatetime
AS

BEGIN

IF OBJECT_ID('Temp_DailyProduction') IS NOT NULL 
    drop table Temp_DailyProduction;

IF OBJECT_ID('Temp_AuditorDailyProduction') IS NOT NULL 
    drop table Temp_AuditorDailyProduction;    

SELECT 
    [Audit Date],
    Auditor,
    Count([Doc #]) AS [Claim Count],
    Count([Primary Error Code]) AS [Final Error],
    SUM(case when [Status]='removed' then 1 else 0 end) as Removed, 
    SOCNUM
INTO Temp_DailyProc
FROM PreClosed
WHERE (((Get_Next_Status)='Closed' Or (Get_Next_Status)='Panel' Or (Get_Next_Status)='HPanel'))
GROUP BY [Audit Date], Auditor, SOCNUM
HAVING ((([Audit Date]) Between @StDate And @EnDate));  

SELECT 
    TDP.[Audit Date], 
    TDP.Auditor, 
    EID.EMPLOYEE AS [Auditor Name], 
    TDP.[Claim Count], 
    TDP.[Final Error], 
    TDP.Removed, 
    TDP.[Removed]/TDP.[Final Error] AS [Error Removal Ratio], 
    TDP.SOCNUM
INTO Temp_AuditorDailyProc  
FROM Temp_DailyProc TDP 
LEFT JOIN PreLookup EID
    ON TDP.Auditor = EID.ID_Trim;

drop table Temp_DailyProduction;

END

1 个答案:

答案 0 :(得分:1)

认为您需要使用AddWithValue方法而不是Add方法。

  

AddWithValue替换了采用的SqlParameterCollection.Add方法   字符串和对象。带有一个字符串和一个的Add的重载   对象因为可能含糊不清而被弃用   SqlParameterCollection.Add带有String和a的重载   SqlDbType枚举值,其中传递带字符串的整数   可以解释为参数值或   对应的SqlDbType值。您可以随时使用AddWithValue   通过指定名称和值来添加参数。

如果有另外一想,您将字符串(Text)值作为Date参数传递。我认为你应该将其转换为日期类型。 e.g。

cmd2.Parameters.Add("@StDate", SqlDbType.Date).Value = DateTime.Parse(txtStartDate.Text);

更有效的方法是使用DateTime.TryParseExact