EDUDOTNET

Tuesday, October 22, 2013

How to find duplicate records in a table of SQL server database using a SQL query?

We have a Customer table in sql server database with multiple fields and also data. Lots of data in this table and also multiple duplicates records of customers in this table.

We want to find all duplicates records of customers and based on "CustCode" column. And also need to delete all duplicates records from Customer table. A customer should be only one record in this table.

We have following Customer table with data:

CustID
CustName
CustCode
Address
Country
PinCode
101
John
EDU005
#Ence Cot
Norway
0NY001
102
Golfy
EDU006
#30 Dam
Estonia
XPOUTR
103
Thom
EDU007
Barkin 80
IceLand
554211
104
John
EDU005
#Ence Cot
Norway
0NY001
105
Dolly
EDU009
#30 MarKha
Lithaunia
PQRSTK
106
Thom
EDU007
Barkin 80
IceLand
554211
107
John
EDU005
#Ence Cot
Norway
0NY001
108
Golfy
EDU006
#30 Dam
Estonia
XPOUTR
109
Thom
EDU007
Barkin 80
IceLand
554211

We can able to find out all duplicates records and delete duplicates records using following sql query:

WITH Cust AS (SELECT  *, rownumber = ROW_NUMBER()
            OVER(PARTITION BY CustCode ORDER BY CustCode)
            FROM Customer)

DELETE Cust WHERE rownumber > 1

After run this query we deleted all duplicated records from Customer. Remaining only one record of per customer. See below Customer table with data:

CustID
CustName
CustCode
Address
Country
PinCode
101
John
EDU005
#Ence Cot
Norway
0NY001
102
Golfy
EDU006
#30 Dam
Estonia
XPOUTR
103
Thom
EDU007
Barkin 80
IceLand
554211
105
Dolly
EDU009
#30 MarKha
Lithaunia
PQRSTK








Thursday, October 17, 2013

What different between Var, Object and Dynamic data type in c#? How to use??

The data types (var, object and dynamic) are generally used while you are working
with anonymous type.

Var:
This is a data type, var data type introduced since .Net Framework 3.0. Var datatype is pre-determined at compilation time. We cannot leave it unassigned or having a null value we need to implicitly assign a value to it otherwise we`ll receive an error. Var does not require explicit type casting for any operation at run-time and we cannot change the type of these variables at run-time.

Object:
This is a root data type, object data type introduced since .Net Framework 2.0. Object data type is determined at run time. This is a alias for System.Object and object is the root of all classes (root data type).
In this we need to do boxing and unboxing to retrieve the actual value. The Object does not allow you to perform any operation such as mathematical operation without doing "boxing and unboxing". Its means we require explicit type casting for any operation at run-time. Useful when doesn't have more information about the data type.

Dynamic:
This is a data type, dynamic data type introduced since .Net Framework 4.0. The dynamic data type allows you to perform any operations and will be resolved at run time. It does not require explicit type casting for any operation at run-time, because it identifies the types at run-time only. Dynamic type can be passed as
function argument and function also can return object type. Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code.

Example Code:
namespace EDUDOTNET_Var_Object_Dynamic
{
    class Program
    {
        static void Main(string[] args)
        { 
            /// <summary>
            /// Example of var type
            /// </summary>            
            // assign a number value
            var varItem = 100;
            //does not require explicit type casting for any operation at runtime
            varItem = varItem + 100;
            //varItem ="EDUDOTNET";//We cannot change the type of these variables at runtime. 
 
            /// <summary>
            /// Example of object type
            /// </summary>
            // assign a number value
            object objItem = 100;
            //require explicit type casting for any operation at runtime
            objItem = (int)objItem + 100;
            //object type does not allows you to perform any operations 
            //objItem += 100; 
            //[Error: Operator '+=' cannot be applied to operands of type 'object' ana 'int']
            objItem = "EDUDOTNET";
 
            /// <summary>
            /// Example of dynamic type
            /// </summary>
            // assign a number value
            dynamic dynItem = 100;
            //does not require explicit type casting for any operation at runtime
            dynItem = dynItem + 100;
            // dynamic type allows you to perform any operations
            dynItem += 100;
            dynItem = "EDUDOTNET";
        }
    }
}



Tuesday, October 15, 2013

How to find all image input in a container class and How to replace title string of each image (Replace '$' with '-') using Jquery?

We can find all image input inside a container class using Jquery .each() function and Replace/Update title string of using Jquery .replace() function.

What is .each() Method in Jquery?
This is Jquery library function. The Jquery  library provides a method, Each(), which will loop through each element of the target element (Object). Using this function we can get all value of each element inside the a container eg. class, tag, id etc. Very useful for multi element DOM manipulation, looping arrays and object properties.

Jquery Code
<script type="text/javascript">
    $(document).ready(function () {
       //Find all image input inside 'ContainerToolbar' class
       var count = 0;
       $('.ContainerToolbar input[type=image').each(function () {
         var allimg = $(this).attr("title");//Get title of image 
         $(this).attr("title", allimg.replace(/\$/g, "_"))//Replace all '$' with '_' in title string.                
         count++;//Counting image input inside the 'ContainerToolbar' class
       });
       alert(count);//Alert displaying total count.
    });
</script>

HTML Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Jquery Demo</title>
    <script src="js/jquery-1.6.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //Find all image input inside 'ContainerToolbar' class
            var count = 0;
            $('.ContainerToolbar input[type=image').each(function () {
                var allimg = $(this).attr("title"); // Get title of image 
                $(this).attr("title", allimg.replace(/\$/g, "_")) //Replace all '$' with '_' in title string.                
                count++; //Counting image input inside the 'ContainerToolbar' class
            });
            alert(count); //Alert displaying total count.
        });
    </script>
</head>
<body>
    <div id="ContainerToolbar" class="ContainerToolbar" style="background-imageurl(../Skins/Custom/Background.gif);">
        <table id="ContainerToolbarGroup" class="ContainerToolbarGroup" cellpadding="0" cellspacing="0"
            style="floatleft;">
            <tbody>
                <tr>
                    <td unselectable="on">
                        <div id="NavigateBack" title="Navigate back">
                            <table id="NavigateBack_Button" class="DisabledButton" cellspacing="0" cellpadding="0"
                                border="0" style="border-collapsecollapse;">
                                <tbody>
                                    <tr>
                                        <td class="ImageButtonCell" unselectable="on">
                                            <input type="image" name="NavigateBack$ctl00" title="Navi$gate$Back" class="Enabled"
                                                src="../Skins/Custom/NavBack.png" onclick="return false;" style="border-width0px;"
                                                id="NavigateBack_ctl00">
                                            <input type="image" name="NavigateBack$ctl01" title="Navi$gate$Back" class="Disabled"
                                                src="../Skins/Custom/NavBackDisabled.png" onclick="return false;" style="border-width0px;"
                                                id="NavigateBack_ctl01">
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </td>
                    <td width="5px" unselectable="on">
                        &nbsp;
                    </td>
                    <td unselectable="on">
                        <div id="NavigateForward" title="Navigate forward">
                            <table id="NavigateForward_Button" class="DisabledButton" cellspacing="0" cellpadding="0"
                                border="0" style="border-collapsecollapse;">
                                <tbody>
                                    <tr>
                                        <td class="ImageButtonCell" unselectable="on">
                                            <input type="image" name="NavigateForward$ctl00" title="$Navigate$Forward$" class="Enabled"
                                                src="../Skins/Custom/NavForward.png" onclick="return false;" style="border-width0px;"
                                                id="NavigateForward_ctl00">
                                            <input type="image" name="NavigateForward$ctl01" title="$Navigate$Forward$" class="Disabled"
                                                src="../Skins/Custom/NavForwardDisabled.png" onclick="return false;" style="border-width0px;"
                                                id="NavigateForward_ctl01">
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>
 

Input Image Default Title
Eg. title="$Navigate$Forward$"

Input Image Title After Page Render (After Calling Jquery)
Eg. title="_Navigate_Forward_"

Display Count In Alert()




Output With Updated Image Title









Tuesday, October 8, 2013

What is a Tuple in C#? When it use? Example of Tuple

Tuple:

Tuple is a generic static class that was added to C# 4.0 and it can hold any amount of elements, and they can be any type we want. So using tuple, we can return multiple values.One great use of tuple might be returning multiple values from a method. It provides an alternative to "ref" or "out" if you have a method that needs to return multiple new objects as part of its response.


A tuple allows you to combine multiple values of possibly different types into a single object without having to create a custom class. This can be useful if you want to write a method that for example returns three related values but you don't want to create a new class.

We can create a Tuple using the Create method. This method provide to 8 overloads, so we can use a maximum of 8 data types with a Tuple. 


Followings are overloads: 


Create(T1)- Tuple of size 1
Create(T1,T2)- Tuple of size 2
Create(T1,T2,T3) – Tuple of size 3
Create(T1,T2,T3,T4) – Tuple of size 4
Create(T1,T2,T3,T4,T5) – Tuple of size 5
Create(T1,T2,T3,T4,T5,T6) – Tuple of size 6
Create(T1,T2,T3,T4,T5,T6,T7) – Tuple of size 7
Create(T1,T2,T3,T4,T5,T6,T7,T8) – Tuple of size 8

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
namespace EDUDOTNET_TupleDemo
{
    class EDUDOTNET
    {
        static void Main(string[] args)
        {
            //This represents a tuple of size 3 (Create a new 3-tuple, or Triple) with all string type
            var tuple = Tuple.Create<stringstringstring>("EDUDOTNET""IT""SOLUTION");
            Console.WriteLine(tuple); 
            //This represents a tuple of size 2 (Create a new 2-tuple, or Pair) with int & string type
            var tuple1 = Tuple.Create<intstring>(51, "CTO-EDUDOTNET");
            Console.WriteLine(tuple1);        
            
            //We can also access values of Tuple using ItemN property. Where N point to a particular item in the tuple.
            //This represents a tuple of size 4 (Create a new 4-tuple, or quadruple) with 3 string & 1 int type
            var tuple2 = Tuple.Create<intstringstringstring>(1, "Khumesh""EDUDOTNET""IT-Solution");
            Console.WriteLine(tuple2.Item1);
            Console.WriteLine(tuple2.Item2);
            Console.WriteLine(tuple2.Item3);
            Console.WriteLine(tuple2.Item4);
            Console.WriteLine("Enjoying With Tuple.......... :)");
            Console.ReadLine();
        }
    }
}