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]