Wednesday, 6 November 2013

LEFT JOIN in LINQ to Entities?

DefaultIfEmpty is used for left joins for EntityFramework 4+

var query = from u in context.Users
    from a in context.Addresses
          .Where(x => u.Primary2Address == x.AddressiD)
          .DefaultIfEmpty()
    from s in context.States
          .Where(x => a.Address2State == x.StateID)
          .DefaultIfEmpty()
    from c in context.Countries
          .Where(x => a.CountryID == x.CountryID)
          .DefaultIfEmpty()
select u.UserName;
 
 

Saturday, 26 October 2013

How to render a DateTime in a specific format in ASP.NET MVC 3?

If all you want to do is display the date with a specific format, just call:
@Model.LeadDate.ToString("dd-MMM-yyyy")

@Model.LeadDate.ToString("MM/dd/yy")
It will result in following format,

26-Sep-2013
10/26/13

Thursday, 24 October 2013

Email validation in mvc

[Required(ErrorMessage = "Email Address is required")]
[DisplayName("Email Address")]
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
ErrorMessage = "Email is is not valid.")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }

Tuesday, 22 October 2013

validating email address using regular expression on razor page/mvc

Alternatively in razor @@ is a normal @ symbol, it is working fine..

/^[a-zA-Z0-9_.-]+@@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{0,4}$/

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") })