Wednesday, October 31, 2012

Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET


JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming LanguageStandard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
JSON is built on two structures:
  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.
In JSON, they take on these forms:
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.

Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET


JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming LanguageStandard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
JSON is built on two structures:
  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.
In JSON, they take on these forms:
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.

Monday, October 29, 2012

Authenticate .NET Web Service with Custom SOAP Header in Sql Server

Authenticate .NET Web Service with Custom SOAP Header in Sql Server 

First step open Web Services 

Basically all the client needs to do is create an authentication object, fill out the username and password, then pass them to the web service object. The web service code is also pretty simple, the .NET framework lets you create custom SOAP headers by deriving from the SoapHeader class, so we wanted to add a username and password:


and create Authenticate Program .create and UserMaster Table 

Create Table UserMaster(UserId int identity ket(1,1),UserName varchar(20),UserPwd varchar(50))

then after we can create a web services in asp.net add + web services 


using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public MyHeader consumer;
    public SqlConnection con = null;
    public SqlCommand cmd = null;
    
    public Service () 
    {

        con = new SqlConnection("server=10.21.157.35;database=elekha;uid=elekha;pwd=iamdoingaccounts;");

    }

    [WebMethod]
    [SoapHeader("consumer", Required = true)]
    public string GetBalance() 
    {
        if (checkConsumer())
            return consumer.UserName + " had 9999999999999 credit";
        else
            return "Error in authentication";
    }
    private bool checkConsumer()
    {
        try
        {
            // In this method you can check the username and password 
            // with your database or something
            // You could also encrypt the password for more security
            
                con.Open();
            

            cmd = new SqlCommand("select * from UserMaster where UserName='"+consumer.UserName+"' and UserPwd='"+consumer.Password+"'", con);
            SqlDataReader rd = cmd.ExecuteReader();
            if (consumer != null)
            {
                if (rd.Read())
                {
                    return true;
                    
                }
                else return false;
            }
            else { return false; }
            con.Close();
            //if (consumer != null)
            //{
            //    if (consumer.UserName == "virendra" && consumer.Password == "1234")
            //        return true;
            //    else
            //        return false;
            //}
            //else
            //    return false;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public class MyHeader : SoapHeader
    {
        public string UserName;
        public string Password;
    }
}
==========================================================
I should also mention that when I say SOAP headers, I actually mean the soap:Header element in a SOAP request, it has nothing to do with the HTTP headers sent with the request. The SOAP request looks something like:
then we can call the Web services in front end 

or .aspx Page


<asp:Panel ID="Panel1" runat="server" BackColor="#CCFFCC" Height="200px" 
        Width="300px">
        <br />
        <br />
        User_Name ::
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        Password&nbsp; ::&nbsp;&nbsp;
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" BackColor="#99CCFF" 
            onclick="Button1_Click" Text="Button" />
    </asp:Panel>

We just get reference to the service and the SOAP header, assign the SOAP header properties, attach it with the SOAP message and then make our call to the web method.
then code behind page Or aspx.cs Page


protected void Button1_Click(object sender, EventArgs e)
    {
        MyWebServices.Service ws = new MyWebServices.Service();
        MyWebServices.MyHeader sh = new MyWebServices.MyHeader();
        sh.UserName = TextBox1.Text.ToString();
        sh.Password = TextBox2.Text.ToString();
        ws.MyHeaderValue = sh;
        Response.Write("<script>alert('"+ws.GetBalance()+"')</script>");
    }



=====================================


Thursday, October 25, 2012

Key codes of keydown and keyup events


keydown/keyup event.keyCode                    
Opera   MSIE  Firefox  Safari  Chrome    Key pressed
_________________________________________________________

  8       8       8       8       8      Backspace
  9       9       9       9       9      Tab
 13      13      13      13      13      Enter
 16      16      16      16      16      Shift
 17      17      17      17      17      Ctrl
 18      18      18      18      18      Alt
 19      19      19      19      19      Pause, Break
 20      20      20      20      20      CapsLock
 27      27      27      27      27      Esc
 32      32      32      32      32      Space
 33      33      33      33      33      Page Up
 34      34      34      34      34      Page Down
 35      35      35      35      35      End
 36      36      36      36      36      Home
 37      37      37      37      37      Left arrow
 38      38      38      38      38      Up arrow
 39      39      39      39      39      Right arrow
 40      40      40      40      40      Down arrow
         44      44      44      44      PrntScrn (see below)
 45      45      45      45      45      Insert
 46      46      46      46      46      Delete
 48-57   48-57   48-57   48-57   48-57   0 to 9
 65-90   65-90   65-90   65-90   65-90   A to Z  
112-123 112-123 112-123 112-123 112-123  F1 to F12 
144     144     144     144     144      NumLock
145     145     145     145     145      ScrollLock

188     188     188     188     188      , <
190     190     190     190     190      . >
191     191     191     191     191      / ?
192     192     192     192     192      ` ~
219     219     219     219     219      [ { (see below)
220     220     220     220     220      \ |
221     221     221     221     221      ] }
222     222     222     222     222      ' "
In most browsers, pressing the PrntScrn key fires keyup events only. 
Caution! Key code 219 also corresponds to the Win Key (Start) in Opera.
The following key codes differ across browsers:
      keydown/keyup event.keyCode                    
Opera   MSIE  Firefox  Safari  Chrome    Key pressed
_________________________________________________________ 

 59     186      59     186     186      ; :             
 61     187     107     187     187      = +             
109     189     109     189     189      - _             
219      91      91      91      91      WIN Key (Start) 
  0      93      93      93      93      WIN Menu        


Opera    All Others (NumLock On/Off)     Key pressed
_________________________________________________________

48/45       96/45                        Numpad 0 Ins    
49/35       97/35                        Numpad 1 End    
50/40       98/40                        Numpad 2 Down   
51/34       99/34                        Numpad 3 Pg Down
52/37      100/37                        Numpad 4 Left   
53/12      101/12                        Numpad 5        
54/39      102/39                        Numpad 6 Right  
55/36      103/36                        Numpad 7 Home   
56/38      104/38                        Numpad 8 Up     
57/33      105/33                        Numpad 9 Pg Up  
42/42      106/106                       Numpad *        
43/43      107/107                       Numpad +        
45/45      109/109   (45 is also Ins)    Numpad -        
78/46      110/46    (78 is also N)      Numpad . Del    
47/47      111/111                       Numpad /