Thursday, 12 September 2013

custom paging using stored procedure sql server

/* create table */
create table tblCustomPaging (StudentName varchar(100),MobileNo varchar(20))

/* Insert into table */
insert into tblCustomPaging values('Amit','9876543210')
insert into tblCustomPaging values('Abhishek','8567575700')
insert into tblCustomPaging values('Sumit','7456456460')
insert into tblCustomPaging values('Bhuppy','9567575788')
insert into tblCustomPaging values('CP','8867575788')
insert into tblCustomPaging values('Sandeep','7767575788')
insert into tblCustomPaging values('OP','9967575788')
insert into tblCustomPaging values('Tarun','8867575700')
insert into tblCustomPaging values('Sachin','8867575700')
insert into tblCustomPaging values('Rajeev','9876543210')
insert into tblCustomPaging values('TK','9876541111')
insert into tblCustomPaging values('Mukesh','9876522222')

Method (1)

; With tmpResult As ( SELECT Row_Number() Over (ORDER BY StudentName) as RowNumber,
 StudentName, MobileNo from tblCustomPaging  as a )
 SELECT a.*, b.MaxRecords FROM tmpResult a CROSS JOIN
    (SELECT count(*) as MaxRecords FROM tmpResult) b
    WHERE RowNumber >  ((2 - 1) * 5)  and RowNumber <= (2 * 5) ORDER BY 1

Method (2)

/* create store procedure */

create proc USP_CustomPagin
@pageSize int=5,   
@pageNumber int=1 ,
@criteria varchar(max)='' 
as   
declare @endRowNumber int   
declare @startRowNumber int   
set @endRowNumber = @pageSize * @pageNumber   
set @startRowNumber  = (@endRowNumber - @pageSize)+ 1   
   
exec('select top '+ @pageSize +' Sno,totalCount, StudentName, MobileNo from (   
        select (select count(*) from tblCustomPaging where 0=0 '+  @criteria + ') as totalCount,   
        row_number() over(order by StudentName)Sno, StudentName, MobileNo   
        FROM tblCustomPaging  where 0=0 '+  @criteria + ' )as d   
            where d.sno between '+ @startRowNumber +' and  '+ @endRowNumber )


/* Run below query like */
USP_CustomPagin
USP_CustomPagin 5,1
USP_CustomPagin 5,2
USP_CustomPagin 5,1,' and StudentName Like ''%a%'' '
USP_CustomPagin 5,1,' and StudentName Like ''%a%'' OR MobileNo like ''%88%'''

Wednesday, 11 September 2013

How can we plug an ASP.NET MVC into an existing ASP.NET application ?

We can combine ASP.NET MVC into an existing ASP.NET application by following the below procedure:

First of all, you have to add a reference to the following three assemblies to your existing ASP.NET application:

i) System.Web.Routing
ii) System.Web.Abstractions
iii) System.Web.Mvc

The ASP.NET MVC folder should be created after adding these assembly references.
Add the folder Controllers, Views, and Views | Shared to your existing ASP.NET application.
And then you have to do the necessary changes in web.config file.
For this you can refer to the below link:

http://www.packtpub.com/article/mixing-aspnet-webforms-and-aspnet-mvc

Saturday, 7 September 2013

asp.net mvc 3 - Converting DateTime format using razor

Tricky solution : 
 @Html.TextBoxFor(m => m.fromData, new { @Value = Model.fromData.ToString("d") })