Thursday, May 22, 2014

Javascript to detect whether user is viewing the page

Today I was writing a javascript to check whether there is a user activity on the page or not. Thought of putting the same to blog so that some on else can use of it.

Here is the code for it......

<script src="~/Scripts/jquery-2.1.1.min.js"></script>
<script>
    //Assinging the setInterval for function time()  for every 1 seconds, used to show time
    var myVar = setInterval(function () { time() }, 1000);
    //assinging the setInterval for function reset() for every 5 seconds, used to reload page
    var myVarReset = setInterval(function () { reset() }, 5000);
   
    //Function to show the time and will be update in nevery second
    function time() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        $('#time').text(t);
    }

    //Function to reload the page which is set to call every 5 seconds using  setInterval
    function reset()
    {
        location.reload();
        alert("Reset");
    }
    //Handling the onmousemove event of the form
    window.onmousemove=function()
    {
        //clering the setInterval for function reset()
        clearInterval(myVarReset);
        //Againg assinging the setInterval for function reset()  for every 5 seconds
        myVarReset = setInterval(function () { reset() }, 5000);
    }
</script>
<text id="time"></text>

So how does this code works?

First of all we need to understand what does setInterval and clearInterval does?

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).
The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.
The ID value returned by setInterval() is used as the parameter for the clearInterval() method.
Tip: 1000 ms = 1 second.

Tip: To execute a function only once, after a specified number of milliseconds, use the setTimeout() method.

Let’s look into the  ode in detail……………….

    var myVar = setInterval(function () { time() }, 1000);
    var myVarReset = setInterval(function () { reset() }, 5000);
In this part we are using setInterval to set two functions “time” and “reset”, one for showing the current time on the page and the other for reloading the page if there is no user activity for 5 seconds. The output of these factions are assigned to variable myVar and myVarReset respectively.

    function time() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        $('#time').text(t);
    }
This part is responsible to show the current time on the page , which will be called every 1 second  as defined by the above setInterval

    function reset()
    {
        location.reload();
        alert("Reset");
    }
This function just reloads the page

    window.onmousemove=function()
    {
        clearInterval(myVarReset);
        myVarReset = setInterval(function () { reset() }, 5000);
    }

Here we handle the onmousemove on the page and resets the setinterval for reset() function using the clearInterval by passing variable myVarReset.

Once it is reset , the function reset() is again set to call eve very 5 seconds using setInterval.


Friday, May 16, 2014

PowerShell script for listing folders in a location with their last Accessed date and size

Today I was asked to write a power shell function for listing the folders in a location with their last Accessed date and size. Thought of sharing the same so that someone else can make use of it.

I just wrote this as function so that it can reused easily. This function give the result in excel


Function Get-FolderSize
{
 Param(
         [Parameter(Mandatory=$true)]
         [string]$loactaion
        )#end param

# Adding to Excell Sheet

$strComputer = “.”
$Excel = New-Object -Com Excel.Application
$Excel.visible = $True

$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = “Folder”
$Sheet.Cells.Item(1,2) = “Last Accessed”
$Sheet.Cells.Item(1,3) = “size”

$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
$intRow = 2


$colItems = Get-ChildItem  $loactaion -recurse | Where-Object {$_.PSIsContainer  }

foreach ($i in $colItems)
{
     $kk = Get-ChildItem  $i.FullName -recurse | Measure-Object -property length -sum
     $Sheet.Cells.Item($intRow,1) = $i.FullName
     $Sheet.Cells.Item($intRow,2) = $i.LastAccessTime
     $Sheet.Cells.Item($intRow,3) = "{0:N2}" -f ($kk.Sum / 1MB) + "MB"
     $intRow = $intRow + 1
}

$WorkBook.EntireColumn.AutoFit()
}


Usage

Get-FolderSize "D:\yourfolder" 

Wednesday, May 14, 2014

Add support for the scroll wheel to the Microsoft Visual Basic for Applications 6 environment

The Classic VB IDE was actually written before the mouse wheel was invented, believe it or not. There have been numerous tools and assorted hacks offered over the years to remedy the native non-support VB provides, but I've found the below more interesting.

Please follow the steps in below page to configure scrolling in VB6 IDE.
 

Monday, December 9, 2013

C# Code to read Active directory attributes for a user

Juts add the below code to a Windows/Web solution and assign the results to a label in you form

using System.DirectoryServices;
using System.Collections;

DirectoryEntry dir = new DirectoryEntry();
dir.Path = "LDAP://YourActiveDirServername ";
DirectorySearcher sea = new DirectorySearcher(dir);
sea.Filter = "(sAMAccountName=user)";
SearchResult seares = sea.FindOne();
StringBuilder str = new StringBuilder();
System.DirectoryServices.ResultPropertyCollection prop = seares.Properties;
ICollection coll = prop.PropertyNames;
IEnumerator enu = coll.GetEnumerator();
while (enu.MoveNext())
{
str.Append(enu.Current + " = " + seares.Properties[enu.Current.ToString()][0] + "\n");
}

label1.Text = str.ToString();

Wednesday, October 23, 2013

How to bind enum to a dropdown/ combobox in c#?

The below code will help you to bind enum to combobox .

        enum Mode
        {
            Cash,
            credit
        }
        private void Form3_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = Enum.GetNames(typeof(Mode));
        }

Thursday, August 15, 2013

Storing Multiple values in a SQL column using bit(&) operator.

There may be situation where we want to store multiple values against a customer, user etc.  Assume for a customer we need to store whether he  owns a House, Car, Bike, Flat, Laptop…….etc.
In normal scenario, what we do is to create separate column for these in the table and save them as Boolean values as below.


 By using the & bit operator in SQL we could eliminate create multiple columns for storing these type of values. Let us see first how we can store this data by using only a single column.

I believe all we know about binary representation like
Decimal  Binary equivalent
2               000010
4               000100
8               001000
16             010000
32             100000



The logic here to first represent the below values for each
Has House 2
Has Car  4
Has Bike 8
Has Flat 16
Has Laptop 32
(You may extent this to store n number using respective decimal/binary representation like 64,128,256……)
So if  we need to store data for a customer who has house and a flat, what we do is add the numbers against “Has House” and ”Has Flat” which will give 18 (2(Has House)+16(Has Flat)=18).  Store these values against the customer in a single column as shown below.




We could compare whether the customer has a house or a flat using the query below,
 
SELECT 'Has House'  FROM dbo.New_Cuatomer_Data WHERE Owns & 2 = 2
SELECT 'Has Car'    FROM dbo.New_Cuatomer_Data WHERE Owns & 4 = 4
SELECT 'Has Bike'   FROM dbo.New_Cuatomer_Data WHERE Owns & 8 = 8
SELECT 'Has Flat'   FROM dbo.New_Cuatomer_Data WHERE Owns & 16 = 16 
SELECT 'Has Laptop' FROM dbo.New_Cuatomer_Data WHERE Owns & 32 = 32 


How does it work?
The logic works here is that, the & operator checks whether the bit positions specified after the & operator in the query is on or not(1 or 0) .For example in the case of 18, it will binary represented as below.

Hence for
SELECT Owns & 2  FROM dbo.New_Cuatomer_Data
 It will return 2 if 2nd position is on (i.e. if 1)

Binary Bit Positions
32
16
8
4
2
1
Decimal equivalent 18  
0
1
0
0
1
0