Thursday, 10 November 2016

How to list all foreign keys in MS SQL database

How to list all foreign keys in MS SQL database


SELECT RC.CONSTRAINT_NAME FK_Name
, KF.TABLE_SCHEMA FK_Schema
, KF.TABLE_NAME FK_Table
, KF.COLUMN_NAME FK_Column
, RC.UNIQUE_CONSTRAINT_NAME PK_Name
, KP.TABLE_SCHEMA PK_Schema
, KP.TABLE_NAME PK_Table
, KP.COLUMN_NAME PK_Column
, RC.MATCH_OPTION MatchOption
, RC.UPDATE_RULE UpdateRule
, RC.DELETE_RULE DeleteRule
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KF ON RC.CONSTRAINT_NAME = KF.CONSTRAINT_NAME

JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KP ON RC.UNIQUE_CONSTRAINT_NAME = KP.CONSTRAINT_NAME

Friday, 4 November 2016

Thursday, 27 October 2016

How to close all active connections to MS SQL database


Query to close all active connections to MS SQL database:

use master
go
alter database PrimumII_LoadTest
set single_user with rollback immediate
go
alter database PrimumII_LoadTest
set multi_user
go

How to lock a table in MSSQL

Query lock a table in MSSQL:

BEGIN TRAN
SELECT 1 FROM <Table Name> WITH (TABLOCKX)
WAITFOR DELAY '00:11:00'
ROLLBACK TRAN  
GO 

Tuesday, 10 May 2016

Code to copy paste using javascript on clipboard

Working on Chrome, IE, Mozilla

 Script:

function copySelectionText(text) {
    //debugger;
    var copysuccess;
    try {
        if (window.clipboardData && clipboardData.setData) {
            clipboardData.setData('text', text);//For IE
        }
        else {//For others
            copysuccess = document.execCommand("copy");
        }
    } catch (e) {
        copysuccess = false;
    }
    return copysuccess;
}

function copyfieldvalue(e, field) {
    SelectText(field);
    var copysuccess = copySelectionText(field.innerText);
}

function SelectText(element) {
    var doc = document
        , text = element
        , range, selection
    ;
    if (doc.body.createTextRange) {
        range = document.body.createTextRange();
        range.moveToElementText(text);
        range.select();
    } else if (window.getSelection) {
        selection = window.getSelection();
        range = document.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
    }
}

Html Changes:

 <button data-bb-handler="ok" type="button" id="btnOtpCopy" data-dismiss="modal" onclick="copyfieldvalue(event,document.getElementById('otpValue'));" class="btn btn-primary">Copy</button>

Thursday, 19 November 2015

How to remove empty spaces from string using dotnet regex?





Use below method to remove empty spaces:


public static string RemoveEmptySpace(string inputString)
        {
            string ouputString = string.Empty;
            if (!string.IsNullOrEmpty(inputString))
            {
                ouputString = Regex.Replace(inputString, "\\s+", " ");
                ouputString = Regex.Replace(ouputString, "^\\s+|\\s+$", "");
            }
            return ouputString;

        }

How to ignore certificate validation when sending an email using SmtpClient in Dotnet


Issue:
If there is some Issue with Server certificate below error will occur,
The remote certificate is invalid according to the validation procedure


Solution:
1)      Use valid server certificates.
2)      Right code to ignore invalid certificate (refer below code lines).


Code lines:
1.      ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
2.      smtpClient.Send(mailMessage);

Add Callback handler (ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate) before sending email (before smtpClient.Send() method)


ValidateServerCertificate() definition:

private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            if (sslPolicyErrors == SslPolicyErrors.None)
            {
                return true;
            }
            //The server certificate is not valid. Continuing with sending email.;
            return true;
        }