Tuesday, 31 December 2013

Quick Example of calling a web service with JQuery

Quick Example of calling a web service with JQuery where Web Service method and JavaScript/ JQuery script are in same WebForm i.e. in a single aspx and its associated c# file.


I am not going to explain. Just check the code below

Note:- Please download and Import latest JQuery file. Just Google it or go to the link
https://developers.google.com/speed/libraries/devguide
and find & click on JQuery

JavaScript Code

<script src="Your_JQuery_File_Path_Along_With_Extension.JS" type="text/javascript"></script>
//Can be like this<script src="jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
    function search() {
    $.ajax({
        type: "POST",
        url: "Your-Web-Form-Name.aspx/search",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert(msg);
        },
        error:function (a,b,c) {
             alert(a+", "+b+", "+c);
        }
    });
    }
    search();
</script>

Note:- Please replace Your-Web-Form-Name.aspx with your web form name with extension .aspx.

C# Code

[System.Web.Services.WebMethod]
public static string search()
{
    return "worked";
}

Monday, 30 December 2013

Whatsapp not working in nokia c3 with wifi (Solved)

Whatsapp not working in nokia c3 with wifi (Solved)

I installed Whatsapp in my Dad's Cell Phone Nokia C3.
Download Here
          whatsapp for s40 at whatsapp Home
          whatsapp at Nokia OVI Store
It was installed with no error however when i started it and tried to activate it, after inserting the phone number, it shows a connection error. May be the phone was got confused to whether to use gprs/edge or wifi.
I tried it with every method
  1. gprs/edge on, wifi on
  2. gprs/edge on, wifi off
  3. gprs/edge off, wifi on
How ever we all know that the third option is the best. So did some research and found the solution.

Here is the solution
In your Nokia C3
  • Go to setting
  • Configuration settings 
  • Personal configuration settings
  • Add new
  • Web. Don't enter any data in any field & press back. 
  • Then press again Add new
  • Access point. Again don't enter data in any field & press back. 
  • Now select each of them & activate them as default. 
  • Come back to config settings
  • Default config settings
  • Personal - activate
  • Then select activate default in all apps
  • Then select prefered access point & select the one labelled as My access point.
Now the phone would not connect to internet using the gprs/edge. Open Whatsapp and select your wifi connection whenever asked.
Done. Its working now.

Thursday, 19 December 2013

Disable Any Keyboard Key using JQuery


Disable Any Keyboard Key using JQuery

Hello there.
Some time, you may want to disable any key e.g. "Enter Key" (Keyboard's Enter Key) in your Web Page. You can do this with a small block of JQuery code.

Note:- Please download and Import latest JQuery file. Just Google it or go to the link

Here is the Code

<script src="Your_JQuery_File_Path_Along_With_Extension.JS" type="text/javascript"></script>
//Can be like this<script src="jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){
$('html').bind('keydown', function(e)
            {
               if(e.keyCode == 13)
               {
                  return false;
               }
            });
});
</script>

Just replace the code value 13 in above JavaScript code block (if(e.keyCode == 13)) with the code of key you want to disable.
Find the key codes in tables given below:

Keyboard key codes 


Key
Code
Key
Code
Key
Code
Key
Code
backspace 8 0 48 a 65 left window key 91
tab 9 1 49 b 66 right window key 92
enter 13 2 50 c 67 select key 93
shift 16 3 51 d 68 numpad 0 96
ctrl 17 4 52 e 69 numpad 1 97
alt 18 5 53 f 70 numpad 2 98
pause/break 19 6 54 g 71 numpad 3 99
caps lock 20 7 55 h 72 numpad 4 100
escape 27 8 56 i 73 numpad 5 101
page up 33 9 57 j 74 numpad 6 102
page down 34 k 75 numpad 7 103
end 35 l 76 numpad 8 104
home 36 m 77 numpad 9 105
left arrow 37 n 78 multiply 106
up arrow 38 o 79 add 107
right arrow 39 p 80 subtract 109
down arrow 40 q 81 decimal point 110
insert 45 r 82 divide 111
delete 46 s 83
t 84
u 85
v 86
w 87
x 88
y 89
z 90

Key
Code
Key
Code
f1 112 num lock 144
f2 113 scroll lock 145
f3 114 semi-colon 186
f4 115 equal sign 187
f5 116 comma 188
f6 117 dash 189
f7 118 period 190
f8 119 forward slash 191
f9 120 grave accent 192
f10 121 open bracket 219
f11 122 back slash 220
f12 123 close braket 221
single quote 222

Monday, 16 December 2013

Find and Delete Duplicate records (Ambiguity) in database while keeping one (In SQL Server)

Find and Delete Duplicate records (Ambiguity) in database while keeping one (In SQL Server)

In our daily life with SQL, we can have a situation where we haven't applied a primary/ unique key on our database table or we do have a primary/ unique key on a column however reset of the columns can have duplicate information (Ambiguity) or our logic for uniquely identifying a record fails  .
In such situation we can have multiple records with same data. Now this is what we call a problem. We need to identify those duplicate records and delete them while keeping one or we can say the original one.
We can do this with the help of ROW_NUMBER() function in SQL Server.

Here is the solution 

Note:- I am explaining it from the very beginning i.e. from creating a table to inserting duplicate records to deleting them (for beginners), you can directly scroll down for the actual query if you are not a beginner.
So lets practice and start with the beginning.

Step 1

Use/ Create a database

Query
CREATE DATABASE SQLisAwesome

USE SQLisAwesome

Step 2

Create a Table

Query
 CREATE TABLE Dummy (
    [Id] [INT]  ,
    [Name] [VARCHAR](100) NOT NULL,
    [Designation] [VARCHAR](100) NOT NULL,
    [Salary] [INT] NOT NULL
)

Step 3

Insert dummy values

Query
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Ram','SE',25000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Sham','SA',26000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Sham','SA',26000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Id],[Name],[Designation],[Salary]) VALUES(NULL,'Ramesh','TL',30000)

Step 4

Now check the data

Query
SELECT * FROM [Dummy]
All Records

Identifying the duplicate records

Query
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Id, Name, Designation, Salary ORDER BY Name),* FROM [Dummy]
Records with Duplicate Record Number equal to the occurrence of that particular record

Now you can identify the duplicate records (duplicate for given columns written after the keywords "PARTITION BY" in above query i.e. "Id, Name, Designation, Salary") with the help of DuplicateRowNo columns
as DuplicateRowNo this will have value 1 if record is not duplicate and more than 1 (2,3,4) if record is duplicate according to the occurrence of that particular record (Number of time the record is repeating it self).

Now separating the duplicate record form the original one(Keep one select rest)
we can do this using Common Table Expressions (CTE)

Query
WITH ABC AS (
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Id, Name, Designation, Salary ORDER BY Name),* FROM [Dummy]
) SELECT * FROM ABC WHERE DuplicateRowNo>1
Duplicate record having occurrence more than one (Keeping original one)

Note:- use semicolon (;) in before WITH keyword if any query is written before it.

Step 5

Delete the Duplicate

Query
WITH ABC AS (
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Id, Name, Designation, Salary ORDER BY Name),* FROM [Dummy]
) DELETE FROM ABC WHERE DuplicateRowNo>1

In Addition
Now suppose you have a Identity column in table which is unique for all records but rest of the columns have duplicate values

Step 6 

Query
ALTER TABLE [Dummy] DROP COLUMN Id
LTER TABLE [Dummy] ADD Id INT NOT NULL UNIQUE IDENTITY(1,1)

Now you will have a Unique, Auto Increment column which cannot have duplicate value however rest of the columns can have.
Insert Dummy values, Leave column Id as it is now Auto Increment.

Query
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Ram','SE',25000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Sham','SA',26000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Sham','SA',26000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Suresh','OE',15000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Ramesh','TL',30000)
INSERT INTO [dbo].[Dummy]([Name],[Designation],[Salary]) VALUES('Ramesh','TL',30000)

Step 7

Now check the data

Query
SELECT * FROM [Dummy]
Duplicate records with unique identity key (Id)
Note:- SQL Server will automatically insert integer numbering in Id column for already existing records.

Identifying the duplicate records. This time do not write Column [Id] After "PARTITION BY" keywords

Query
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Name, Designation, Salary ORDER BY Name),* FROM [Dummy]

Sparating the duplicate record form the original one(Keep one select rest) using CTE

Query
WITH ABC AS (
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Name, Designation, Salary ORDER BY Name),* FROM [Dummy]
) SELECT * FROM ABC WHERE DuplicateRowNo>1

Step 8

Delete the Duplicate

Query
WITH ABC AS (
SELECT DuplicateRowNo=ROW_NUMBER() OVER (PARTITION BY Name, Designation, Salary ORDER BY Name),* FROM [Dummy]
) DELETE FROM ABC WHERE DuplicateRowNo>1

Done
Check Your data

Query
SELECT * FROM [Dummy]

Thursday, 26 September 2013

Second Max Salary ( nth max salary)

Second Max Salary ( nth max salary)

In interviews related to SQL a questions is almost always asked. You must have heard or being asked in your career that is " Find Second Max Salary from the table". A beginner of SQL will start thinking about the System function/ Aggregate Function MAX() and will get confused that MAX() will return the Maximum value, how is it possible to find the Second max.

I will say, why just second max as we can find 5th max, 10th max that is nth max.

Here is the solution 

lets practice and start with the beginning.

Step 1

Use/ Create a database

Query
CREATE DATABASE SQLisAwesome

USE SQLisAwesome

Step 2

Create a Table

Query
 CREATE TABLE SalaryDummy (
    [Id] [INT] NOT NULL IDENTITY(1,1),
    [Name] [VARCHAR](100) NOT NULL,
    [Designation] [VARCHAR](100) NOT NULL,
    [Salary] [INT] NOT NULL,
 CONSTRAINT [PK_SalaryDummy] PRIMARY KEY CLUSTERED
    ( [Id] ASC )
)

Step 3

Insert dummy values

Query
INSERT INTO [dbo].[SalaryDummy]([Name],[Designation],[Salary]) VALUES('Ram','SE',25000)
INSERT INTO [dbo].[SalaryDummy]([Name],[Designation],[Salary]) VALUES('Sham','SA',26000)
INSERT INTO [dbo].[SalaryDummy]([Name],[Designation],[Salary]) VALUES('Suresh','OE',15000)
INSERT INTO [dbo].[SalaryDummy]([Name],[Designation],[Salary]) VALUES('Ramesh','TL',30000)

 Step 4

 Get second Max salary

Using Subquery (Replace 2 at right hand side of '=' in the where condition with your desired no of Max salary)

Query
SELECT  a.Salary
FROM   SalaryDummy a
WHERE   Salary IS NOT NULL
AND (SELECT COUNT(Salary) FROM SalaryDummy b WHERE b.Salary>a.Salary)+1=2

now suppose you have two different persons having same salary

Query 
INSERT INTO [dbo].[SalaryDummy]([Name],[Designation],[Salary]) VALUES('Aman','TL',30000)

In this case we will use DISTINCT key word in the condition

Query 
SELECT  a.Salary
FROM   SalaryDummy a
WHERE   Salary IS NOT NULL
AND (SELECT COUNT( DISTINCT Salary) FROM SalaryDummy b WHERE b.Salary>a.Salary)+1=2
You can also find up to nth salaries in a single query by modifying the where condition a little

Query
SELECT  a.Salary
FROM   SalaryDummy a
WHERE   Salary IS NOT NULL
AND (SELECT COUNT( DISTINCT Salary) FROM SalaryDummy b WHERE b.Salary>a.Salary)+1<=2
ORDER BY a.Salary DESC 

You can also use the logic in the where condition for displaying the position order of salary

Query
SELECT  a.Salary, (SELECT COUNT( DISTINCT Salary) FROM SalaryDummy b WHERE b.Salary>a.Salary)+1 AS [Position Order]
FROM   SalaryDummy a
WHERE   Salary IS NOT NULL
AND (SELECT COUNT( DISTINCT Salary) FROM SalaryDummy b WHERE b.Salary>a.Salary)+1<=2
ORDER BY a.Salary DESC

All the above can also be achieved using JOINs and CTE (Common table expression)

Query
;WITH abc AS (       
    SELECT  a.Salary, COUNT ( DISTINCT a.Salary) AS Position
    FROM    SalaryDummy a INNER JOIN SalaryDummy b ON b.Salary>=a.Salary
    WHERE   a.Salary IS NOT NULL
    GROUP BY a.Salary
) SELECT * FROM abc WHERE abc.Position=2

and for up to nth salary

Query
;WITH abc AS (       
    SELECT  a.Salary, COUNT ( DISTINCT a.Salary) AS Position
    FROM    SalaryDummy a INNER JOIN SalaryDummy b ON b.Salary>=a.Salary
    WHERE   a.Salary IS NOT NULL
    GROUP BY a.Salary
) SELECT abc.Salary, abc.Position FROM abc WHERE abc.Position<=2
ORDER BY abc.Salary DESC

Friday, 2 August 2013

Web Service in JAVA

Web Service in JAVA (using Notepad, JAVA JDK, Tomcat Web Server, some additional JARs)

In this tutorial we will learn how to create Web Service using JAVA.

What is a WEB Service?

Web Service Wiki
Web Service W3School

WEB Service in JAVA

Java provide "javax.jws.WebService" class for creating a Web Service. How ever for creating a web service, with just notepad, we need some additional JAR files, which can be found form Net Beans installation directory. If you do not have Net Beans installed in your system, do not bother, these file are available in this tutorial.
Follow the steps.

Step 1: Create a .java file and Compile it.

Open notepad write the following code and save it with Addition.java name (Extension should be .java) at path "c:\java\src\ws\Addition.java".

Addition.java

// c:\java\src\ws\Addition.java
package ws;
import javax.jws.WebService;

@WebService
public class Addition {
 public double add( double value1, double value2 ) {
  return value1 + value2;
 }
}
   Now compile this file.
  1. Open Command prompt 
  2. Navigate to path "c:\java\src\ws\" 
  3. Type Command: javac Addition.java 
Snapshot:
Note: Make sure your environment variable is set for Java PATH if not then either set it or compile this file your java's bin folde. 

Step 2: Create web.xml file. 

Open notepad write the following code and save it with web.xml name (Name should be web.xml only).

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Addition</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Addition</servlet-name>
        <url-pattern>/add</url-pattern>
    </servlet-mapping>
<!-- not needed
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
-->
</web-app>

Step 3: Create sun-jaxws.xml file. 

Open notepad write the following code and save it with sun-jaxws.xml name (Name should be sun-jaxws.xml only).

sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
  <endpoint implementation="ws.Addition" name="Addition" url-pattern="/add"/>
</endpoints>

Step 4: Create context.xml file. 

Open notepad write the following code and save it with context.xml name (Name should be context.xml only).

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Addition"/>

Step 5: Create directories. 

Now go to your Tomcat's webapps ("Tomcat's installation path\Tomcat\webapps") folder and create following directories in it
  1. Tomcat's installation path\Tomcat\webapps\Addition
  2. Tomcat's installation path\Tomcat\webapps\Addition\META-INF
  3. Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF
  4. Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF\classes
  5. Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF\classes\ws
  6. Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF\lib

Step 6: Download JARs. 

  1. activation.jar
  2. webservices-api.jar
  3. webservices-extra.jar
  4. webservices-extra-api.jar
  5. webservices-rt-2.0.jar
  6. webservices-tools.jar

Step 6: Copy files. 

  • Copy and paste all the jar files in "Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF\lib" folder.
  •  Copy and paste Addition.class file form "c:\java\src\ws\Addition.class" to "Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF\classes\ws"
  • Copy and paste web.xml & sun-jaxws.xml to "Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF"
  • Copy and paste context.xml to "Tomcat's installation path\Tomcat\webapps\Addition\META-INF"

Step 7: Run on localhost. 

Start your Tomcat server and run the web service in browser at web address

http://localhost:port/Addition/add?wsdl

Monday, 29 July 2013

JQuery :- Two Tab Options, Slide Down Form Template

JQuery :-  Two Tab Options, Slide Down Form Template
Suppose you want to create a small, dynamic form having to tab options in it.
Example:  Password Recovery
 I am here taking the example of Password Recovery Form where one have two basic options.
  1. Through a link to a registered Email-Id
  2. Through a Security Question
Example
Heading :: (e.g. Password Recovery )
Click on Security Question Tab or Email Tab

User Name
Prerequisite knowledge  and Requirements
  1. JQuery js file (e.g. jquery.min.js)
  2. JavaScript
  3. HTML
  4. CSS 3 
Code (Copy & paste in notepad and save it as pageName.html ) (Extension should be .html)
Click here for show/hide code