Friday, August 23, 2013

Simply Query WHERE in Special scenario

Today I got to know the most effective way to query the result from WHERE condition in a special scenario.

For example:
If you want to get the result set from Employee table
 When Select All Department or When Select One Department (only two options are there)

 You have to consider only WHERE column = <some value>
 If select All no need to worry about WHERE column IN (<all values>) or else IF ELSE or SWITCH Conditions

 Just simple you can get the result via OR logic
 Remember only one statement or both statement has to be TRUE to return in OR gate.


WHERE DepartmentId in (@Dep)  OR @Dep in (0)
when select all no value passes. So if pass 0 the logic change as
WHERE DepartmentId in (0)  OR @Dep in (0) -> TRUE OR TRUE

Look how it simple. :)

Wednesday, August 21, 2013

Email sending via MvcMailer

In ASP.Net MVC 3/4, MvcMailer provides you with an ActionMailer style email sending

To Install in Nuget Package : PM> install-package MvcMailer

Follow the simple example: Leave Request Mail

1) Define a mail model for the Leave Request :

    public class LeaveRequestMailModel
    {

       //main attributes
        public string Subject { get; set; }
        public string ToMail { get; set; }
        public string FromMail { get; set; }
        public string Message { get; set; }
        public string ViewName { get; set; }

       //customized attributes
        public string CustomEmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string FromDate { get; set; }
        public string ToDate { get; set; }
        public string LeaveType { get; set; }       
        public int LeaveApprovalHistoryId { get; set; }
        public int LeaveTransactionId { get; set; }
    }

2) Define Sending mail Service :


As you see here, populate method will call you back with an already instantiated MvcMailMessage object so you can set its properties as required.
MvcMailMessage is an extension of MailMessage from the core .Net library, with new properties added so you can specify the ViewName, MasterName, LinkedResources etc.

3) Design View for the Leave Request Email Template:

Here you can see a section of the View. In there I have  used inline css. Else email display without any styles.

4) Call Sending mail service via the controller :


Define the parameters relevant to the Leave request mail as in the above.

5) Setup the web config :


6) Finally you can run and see the output as follow:




It is really cool.  Those who are interested on this also better try and see.

Refer : https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
            http://smsohan.com/blog/2012/10/02/mvcmailer-new-api/