Click here to Skip to main content
15,900,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have following stored procedure in database which is used for paging purpose for a gridview. it inserts records in a temporary table, sets rowcount and just returns specific rows.


procedure [dbo].[GetResults]
@pIndex varchar(50),
@startRowIndex int,
@maximumRows int 
AS
DECLARE @TempItems TABLE
(
ID int IDENTITY,
GMKey int,
Details nChar(800)
)
DECLARE @maxRow int

SET CONCAT_NULL_YIELDS_NULL OFF
SET ANSI_WARNINGS OFF

SET @maxRow = (@startRowIndex + @maximumRows) - 1 
SET ROWCOUNT @maxRow

INSERT INTO @TempItems (GMKey,TABLENAME,Details)
Select PRIMARYINDEX as GMKey,  LTRIM(RTRIM(cast(Facility_Name as nChar(800)))) as Details 
from SEARCHINFO where Facility_Name LIKE @pIndex + '%' order by PRIMARYINDEX DESC

SET ROWCOUNT @maximumRows
SELECT GMKey, TABLENAME, Details FROM @TempItems WHERE ID >= @startRowIndex

RETURN 


Our requirement is not to have stored procedure in the database, this functionality has to be implemented through asp.net code. I am not sure how to achieve this using a normal command object. As you can note there are statements as SET ROWCOUNT, create temporary table etc. Can I use the script of the stored procedure and give as commandtext to command object and execute it? or is there any other better option.

Thanks

Vijay
Posted
Updated 21-Sep-10 3:53am
v2
Comments
HimanshuJoshi 21-Sep-10 9:53am    
Edited for code-block
Hiren solanki 21-Sep-10 9:54am    
You should have only way to give command text to command object.

The Stored Procedure is nothing but a pre-complied SQL script which is stored as a named object and can be invoked by the name. Whatever SQL is written in the stored procedure, can be executed as an SQL srcipt (Given the fact that, parameter values are provided in the script)

So, you can of course build the appropirate SQL script in your application (With the parameter values) and execute as a command text (Not as a Stored Procedure) via the command object. But, you might need to adjust your Ado.net specific application codes so that, it can obtain value from the result set, instead of reading the output parameters if there is any.

By the way, use of SCOPE_IDENTITY() is a better option for getting the newly inserted ID of the table. You can Google it to learn how to use it.
 
Share this answer
 
My basic requirement is perform a search on a table e.g. "where facility_name like '<param />%' and if the query returns 100 records but my gridview is supposed to show second page i.e. it is supposed to show 6 - 10 from the returned dataset of 100 records. Can we not avoid the script above and instead just give a simple sql query which would return only 6 to 10 rows (i.e. only 5 rows). The script above does that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900