过程或函数'Student'需要参数'@SortExpression',这是未提供的

时间:2011-06-27 09:35:04

标签: asp.net sql-server-2005 ado.net

我收到这个奇怪的错误我已经检查了sort的值它也不是null它仍然给我错误我的代码。

public static List<MyDb_student> GetAllUsers(String sort, CustomPaging paging)
        {
            con.Open();
            SqlCommand objParams = new SqlCommand();
            objParams.Parameters.Add("@SortExpression",SqlDbType.VarChar).Value = sort;
            objParams.Parameters.Add("@StartRowIndex",SqlDbType.Int).Value = paging.startRow;
            objParams.Parameters.Add("@MaximumRows",SqlDbType.Int).Value = paging.maxRow; 

            objParams.CommandType = System.Data.CommandType.StoredProcedure;
            objParams.CommandText = "clud_GetAllUsers";
            objParams.Connection = con;
            SqlDataAdapter objSda = new SqlDataAdapter(objParams.CommandText, con);
            DataSet objDS = new DataSet();
            objSda.Fill(objDS);
            //to inialize the list.
            List<MyDb_student> lstStudent = new List<MyDb_student>();
            //to itereate through the data row.
            foreach (DataRow  dr in objDS.Tables[0].Rows)
            {
                //passing each row to the constructor to dump the values in the constructor.
                MyDb_student objStu = new MyDb_student(dr);
                //adding the value to the list by passing the object.
                lstStudent.Add(objStu);
            }
            con.Close();
            return lstStudent;
        }

我的存储过程

ALTER PROCEDURE [dbo].[clud_GetAllUsers] 
    @SortExpression VARCHAR(100),
    @StartRowIndex INT,
    @MaximumRows INT   

AS
BEGIN
IF LEN(@sortExpression) = 0
    SET @sortExpression = 'UserID'

DECLARE @sql nvarchar(4000)

SET @sql = 'SELECT * FROM (
                            SELECT UserID,UserName,EmailID,MobileNO,ROW_NUMBER() OVER (ORDER BY ' + @SortExpression + ' desc) AS RowRank
                            FROM UserRegistration ) 
                        AS UserRegistrationWithRowNumbers
            WHERE RowRank > ' + CONVERT(nvarchar(10), @StartRowIndex) +
                ' AND RowRank <= (' + CONVERT(nvarchar(10), @StartRowIndex) + ' + '
                + CONVERT(nvarchar(10), @MaximumRows) + ')'
    EXEC sp_executesql @sql
END

我无法修复它。

2 个答案:

答案 0 :(得分:0)

而不是

IF LEN(@sortExpression) = 0
    SET @sortExpression = 'UserID'

尝试添加isnull以捕获null:

IF LEN(ISNULL(@sortExpression, '')) = 0
    SET @sortExpression = 'UserID'

答案 1 :(得分:0)

只需检查如下:

     if(@sortExpression IS NULL)
           SET @sortExpression = 'UserID'

希望这能解决问题..