Sending Email in ASP.NET MVC using Postal

Sending Email is a very easy thing in ASP.NET.

In ASP.NET MVC we can render our view as email template. There are bunch of third party packages.

So recently I got to know Postal package

More About Postal

So I created Quick sample using Postal.

You can find working sample here

Video On Youtube

Advertisements

Remote modal validation attribute in ASP.NET MVC

We can use validation attribute to validate forms. Using Jquery validation plugin we can do client side validation with the help of modal validation attribute in mvc.

I have a post on client side validation.

Client Side Form validation in ASP.NET MVC

So some times we need to connect with server to validate properties. So now it is painless. We have Remote  attribute to validate such properties behind the seen.

Let’s suppose we need to validate user name property. User name should be validated with server. It can not be duplicated. So we can Use Remote validation attribute.

To validate want to connect to the server. So we can provide controller action to Remote attribute to call action and validate property behind the seen.

We can decorate property as below


[Remote("CheckUserName","Home", ErrorMessage ="User name already exists" ) ]
public string UserName { get; set; }

Modal class

usermodal

In action we should return true or false. if false returned from action error message will be shown.

Action


public JsonResult CheckUserName(string username)
{
//logic for check user name with database

return Json(false, JsonRequestBehavior.AllowGet);
}

action

My sample view contains form for enter user data


@model RemoteAttributeTestMVC.Models.User
@{
ViewBag.Title = "Home Page";
}
<div class="row" style="margin-top:10px;">
@using (Html.BeginForm("CreateUser", "Home"))
{
<div class="form-group order-form-group">
@Html.LabelFor(m => m.UserName)
@Html.EditorFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
}
</div>

This will render following form

form

To allow client side validation we have to add jquery validation plugin . I have added reference in _Layout.cshtml  as below

jval

Then run the project and when we enter values in user name text box and press tab it sends a request to CheckUserName action with parameter of user name behind the seen.

if action return false validation message will be shown in the form.

error

Download source code

MERGE in Sql Server

From Sql server 2012 we can merge two tables. We can map data from one table to another table. If record exists can do a update and if not insert operation can be done.

Syntax


MERGE <target_table>
USING <table_source>
ON <Search>
WHEN MATCHED THEN UPDATE SET <set_clause>
WHEN NOT MATCHED THEN INSERT VALUES (<values_list>)

Create two tables and Insert some records


CREATE TABLE TargetTable([Id] INT, [Name] NVARCHAR(50))

CREATE TABLE SourceTable([Id] INT, [Name] NVARCHAR(50))

INSERT INTO TargetTable(Id,Name)
VALUES(1, 'John'), (2, 'Mezi'), (3, 'Tom')

INSERT INTO SourceTable(Id,Name)
VALUES(1, 'JohnNew'), (4, 'Peter'), (3, 'Tom-new')

Then do Merge


MERGE TargetTable AS T
USING SourceTable AS S
ON T.Id = S.Id
WHEN MATCHED THEN UPDATE SET T.Name = S.Name
WHEN NOT MATCHED THEN INSERT VALUES (S.Id, S.Name);

Now view TargetTable Data

merge

 

format currency in jquery

So when we are in web development we want show Currency value with some formatting. Like thousand separator, decimal separator and currency symbol.

I am using below Jquery function to format the currency values.


function formatCurrency(value, decimals, decpoint, separator, symbol) {
decimals = (typeof decimals == 'undefined' ? 2 : decimals);
decpoint = (typeof decpoint == 'undefined' ? '.' : decpoint);
separator = (typeof separator == 'undefined' ? ',' : separator);
symbol = (typeof symbol == 'undefined' ? '$' : symbol);

var parts = value.toFixed(decimals).toString().split(decpoint);
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, separator);

return (symbol + parts.join(decpoint)).toLocaleString();
}

We can format currency value as we required. If we just pass the currency value it will give us value with default formatting.

formatCurrency(5873839) will give us ” $5,873,839.00″

unable to open bcp host data-file

So when I am using bcp utility in sql server I got this error. All syntax are fine. I was wandering.

Issue was sql agent’s user not having permission to access host file location.

So first I wanted to know sql agent’s current user.

I used following query to sql server service acounts.


SELECT DSS.servicename,
DSS.startup_type_desc,
DSS.status_desc,
DSS.last_startup_time,
DSS.service_account,
DSS.is_clustered,
DSS.cluster_nodename,
DSS.filename,
DSS.startup_type,
DSS.status,
DSS.process_id
FROM sys.dm_server_services AS DSS;

Then grant permission to sql agent user. Everything works fine.

Login failed for user IIS APPPOOL\DefaultAppPool

Again I got a Error.

Login failed for user iis APPPOOL\Default. I fixed this issue by running following sql script in sql server.


IF NOT EXISTS (SELECT name FROM sys.server_principals WHERE name = 'IIS APPPOOL\DefaultAppPool')
BEGIN
CREATE LOGIN [IIS APPPOOL\DefaultAppPool]
FROM WINDOWS WITH DEFAULT_DATABASE=[MISDB],
DEFAULT_LANGUAGE=[us_english]
END
GO
CREATE USER [WebDatabaseUser]
FOR LOGIN [IIS APPPOOL\DefaultAppPool]
GO
EXEC sp_addrolemember 'db_owner', 'WebDatabaseUser'
GO

Set your required database as default database.