Friday, August 31, 2018

How to delete duplicate records from table in SQL Server. – SQL Interview Question

SQL सर्वर में Table से डुप्लिकेट रिकॉर्ड कैसे हटाएं। - SQL INTERVIEW QUESTION

Dear Reader,
Please click below link to see the solution of this question.

Click Here for Part - 1
Click Here for Part - 2

This is the script for your practice:

यह आपके अभ्यास के लिए Script है:
-- Query For Interview Question-3. How to delete duplicate records of a table.
create table TestQ3_1 -- Having Identity Column
(
ID int identity(1,1),
Ename nvarchar(200),
EAddress nvarchar(200)
)
Go
Insert into TestQ3_1 (Ename,EAddress)
Values('Nm1','Add1'),('Nm1','Add1'),('Nm2','Add2'),('Nm2','Add2'),('Nm3','Add3'),('Nm3','Add3')
Go

create table TestQ3_2 -- Have id column but not identity
(
ID int,
Ename nvarchar(200),
EAddress nvarchar(200)
)
Go
Insert into TestQ3_2 (ID,Ename,EAddress)
Values(1,'Nm1','Add1'),(1,'Nm1','Add1'),(2,'Nm2','Add2'),(2,'Nm2','Add2'),
(3,'Nm3','Add3'),(3,'Nm3','Add3')
Go
create table TestQ3_3 -- table with no ID column
(
Ename nvarchar(200),
EAddress nvarchar(200)
)
Go
Insert into TestQ3_3 (Ename,EAddress)
Values('Nm1','Add1'),('Nm1','Add1'),('Nm2','Add2'),('Nm2','Add2'),('Nm3','Add3'),('Nm3','Add3')
Go


Thanks.

Rudra Pratap Singh
Software Engineer.
Singhrudrapratap29@gmail.com

Monday, August 6, 2018

JS Validations for Input Field Type ‘Number’


Dear Readers,
Sometimes we have need of validation in number type input field in web pages. Main types of validations are :

1)      Drag and drop should be disabled
2)      Input should be only number whether on paste or on input.
3)      Should allow only one dot in the input number if input is decimal

Here, we will take input type=”number” control of HTML. You can use type=”text” as well. Basic benefit of this control is, it provides basic validations automatically. For more details about number type input, CLICKHERE.

Step 1: Create a JS file in your editor, Notepad, Notepad++, Visual Studio Code etc.

Step 2: Write the following code:
Following code will disable the Drag and Drop to the control-
$(':input[type="number"]').attr("ondrop", "return false;");
$(':input[type="number"]').attr("ondrag", "return false;");
Following code will validate the value on keypress of the keyboard.
$(':input[type="number"]').keypress(function (event) {
    // allow only one dot (.) in numeric field (For decimal values)
    // It will also restrict 'e' + - symbols for numeric text boxes because these symbols can be entered by default in input type ‘number’
    if(event.which != 8 && ((event.which != 46 || (event.which == 46 && $(this).val() == '')) ||
            $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)){
        return false;
    }
});

Step 3: Write the following code for validation on paste of the values
$(':input[type="number"]').on('paste', function () {
   
    var ctrlval = $(this).val();
    var $this = $(this);
    setTimeout(function () {
        if (!$.isNumeric($this.val())) {
            $this.val($this.val().replace(/[^0-9]/g, ''));
            $this.val(ctrlval);
        }
    }, 4);
});

Step 4: Save the file with .JS extension and drag and drop link of this file in your webpage.
For example: <script src="~/JS/CommonJS.js"></script>

I hope it will help. Mail me for any more query. Suggestions/feedbacks are most welcome.

CLICK HERE to subscribe my youtube channel.
Happy Coding.

Rudra Pratap Singh
Software Engineer

Saturday, August 4, 2018

Display Remaining Session Time and alert before Expire

Hello Readers,
Sometimes we have such a need that we have to show remaining time of session timeout and give
alert to the user before a particular time. So, we’ll see the code for the same requirement.
I am using ASP.NET MVC framework and Jquery as scripting language.
Step: 1 Add following code in web.config file:
<system.web>
<sessionState timeout="20"></sessionState>
</system.web>
Note: Give timeout value as per required session out time.
Step: 2 Add following code in MVC View at top. This is the code to fetch session timeout details.
@{
var conf = System.Web.Configuration.WebConfigurationManager.
OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.
ApplicationVirtualPath);
var section = (System.Web.Configuration.SessionStateSection)conf.
GetSection("system.web/sessionState");
       string timeout = section.Timeout.TotalMinutes.ToString();
}
Step: 3 Add a div to the view, to which we will make alert dialog for user.
Here we’ll give alert before 5 minutes.
<div id="sessionDialog" style="display: none">
Your session is about to expire in 5 minutes. Press Resume to keep alive.
<input type="button" id="btnResume" value="Resume"
onclick="ResumeSession(@timeout);" />
<input type="button" id="btnContinue" value="Continue"
onclick="ContinueSession();" />
</div>
Step: 4 Add a <p> tag in which we’ll show remaining timings of the session.
<p id="Sess"></p>
Step: 5 Add code in <script> tag
<script>
var Rtm = 0;
       var isContinue = false;
       function OpenInputDialog(x) {
           clearInterval(x);
           var fL = 0;
           $("#sessionDialog").dialog({
               autoOpen: false,
               modal: true,
               title: "Session Warning!",
               dialogClass: 'dialog1',
               closeOnEscape: true,
               close: function () {
                   $(this).dialog("close");
               },
               show: { effect: "clip", duration: 300 }
           });
           
           $("#sessionDialog").dialog("open");
           
       };
       function fn_SessionCount(tt)
       {
           if(!go)
               return;
           var t= 0;
           t= tt-1;
           var time = 0;
           time = 60;
var x = setInterval(function () {
  document.getElementById("Sess").innerHTML = "";
  document.getElementById("Sess").innerHTML = t+" Minutes "
+ time+" Seconds";
               time -= 1;
               if(time == 0)
               {
                   t -= 1;
                   time = 60;
               }
               if (t < 2) {
                   Rtm = t+1;
                   if(isContinue == false)
                   {
                       OpenInputDialog(x);
                   }
               }
               if(t == 0 && time == 1)
               {
                   clearInterval(x);
                   location.href = '../Login/Logout';
               }
           }, 1000);
       }
       function ResumeSession(tm)
       {
           var ss =0;
           debugger
           $.ajax({
               url: '../Login/SessionTimeout',
               data:{time:tm+""},
               dataType: "json",
               type: "GET",
               async:false,
               success: function (data) {
                   $("#sessionDialog").dialog("close");
                   ss=1;
               },
               error: function () {
                   alert(" An error occurred.");
               }
           });
           if (ss==1) {
               fn_SessionCount(@timeout);
       }
   }
   function ContinueSession()
   {
       go = true;
       isContinue = true;
       $("#sessionDialog").dialog("close");
       fn_SessionCount(Rtm);
   }
</script>
Step: 6 Add style for dialog box.
<style>
.dialog1 {
       background-color:lightblue;
       /* Give your desired design for dialog box */
    }
</style>
Step: 7 Call the function on page load.
$(document).ready(function () {     
     go = true;
     fn_SessionCount(@timeout);  
});
Save & build the code and run.
I hope it will help.
Subscribe my youtube channel HERE for programming related tutorials.
Feel free to mail me - Singhrudrapratap29@gmail.com
Happy coding.
Rudra Pratap Singh
Software Engineer

How to Get Organic Views on Your Channel/Content

 Hi Friends, This post will be very short and specific in the information.  If you are a content creator and want some help in getting organ...