0

Hi everyone i am try creat pageing for some entity i have function whith signature like this public static List<someClass> somePageingFunc(int curentPage,int pageSize,int out maxCount) i can't use stored procedur i am new in sql please tell me what I'm doing wrong

string comandStr = "DECLARE @MaxCount int SELECT @MaxCount= Count (*) FROM tbMediaItems"+
                                    " SELECT top(@NumOfRows) * FROM ( SELECT RowNum = ROW_NUMBER() OVER ("+
                                    "ORDER BY Artist), * FROM tbMediaItems) AS a WHERE RowNum > (@NumOfRows * (@CurrentPage - 1)) RETURN @MaxCount";
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = comandStr;
                cmd.CommandType = CommandType.Text;
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@NumOfRows", SqlDbType.Int).Value = pageSize;
                cmd.Parameters.Add("@CurrentPage", SqlDbType.Int).Value = curentPage;
                cmd.Parameters.Add("@Result", SqlDbType.Int).Direction = 

ParameterDirection.ReturnValue;
try
                {
                    //SqlDataReader reder = cmd.ExecuteReader();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        var ss = reader[0];
                        var bb = reader[1];
                    }
                    maxCount = Convert.ToInt32(cmd.Parameters["@Result"].Value);
                }
                catch (Exception ex)
                {

                }

I get Exeption Message = A RETURN statement with a return value cannot be used in this context. When i remove RETURN @MaxCount all work ok but i am not get MaxCount

HaikMnatsakanyan
  • 303
  • 2
  • 5
  • 13

2 Answers2

2

Update your code and use ParameterDirection.Output to get return value :

cmd.Parameters.Add("@Result", SqlDbType.Int).Direction = 
ParameterDirection.ReturnValue;

to

 cmd.Parameters.Add("@Result", SqlDbType.Int).Direction  = 
 ParameterDirection.Output;

Edit 1:

you can create store-procedure as follows:

CREATE PROC [dbo].[procGetMaxCount]
@NumOfRows int,
@CurrentPage int,
@Result int OUTPUT  
AS    
BEGIN   
    SELECT @Result= Count (*) FROM tbMediaItems
                      SELECT top(@NumOfRows) * FROM ( SELECT RowNum = ROW_NUMBER() OVER (ORDER BY Artist), * FROM tbMediaItems) AS a WHERE RowNum > (@NumOfRows * (@CurrentPage - 1))                                       
END

and update your .cs file as :

 string comandStr="procGetMaxCount";

and cmd.CommandType = CommandType.StoredProcedure;

Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
1

RETURN is used within stored procedures to exit and indicate a status at the same time. You CANNOT use it simply within a query batch.

For your use case, I would probably do one of two things:

  1. Use two queries in a batch. You can use reader.NextResult to move between multiple result sets in a batch.
  2. Use two independent SqlCommands

A little more complicated, but you can also add an additional column to the result set, e.g.

SELECT top(@NumOfRows) B.*, A.TotalRows
FROM (SELECT TotalRows = COUNT(*) FROM tbMediaItems) A
CROSS JOIN ( SELECT RowNum = ROW_NUMBER() OVER (ORDER BY Artist) ...

Although the COUNT(*) in here or in your original could be replaced with this type of query on the sys.partitions table if your table is very large. It is mostly accurate except for very highly transactional tables.

Community
  • 1
  • 1
RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262