Friday 26 June 2015

Install//UnInstall Windows Service bat file


--------------------------------------
Install Windows Service bat file
--------------------------------------
//Notes put his bat file in same folder and run as a admin
CD\
CD C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319


InstallUtil.exe "%~dp0WindowsService.exe"

pause

--------------------------------------
UnInstall Windows Service bat file
--------------------------------------
//Notes put his bat file in same folder and run as a admin
CD\
CD C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319

InstallUtil.exe /u "%~dp0WindowsService.exe"

pause


How To filter distinct Values from a Datatable in C#

   DataView view = new DataView(table);
   DataTable distinctValues = view.ToTable(true, "id");

Tuesday 23 June 2015

SP to Generate Random Password in SQL (with upper,lower case characters,digits ,punctuation characters)


GO
/****** Object:  StoredProcedure [dbo].[USP_GenerateRandomPassword]    Script Date: 6/23/2015 12:52:26 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Gokuldas.Palapatta
-- Create date: 19th feb 20114
--/* Description: Should contain both upper and lower case characters
--(e.g., a-z, A-Z), digits and punctuation characters as well as
-- letters e.g., 0-9, !@#$%^&*()_+|~-=\`{}[]:";'<>?,./) and should be at least
--eight (alphanumeric with one special and one upper case) characters long */
--declare @x varchar(10)
--execute USP_GenerateRandomPassword @Password=@x output
--select @x
-- =============================================
CREATE  PROCEDURE [dbo].[USP_GenerateRandomPassword] (@Password varchar(10)='' OUTPUT)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @vRandomNum_Var varchar(10)
DECLARE @SPL VARCHAR(2)
SET @vRandomNum_Var = ''

Declare  @TempTable TABLE(
                         ID int,
                         Charachter char(20))
INSERT INTO @TempTable (ID, Charachter)  SELECT 35, '#'
INSERT INTO @TempTable (ID, Charachter)  SELECT 64, '@'
INSERT INTO @TempTable (ID, Charachter)  SELECT 36, '$'
INSERT INTO @TempTable (ID, Charachter)  SELECT 37, '%'
INSERT INTO @TempTable (ID, Charachter)  SELECT 94, '^'
INSERT INTO @TempTable (ID, Charachter)  SELECT 38, '&'
INSERT INTO @TempTable (ID, Charachter)  SELECT 42, '*'
INSERT INTO @TempTable (ID, Charachter)  SELECT 95, '_'
INSERT INTO @TempTable (ID, Charachter)  SELECT 45, '-'

  SET @SPL = (SELECT top 1 Charachter FROM @TempTable
ORDER BY NEWID())



SET @vRandomNum_Var = (select top 1 cast((Abs(Checksum(NewId()))%10) as varchar(1)) +
       char(ascii('a')+(Abs(Checksum(NewId()))%25)) +
       char(ascii('A')+(Abs(Checksum(NewId()))%25)) +
  @SPL +
       left(newid(),4)
FROM @TempTable)
SET @vRandomNum_Var = REPLACE(@vRandomNum_Var,' ','')
    SELECT @Password = @vRandomNum_Var

END




Tabular function to split by delimiter in SQL


GO
/****** Object:  UserDefinedFunction [dbo].[FN_SplitStringByDelimiter]    Script Date: 6/23/2015 12:48:47 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--select * from dbo.[FN_SplitStringByDelimiter] ('goku,goku1,goku2',',')

CREATE FUNCTION [dbo].[FN_SplitStringByDelimiter]

(

@List VARCHAR(8000),   -- String of values

@SplitOn NVARCHAR(5)    -- delimiter value

)

RETURNS @RtnValue TABLE

(

Id INT IDENTITY(1,1),

Component VARCHAR(50)

)

AS

BEGIN

WHILE (CHARINDEX(@SplitOn,@List)>0)

BEGIN


IF LEN(LTRIM(RTRIM(SUBSTRING(@List,1,CHARINDEX(@SplitOn,@List)-1)))) > 0

INSERT INTO @RtnValue (Component)

SELECT Component = LTRIM(RTRIM(SUBSTRING(@List,1,CHARINDEX(@SplitOn,@List)-1)))

SELECT @List = SUBSTRING(@List, CHARINDEX(@SplitOn,@List)+LEN(@SplitOn),LEN(@list))

END


IF LEN(LTRIM(RTRIM(@List))) > 0
INSERT INTO @RtnValue (Component)
SELECT Component = LTRIM(RTRIM(@List))

-- Delete StringEmpty and NULL values from List
DELETE FROM @RtnValue WHERE Component is null OR Component=''

RETURN

END

Friday 19 June 2015

Sent mail..

-------------------------------------------------------------------------------------------------------------------------
Mail helper class
-------------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Web;


    public class MailHelper
    {
        /// <summary>
        /// This helper class sends an email message using the System.Net.Mail namespace
        /// </summary>
        /// <param name="fromEmail">Sender email address</param>
        /// <param name="toEmail">Recipient email address</param>
        /// <param name="bcc">Blind carbon copy email address</param>
        /// <param name="cc">Carbon copy email address</param>
        /// <param name="subject">Subject of the email message</param>
        /// <param name="body">Body of the email message</param>
        /// <param name="attachment">File to attach</param>

        #region Static Members

        public static void SendMailMessage(string toEmail, string fromEmail, string bcc, string cc, string subject, string body, List<string> attachmentFullPath)
        {
            //create the MailMessage object
            MailMessage mMailMessage = new MailMessage();

            //set the sender address of the mail message
            if (!string.IsNullOrEmpty(fromEmail))
            {
                mMailMessage.From = new MailAddress(fromEmail);
            }

            //set the recipient address of the mail message
            mMailMessage.To.Add(new MailAddress(toEmail));

            //set the blind carbon copy address
            if (!string.IsNullOrEmpty(bcc))
            {
                mMailMessage.Bcc.Add(new MailAddress(bcc));
            }

            //set the carbon copy address
            if (!string.IsNullOrEmpty(cc))
            {
                mMailMessage.CC.Add(new MailAddress(cc));
            }

            //set the subject of the mail message
            if (!string.IsNullOrEmpty(subject))
            {
                mMailMessage.Subject = "UBH Web Application Notification" ;
            }
            else
            {
                mMailMessage.Subject = subject;
            }
           
            //set the body of the mail message
            mMailMessage.Body = body;

            //set the format of the mail message body
            mMailMessage.IsBodyHtml = false;
         
            //set the priority
            mMailMessage.Priority = MailPriority.Normal;

            //add any attachments from the filesystem
            foreach (var attachmentPath in attachmentFullPath)
            {
                Attachment mailAttachment = new Attachment(attachmentPath);
                mMailMessage.Attachments.Add(mailAttachment);
            }
           
            //create the SmtpClient instance
            SmtpClient mSmtpClient = new SmtpClient();

            //send the mail message
            mSmtpClient.Send(mMailMessage);
        }

        /// <summary>
        /// Determines whether an email address is valid.
        /// </summary>
        /// <param name="emailAddress">The email address to validate.</param>
        /// <returns>
        /// <c>true</c> if the email address is valid; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValidEmailAddress(string emailAddress)
        {
            // An empty or null string is not valid
            if (String.IsNullOrEmpty(emailAddress))
            {
                return (false);
            }

            // Regular expression to match valid email address
            string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                                @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                                @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

            // Match the email address using a regular expression
            Regex re = new Regex(emailRegex);
            if (re.IsMatch(emailAddress))
                return (true);
            else
                return (false);
        }

        #endregion
    }





-------------------------------------------------------------------------------------------------------------------------
Code behind
-------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Data;

namespace MailSentCracker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void btnsent_Click(object sender, RoutedEventArgs e)
        {
            bool status = true;

            status = Sendmail();
            if (status == true)
            {
                MessageBox.Show("Sent Succesfully! Life ginga lala.. he he..");

            }
            else
            {
                MessageBox.Show("Some thing gone wrong or Fill the Required fileds (from,to message body)");
            }
        }

        public bool Sendmail()
        {
            bool status = false;
     
         
            try
            {

           
                {

                 
                    string messageBody = txtbody.Text.Length > 1 ? txtbody.Text.ToString() : "";
                    string From = txtfrom.Text.Length > 1 ? txtfrom.Text.ToString() : "";
                    string To = txtTO.Text.Length > 1 ? txtTO.Text.ToString() : "";
                    string cc = txtCC.Text.Length > 1 ? txtCC.Text.ToString() : "";
                    string bcc = txtbcc.Text.Length > 1 ? txtbcc.Text.ToString() : "";
                    string subject = txtsubject.Text.Length > 1 ? txtsubject.Text.ToString() : "";
                    if (messageBody.Length > 1 && From.Length > 1 && To.Length > 1)
                    {
                        MailHelper.SendMail(From, To, bcc, cc, subject, messageBody, "");
                        //MailHelper.SendMail(DefaultEmail, DT.Rows[0]["CanEmail"].ToString(), EmailCC1, DT.Rows[0]["LogUserEmail"].ToString(), "Offer Letter", messageBody, filename);
                        // MailHelper.SendMail(, txt, EmailCC1, DT.Rows[0]["LogUserEmail"].ToString(), "Offer Letter", messageBody);
                        status = true;
                    }
                    else
                    {
                        status = false;
                     

                    }
                }



            }
            catch (Exception ex)
            {
                status = false;

            }
            finally
            {
               
            }
            return status;
        }
    }
}

--------------------------------------------------------------------------------------------------------------
app config
--------------------------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<configuration>

  <system.net>
    <mailSettings>
      <smtp from="goku@gmail.com">
        <network host="smtpout.secureserver.net" port="45" userName="goku@gmail.com" password="dqdwdqwd"/>
      </smtp>
     
    </mailSettings>
  </system.net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>