Shalvin Interests

Tuesday, February 23, 2016

JavaScript Basics

GetElementById

<html>
<head>
    <title></title>
    <script>
        window.onload = function()
        {
            document.getElementById("Hello").innerHTML = "Shalvin P D";
        }
    </script>
</head>
<body>
    <div id="Hello"></div>
</body>
</html>

Check whether Cookie is enabled
<script>
        window.onload = function () {
            if (navigator.cookieEnabled) {
                alert("This browser supports cookies");
            }
            else {
                alert("This browser does not support cookies");
            }
        }
    </script>

typeof
 function NumberEg()
        {
             var fare = 22.50;
            alert(typeof fare);
        }
output :  numeric



Strings
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <script>
        function ConcatEg()
        {
            alert("Shalvin" + ' P D');
        }

        function Embed()
        {
            alert("Shalvin \"P  D\"");
        }

        function EmbedSingleQuote()
        {
            alert('Merin\'s assignment');
        }

        function LengthEg()
        {
            var name = 'Simi Simon';
            alert(name.length);
        }

        function TrimEg()
        {
            var name = ' Shibin ';
            alert('[' + name.trim() + ']');
            alert('[' + name + ']');
        }
        function toLowerEg() {
            var name = 'Sreelakshmi S';
            alert(name.toLocaleLowerCase());
        }
        function toUpperCaseEg() {
            var name = 'soorya';
            alert(name.toUpperCase());
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        <br />
        <asp:Button OnClientClick ="ConcatEg()" ID="btnSD" runat="server" Text="Single and Double Quote" />
    </p>
    <p>
        <asp:Button OnClientClick ="Embed()" ID="btnEmbed" runat="server" Text="Embed Double Quotes" />
    </p>
    <p>
        <asp:Button OnClientClick ="EmbedSingleQuote()" ID="btnEmbedSingle" runat="server" Text="Embed Single Quote" />
    </p>
    <p>
        <asp:Button OnClientClick="LengthEg()" ID="btnLength" runat="server" Text="Length" />
    </p>
    <p>
        <asp:Button Text="trim" runat="server" OnClientClick ="TrimEg()" />
    </p>
    <p>
    <asp:Button OnClientClick="toLowerEg()" Text="toLower" runat="server" />

        </p>
    <p>
    <asp:Button Text="toUpperCase" OnClientClick="toUpperCaseEg()" runat="server" />
        </p>
</asp:Content>

JavaScript Arrays

<script>
        var friends = [];
        friends[0] = 'Praseed';
        friends[1] = 'Pai'

        window.onload = function()
        {
              document.write(friends[0]);
        }
    </script>

Push for adding items to Array

<script>
        var friends = [];
        friends[0] = 'Praseed';
        friends[1] = 'Pai'
        friends.push('Vaisakh');

        window.onload = function()
        {
            document.write(friends[0] + "<br/>");
            document.write(friends[2]);
        }
    </script>

Pop


 <script>
        var friends = [];
        friends[0] = 'Praseed';
        friends[1] = 'Pai'
        friends.push('Vaisakh');
        friends.pop();

        window.onload = function()
        {
            document.write(friends[0] + "<br/>");
            document.write(friends[2]);
        }
    </script>


Iterating an Array
<script>
        var friends = [];
        friends[0] = 'Praseed';
        friends[1] = 'Pai'
        friends.push('Vaisakh');
        
        window.onload = function()
        {
            for (var i = 0; i < friends.length; i++) {
                document.write(friends[i] + "<br/>");
            }
        }
    </script>


Asp .Net JavaScript Example

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ArrayPushDisplayShalvin.aspx.cs" Inherits="ArrayPushDisplayShalvin" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
 <script>
     var Friends = ['Shalvin', 'Reny', 'Praseed'];
     window.onload = function () {
         SetFocusOnTextBox();
         Display();
     }
     function Display()
     {
         var lbl = document.getElementById("lblMessage");
         lbl.innerHTML = "";
         for (var i = 0; i < Friends.length; i++) {
             lbl.innerHTML += Friends[i] + "<br/>";
             //lbl.innerText += Friends[i] + "<br/>";
     }
         return false;
     }
     function Save()
     {
         var s = document.getElementById("txtName");
         Friends.push(s.value);
         Display();
         s.value = "";
         s.focus();
         return false;
     }

     function SetFocusOnTextBox()
     {
         var s = document.getElementById("txtName");
         s.focus();
     }

     function Delete() {
         Friends.pop();
         Display();
         return false;
     }
 </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        Name
        <asp:TextBox ID="txtName" runat="server" ClientIDMode="Static"></asp:TextBox>
    </p>
    <p>
        <asp:Button ID="btnSave" OnClientClick ="return Save()" runat="server" Text="Save " ClientIDMode="Static" />
&nbsp;
        <asp:Button ID="btnDeleteFromLast" runat="server" 
            OnClientClick ="return Delete()"
            ClientIDMode="Static" Text="Delete from Last" />
&nbsp;
        <asp:Button ID="btnDisplay" runat="server" Text="Display" 
            OnClientClick ="return Display()" ClientIDMode="Static" OnClick="btnDisplay_Click" />
    </p>
    <p>
        <asp:Label ID="lblMessage" runat="server" 
            ClientIDMode="Static"></asp:Label>
        <br />
    </p>
</asp:Content>

JavaScript Objects

I am creating an object.

<head>
    <script>
        var Contact = {};
        Contact.Name = "Shalvin P D";
        window.onload = function () {
            alert(Contact.Name);
        }
    </script>
</head>
<body>
</body>

Another way of doing the same:

<head>
    <title></title>
    <script >
        var Contact = {Name : "Shalvin P D"};
            window.onload = function () {
            alert(Contact.Name);
        }
    </script>
</head>
<body>

</body>

Let's have few more properties

<head>
    <title></title>
    <script >
        var Contact = {Name : "Shalvin P D", Location : "Kochi"};
            window.onload = function () {
            alert("I am " + Contact.Name + " located at " + Contact.Location);
        }
    </script>
</head>
<body>

</body>



If I try to alert something like Contact.Specialization we get undefined. Because Specialization doesn't exist in Contact.

Bracket Notation

<head>
       <script>
        var Contact = {};
        Contact["Name"] = "Shalvin P D";
        window.onload = function () {
            alert(Contact.Name);
        }
    </script>
</head>


Iterating through properties using for in


<script>
        var Contact = {Name : "Shalvin P D", Location : "Kochi", Specialization:".Net"};
        window.onload = function () {
            for (var key in Contact) {
               document.write(key + "<br/>");
            }
        }
</script>

Iterating through property values


  <script>
        var Contact = {Name : "Shalvin P D", Location : "Kochi", Specialization:".Net"};
        window.onload = function () {
            for (var key in Contact) {
                document.write(Contact[key]);
            }
        }
    </script>

Wednesday, February 17, 2016

AngularJS Part 3 : Filters


<body ng-app="">
    <ul ng-init="name=['Shalvin','Reny John Jose', 'John James','Prakash Jacob Mathew']">
        <li ng-repeat="x in name">{{x | uppercase }}</li>

    </ul>
 </body>




<body ng-app="">
    Name <input type="text" ng-model="namef" />
    <ul ng-init="name=['Shalvin','Reny John Jose', 'John James','Prakash Jacob Mathew','Anson']">
        <li ng-repeat="x in name | filter:namef">{{x}}</li>
    </ul>
</body>


Filter and OrderBy



<body ng-app="">
    Name <input type="text" ng-model="namef" />
    <ul ng-init="name=['Shalvin','Reny John Jose', 'John James','Prakash Jacob Mathew','Anson']">
        <li ng-repeat="x in name | filter:namef | orderBy:x">{{x}}</li>
    </ul>
</body>

Saturday, February 6, 2016

AngularJS Part 2 : Controllers


<html ng-app="myApp">
<head >
    <title></title>
    <script src="Scripts/angular.js"></script>
</head>
<body ng-controller="HomeCtrl">
         <div>
            <ul>
                <li ng-repeat="x in Friends">{{x}}</li>
            </ul>
         </div>
    <script>
        var app = angular.module("myApp", [])
        .controller("HomeCtrl", function ($scope) {
            $scope.Friends = ['Shalvin', 'Pankaj', 'Praseed'];
        });
    </script>
</body>
</html>

Controller in separate JavaScript file
App.js

var app = angular.module("myApp", [])
.controller("HomeCtrl", function ($scope) {
    $scope.Friends = ['Shalvin', 'Pankaj', 'Praseed'];
});

index.htm
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myApp">
<head>
    <title></title>
    <script src="../Scripts/angular.js"></script>

    <script src="Apps/App.js"></script>
</head>
<body ng-controller="HomeCtrl">
    <ul>
        <li ng-repeat="x in Friends">{{x}}</li>
    </ul>
</body>


Controller Complex Array
AppComplex.js
var app = angular.module("myApp", [])
.controller("HomeCtrl", function ($scope) {
    $scope.Friends = [{ Name: 'Shalvin', Location: 'Kochi' },
        {Name:'Pankaj', Location :'NCR'},
        {Name:'Praseed', Location:'Alwaye'}];
});


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myApp">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/AppComplex.js"></script>
</head>
<body ng-controller="HomeCtrl">

    <ul>
        <li ng-repeat="x in Friends">{{x.Name}} located  at {{x.Location}}</li>
    </ul>
</body>
</html>


Friday, January 22, 2016

SQL for Todo

create database ShalvinTodo

use ShalvinTodo

create table Tasks(TaskId int identity(1,1) primary key, TaskName varchar(40), Status varchar(15))

insert into Tasks values ('Wath AngularJS Video', 'Ongoing')
insert into Tasks values ('.Net Saturday Class', 'Ongoing')
insert into Tasks values ('User session', 'Not started')
insert into Tasks values ('Shopping', 'Not started')

select * from Tasks

select * from Tasks where Status = 'Ongoing'

select TaskName from Tasks

select TaskName, Status from Tasks

delete from Tasks where TaskId = 3

update Tasks set Status = 'Finished' where TaskId = 2

Sunday, December 27, 2015

C++ File Handling

Program to create a file and put value to it 

The header file for C++ file handling is fstram.h. We are making use of Output stream (ofstream) for outputting data from Program to file. Here the open method of ofstream class is used for opening the file. It is also possible to use the constructor of ofstream class.


#include <iostream.h>
#include <fstream.h>

int main()
{
    ofstream flout;
    flout.open("marks.dat", ios::out);
    char ans = 'y';
    int rollno;
    float marks;
    while(ans == 'y' || ans == 'Y')
    {
        cout<<"\n Enter Rollno. : ";
        cin>>rollno;
        cout<<"\n Enter Points :";
        cin>>points;
        flout<<rollno<<'\n'<<points<<'\n';
        cout<<"\n Want to enter more records?(y/n)..";
        cin>>ans;

    }
    flout.close();
    return 0;

}


The text file will be created in the bin folder.




Program to input details of 5 students and display them
This progam makes use of ofstream and ifstream classes.



#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main()
{
    system("cls");
    ofstream fout("student", ios::out);
    char name[30], ch;
    float marks = 0.0;

    //Loop to get 5 records
    for(int i = 0; i<5;i++)
    {
        cout<<"Student"<<(i+1)<<":\tName:";
        cin.get(name, 30);
        cout<<"\t\tMarks:";
        cin>>marks;
        cin.get(ch);
        fout<<name<<'\n'<<marks<<'\n';

    }
    fout.close();
    ifstream fin("student", ios::in);
    fin.seekg(0);
    cout<<"\n";

    for(i=0;i<5;i++)
    {
        fin.get(name, 30);
        fin.get(ch);
        fin>>marks;
        fin.get(ch);
        cout<<"Student Name :"<<name;
        cout<<"\tMarks :"<<marks<<"\n";
    }
    fin.close();
    return 0;
}

Writing to and reading from multiple files in succession

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>

int main()
{
    system("cls");
    //Create two files
    ofstream fileout;
    fileout.open("Stunames", ios::out);
    fileout<<"Arundhati \n"<<"Lakshmi \n"<<"Shalvin \\";
    fileout.close();
    fileout.open("stumarks", ios::out);
    fileout<<"100\n"<<"100\n"<<"95\n";
    fileout.close();

    //Read these files
    char line[80];
    ifstream filin;
    filin.open("stunames", ios::in);
    cout<<"The contents of studnames are given below\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.close();

    filin.open("stumarks", ios::in);
    cout<<"\nThe contents of stumarks file are given below\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.close();
    return 0;
}

Display contents of a file using get() function
get() function read a byte of data.


#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main()
{
    system("cls");
    char ch;
    ifstream fin;
    fin.open("marks.dat",  ios::in);
    if(!fin)
    {
        cout<<"Cannot open file !!\n";
        return 1;
    }
    while(fin)
    {
        fin.get(ch);
        cout<<ch;
    }
    fin.close();
    return 0;

}

When end-of-file is reached, the stream associated  with the file becomes zero.

Output



Create a file using put() function
put() function will also write one byte at a time

The following program creates a ASCII chart of printable characters and displays them in 10 column format.


#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main()
{
    system("cls");
    ofstream fout;
    fout.open("Aschars", ios::app);
    if(!fout)
    {
        cout<<"the file cannot be opened!!\n";
        return 1;
    }
    char ch;
    int line = 0;
    for(int i = 33;i<128;i++)
    {
        cout.put(((char)i));
    }
    fout.close();

    ifstream fin;
    fin.open("Aschars", ios::in);
    fin.seekg(0);
    for(i = 33;i<129;i++)
    {
        fin.get(ch);
        cout<<"  "<<i<<"=";
        cout.put((char)(i));
        if(!(i%10))
        {
            cout<<endl;line++;
        }
        if(line >> 22)
        {
            system("PAUSE");
            line = 0;
        }
    }
    return 0;
}

read() and write() functions for working reading and writing entire structure

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
struct customer
{
    char name[51];
    float balance;

};

int main()
{
    system("cls");
    customer savac;
    strcpy(savac.name, "Shalvin P D");
    savac.balance = 10500.50;
    ofstream fout;
    fout.open("Saving", ios::out|ios::binary);
    if(!fout)
    {
        cout<<"File cannot be openened\n";
        return 1;
    }
    fout.write((char *) &savac, sizeof(customer));
    fout.close();

    ifstream fin;
    fin.open("Saving", ios::in|ios::binary);
    fin.read((char *) &savac, sizeof(customer));
    cout<<savac.name;
    cout<<" has the balance amount of Rs. "<<savac.balance<<"\n";
    fin.close();
    return 0;
}

Output