Sunday 30 June 2013

Generate list of months in sqlserver

DECLARE @year INT
SET @year = 2013

;WITH mths AS(
    SELECT 1 AS mth, DATENAME(MONTH, CAST(@year*100+1 AS VARCHAR) + '01')  AS monthname
    UNION ALL
    SELECT mth+1, DATENAME(MONTH, CAST(@year*100+(mth+1) AS VARCHAR) + '01') FROM mths WHERE mth < 12
)
SELECT * FROM mths 

Monday 10 June 2013

Send E-mail by javascript/ jquery using webservice

<style>
        /*------------------POPUPS------------------------*/#fade
        {
            display: none;
            background: #fff;
            position: fixed;
            left: 0;
            top: 0;
            z-index: 10;
            width: 100%;
            height: 100%;
            opacity: .50;
            z-index: 9999;
        }
        .popup_block
        {
            display: none;
            background: #fff;
            padding: 10px;
            border: 5px solid #ddd;
            float: left;
            font-size: 1.2em;
            position: fixed;
            top: 50%;
            left: 50%;
            z-index: 99999;
            -webkit-box-shadow: 0px 0px 20px #000;
            -moz-box-shadow: 0px 0px 20px #000;
            box-shadow: 0px 0px 20px #000;
            -webkit-border-radius: 10px;
            -moz-border-radius: 10px;
            border-radius: 10px;
        }
        img.btn_close
        {
            float: right;
            margin: -40px -40px 0 0;
        }
        .popup p
        {
            padding: 5px 10px;
            margin: 5px 0;
        }
        /*--Making IE6 Understand Fixed Positioning--*/*html #fade
        {
            position: absolute;
        }
        *html .popup_block
        {
            position: absolute;
        }
        #fade
        {
            z-index: 0;
        }
    </style>
 <script language="javascript" type="text/javascript" src="jquery_bundle/jquery-1.9.1.js"></script>
    <script language="javascript" type="text/javascript">

        jQuery.fn.EncHTML = function() {
            return String(this.html())
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
        };

        function sendMail() {
            popUp('80', 'divTaskPopup');
            var message = "";
            message = "<div> Hi i am -----------------------.";
            message += "<table align='left' border='0' cellpadding='0' cellspacing='3' width='100%'> ";
            message += " <tr> ";
            message += "<td width='250' height='34' align='left' valign='top'>";
            message += " Reason";
            message += " </td>";
            message += " <td width='313' align='left' valign='top'>";
            message += " XXXXXXX";
            message += "</td>";
            message += "</tr>";
            message += "<tr>";
            message += "<td width='250' height='34' align='left' valign='top'>";
            message += " Would you like to sing up in future ?";
            message += "</td>";
            message += "<td width='313' align='left' valign='top'>";
            message += " XXXXXXX";
            message += "</td>";
            message += "</tr>";
            message += "<tr>";
            message += "<td width='250' height='120px' align='left' valign='top'>";
            message += " Description";
            message += "</td>";
            message += "<td width='313' align='left' valign='top' colspan='3'>";
            message += " XXXXXXX XX X X X XXXX X X X X XX X X X X X X X X X XX X X";
            message += "</td>";
            message += "</tr>";
            message += "</table>";
            message += "</div>";
            //Encode HTML
            var message = $(message).EncHTML();
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "webservice/remote_service.asmx/sendMailAnsService",
                data: "{'message':'" + message + "'}",
                dataType: "json",
                async: true,
                success: function(data) {
                    popUpClose();
                },
                error: function(result) {
                    alert("Error");
                }
            });

        }
     
        function popUp(width, popUpID) {
            var popID = popUpID;
            var popWidth = width;
            //Fade in the Popup and add close button
            $('#' + popID).fadeIn().css({ 'width': Number(popWidth) }).prepend('<a href="javascript:void(0)" class="close"><img src="../images/close_pop.png" class="btn_close" title="Close Window" alt="Close" onclick="popUpClose();" /></a>');
            //Define margin for center alignment (vertical + horizontal)
            var popMargTop = ($('#' + popID).height() + 80) / 2;
            var popMargLeft = ($('#' + popID).width() + 80) / 2;
            //Apply Margin to Popup
            $('#' + popID).css({
                'margin-top': -popMargTop,
                'margin-left': -popMargLeft
            });
            //Fade in Background
            $('body').append('<div id="fade"></div>');
            //Add the fade layer to bottom of the body tag.
            $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn();
            //Fade in the fade layer
            return false;
        }
        function popUpClose() {
            $('#fade , .popup_block').fadeOut(function() {
                $('#fade, a.close').remove();
                $('#headerMessage').fadeOut();
            });
        }
   
    </script>
 <form id="form1" runat="server">
    <div>
        <input type="button" value="send" onclick="sendMail();" />
    </div>
    <div id="divTaskPopup" class="popup_block">
        <div align="center">
            <img src="http://sierrafire.cr.usgs.gov/images/loading.gif" style="height: 50px;
                width: 50px" />
        </div>
    </div>
 </form>
------remote_service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Web.Script.Serialization;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class remote_service : System.Web.Services.WebService
{
    public remote_service()
    {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public IList<string> sendMailAnsService(string message)
    {
        IList<string> result = new List<string>();
        //Decode  html
        message = Server.HtmlDecode(message);
        string strSenderMailId = "xxxxxx@xxxx.com";
        string strSenderMailPassword = "xxxxxxxxxx";
        string strMailFrom = "xxxxxxxxxx";
        string strSmtpClient = "smtp.gmail.com";
        int strSmtpPort = 587;
        int r = sendMail.sendMailHandler(strSenderMailId, strSenderMailPassword, strMailFrom, strSmtpClient, strSmtpPort, "xxxxxxxxx@gmail.com", message, "testMail");
        result.Add(r.ToString());
        return result;
    }
}
---------sendMailHandler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class sendMail
{
public sendMail()
{
}
    public static int sendMailHandler(string strSenderMailId, string strSenderMailPassword, string strMailFrom, string strSmtpClient, int strSmtpPort, string sendTo, string strMessage, string strSubject)
    {
        try
        {
            string emailID = strSenderMailId;
            string password = strSenderMailPassword;
            System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
            System.Net.NetworkCredential cred = new System.Net.NetworkCredential(emailID, password);
            myMail.To.Add(sendTo);
            myMail.Subject = strSubject;
            myMail.From = new System.Net.Mail.MailAddress(strMailFrom);
            myMail.IsBodyHtml = true;
            myMail.Body = strMessage;
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(strSmtpClient);
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = cred;
            smtp.EnableSsl = true;
            smtp.Port = strSmtpPort;
            smtp.Send(myMail);
            return 1;
        }
        catch
        {
            return 0;
        }
    }

}

Friday 7 June 2013

Whats wrong with IsNumeric() Or Alternative of isnumeric

SQL's ISNUMERIC() function has a problem. It can falsely interpret non-numeric letters and symbols (such as D, E ,',' ), and even tabs (CHAR(9)) as numeric.like 12,90

CREATE FUNCTION dbo.isReallyNumeric
(
    @num VARCHAR(64)
)
RETURNS BIT
BEGIN
    IF LEFT(@num, 1) = '-'
        SET @num = SUBSTRING(@num, 2, LEN(@num))

    DECLARE @pos TINYINT

    SET @pos = 1 + LEN(@num) - CHARINDEX('.', REVERSE(@num))

    RETURN CASE
    WHEN PATINDEX('%[^0-9.-]%', @num) = 0
        AND @num NOT IN ('.', '-', '+', '^')
        AND LEN(@num)>0
        AND @num NOT LIKE '%-%'
        AND
        (
            ((@pos = LEN(@num)+1)
            OR @pos = CHARINDEX('.', @num))
        )
    THEN
        1
    ELSE
    0
    END
END
go
----------------------------------
CREATE FUNCTION dbo.isReallyInteger
(
    @num VARCHAR(64)
)
RETURNS BIT
BEGIN
    IF LEFT(@num, 1) = '-'
        SET @num = SUBSTRING(@num, 2, LEN(@num))

    RETURN CASE
    WHEN PATINDEX('%[^0-9-]%', @num) = 0
        AND CHARINDEX('-', @num) <= 1
        AND @num NOT IN ('.', '-', '+', '^')
        AND LEN(@num)>0
        AND @num NOT LIKE '%-%'
    THEN
        1
    ELSE
        0
    END
END
GO

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...