Thursday, 21 April 2016
Sunday, 17 April 2016
Find Out the Parameter value from the Query String in the URL using JavaScript
How to find Out the Parameter value from the Query String in the URL?
Here is the Code to do this-
function GetParameterValues(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
var urlparam = url[i].split('=');
if (urlparam[0].toLowerCase() == param.toLowerCase()) {
return urlparam[1];
}
}
}
Use:
<script>
var name = GetParameterValues('name');
var id = GetParameterValues('ID');
alert("Hello " + name + " your ID is " + id);
</script>
Create HTML Table dynamically with the JSON Dataset available using Javascript
Create HTML Table dynamically with the JSON Dataset available using Javascript
Here is the Code to achieve this:-function CreateTable(data) {
var parentDivTable = document.getElementById('tblAllEmployee');
var table = document.createElement('table');
table.setAttribute('border', '1');
table.setAttribute('class', 'CSSTableGenerator');
var thead = document.createElement('thead');
var tbody = document.createElement('tbody');
var tr = document.createElement('tr');
var td = document.createElement('td');
var header = ['Employee Id', 'Employee Name', 'Contact Number'];
var i = 0;
for (i = 0; i < header.length; i++)
{
td = document.createElement('th');
td.innerHTML = header[i];
tr.appendChild(td);
}
thead.appendChild(tr);
for (i = 0; i < data.length; i++)
{
tdEmpId = document.createElement('td');
tdEmpId.innerHTML = data[i].EmployeeId;
tdEmpName = document.createElement('td');
tdEmpName.innerHTML = data[i].Name;
tdContactNumber = document.createElement('td');
tdContactNumber.innerHTML = data[i].ContactNumber;
tr = document.createElement('tr');
tr.appendChild(tdEmpId);
tr.appendChild(tdEmpName);
tr.appendChild(tdContactNumber);
tbody.appendChild(tr);
}
table.appendChild(thead);
table.appendChild(tbody);
while (parentDivTable.hasChildNodes())
{
parentDivTable.removeChild(parentDivTable.firstChild);
}
parentDivTable.appendChild(table);
}
Here "data" is the JSON dataset or arrays, "tblAllEmployee" is the Div Id under which the HTML table will be created, "CSSTableGenerator" is the class for the table.
Consume REST Api or Web Api using Jquery for all CRUD(Create, Read, Update, Delete) Operations
Consume ASP.NET Web Api using Jquery for all CRUD(Create, Read, Update, Delete) Operations
Here is the HTML Codes:-
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Employee</title>
<script src="Scripts/bootstrap.js"></script>
<script src="Scripts/bootstrap.min.js"></script>
<script src="Scripts/modernizr-2.6.2.js"></script>
<script src="Scripts/jquery-1.10.2.intellisense.js"></script>
<script src="Scripts/jquery-1.10.2.js"></script>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="Scripts/respond.js"></script>
<script src="Scripts/respond.min.js"></script>
<style type="text/css">
.CSSTableGenerator {
margin: 0px;
padding: 0px;
width: 100%;
box-shadow: 10px 10px 5px #888888;
border: 1px solid #000000;
-moz-border-radius-bottomleft: 0px;
-webkit-border-bottom-left-radius: 0px;
border-bottom-left-radius: 0px;
-moz-border-radius-bottomright: 0px;
-webkit-border-bottom-right-radius: 0px;
border-bottom-right-radius: 0px;
-moz-border-radius-topright: 0px;
-webkit-border-top-right-radius: 0px;
border-top-right-radius: 0px;
-moz-border-radius-topleft: 0px;
-webkit-border-top-left-radius: 0px;
border-top-left-radius: 0px;
}
.CSSTableGenerator table {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
.CSSTableGenerator tr:last-child td:last-child {
-moz-border-radius-bottomright: 0px;
-webkit-border-bottom-right-radius: 0px;
border-bottom-right-radius: 0px;
}
.CSSTableGenerator table tr:first-child td:first-child {
-moz-border-radius-topleft: 0px;
-webkit-border-top-left-radius: 0px;
border-top-left-radius: 0px;
}
.CSSTableGenerator table tr:first-child td:last-child {
-moz-border-radius-topright: 0px;
-webkit-border-top-right-radius: 0px;
border-top-right-radius: 0px;
}
.CSSTableGenerator tr:last-child td:first-child {
-moz-border-radius-bottomleft: 0px;
-webkit-border-bottom-left-radius: 0px;
border-bottom-left-radius: 0px;
}
.CSSTableGenerator tr:hover td {
}
.CSSTableGenerator tr:nth-child(odd) {
background-color: #ffaa56;
}
.CSSTableGenerator tr:nth-child(even) {
background-color: #ffffff;
}
.CSSTableGenerator td {
vertical-align: middle;
border: 1px solid #000000;
border-width: 0px 1px 1px 0px;
text-align: left;
padding: 7px;
font-size: 10px;
font-family: Arial;
font-weight: normal;
color: #000000;
}
.CSSTableGenerator tr:last-child td {
border-width: 0px 1px 0px 0px;
}
.CSSTableGenerator tr td:last-child {
border-width: 0px 0px 1px 0px;
}
.CSSTableGenerator tr:last-child td:last-child {
border-width: 0px 0px 0px 0px;
}
.CSSTableGenerator tr:first-child td {
background: -o-linear-gradient(bottom, #ff7f00 5%, #bf5f00 100%);
background: -webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ff7f00), color-stop(1, #bf5f00) );
background: -moz-linear-gradient( center top, #ff7f00 5%, #bf5f00 100% );
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#ff7f00", endColorstr="#bf5f00");
background: -o-linear-gradient(top,#ff7f00,bf5f00);
background-color: #ff7f00;
border: 0px solid #000000;
text-align: center;
border-width: 0px 0px 1px 1px;
font-size: 14px;
font-family: Arial;
font-weight: bold;
color: #ffffff;
}
.CSSTableGenerator tr:first-child:hover td {
background: -o-linear-gradient(bottom, #ff7f00 5%, #bf5f00 100%);
background: -webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ff7f00), color-stop(1, #bf5f00) );
background: -moz-linear-gradient( center top, #ff7f00 5%, #bf5f00 100% );
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#ff7f00", endColorstr="#bf5f00");
background: -o-linear-gradient(top,#ff7f00,bf5f00);
background-color: #ff7f00;
}
.CSSTableGenerator tr:first-child td:first-child {
border-width: 0px 0px 1px 0px;
}
.CSSTableGenerator tr:first-child td:last-child {
border-width: 0px 0px 1px 1px;
}
</style>
</head>
<body>
<div class="container">
<div class="row" id="AllEmployees">
<div class="col-md-12">
<h3> Get All Employees</h3>
</div>
<div class="col-md-12">
<input type="button" id="btnGetAllEmployees" value="Go" />
</div>
<div class="col-md-12" id="tblAllEmployee">
</div>
</div>
<div class="row" id="SingleEmployee">
<div class="col-md-12">
<h3> Get Employee by Id</h3>
</div>
<div class="col-md-6">
<input type="text" id="txtSearchEmpId" />
</div>
<div class="col-md-6">
<input type="button" id="btnGetEmployeeById" value="Go" />
</div>
<div class="col-md-12" id="singleEmp">
<div class="col-md-12">
<span>Employee Id: </span><span id="spnEmpId"></span>
</div>
<div class="col-md-12">
<span>Employee Name: </span><span id="spnEmpName"></span>
</div>
<div class="col-md-12">
<span>Contact Number: </span><span id="spnContact"></span>
</div>
</div>
</div>
<div class="row" id="CreateEmployee">
<div class="col-md-12">
<h3> Add Employee</h3>
</div>
<div class="col-md-12" id="singleEmp">
<div class="col-md-12">
<span>Employee Id: </span><span id="spnEmpId"></span>
</div>
<div class="col-md-12">
<span>Employee Name: </span><span id="spnEmpName"><input type="text" id="txtEmpName" /></span>
</div>
<div class="col-md-12">
<span>Contact Number: </span><span id="spnContact"><input type="text" id="txtEmpContact" /></span>
</div>
<div class="col-md-12">
<input type="button" id="btnAddEmployee" value="Create" />
</div>
</div>
</div>
<div class="row" id="UpdateEmployee">
<div class="col-md-12">
<h3> Update Employee</h3>
</div>
<div class="col-md-12" id="singleEmp">
<div class="col-md-12">
<span>Employee Id: </span><span id="spnEmpId"><input type="text" id="txtEmpId" /></span>
</div>
<div class="col-md-12">
<span>Employee Name: </span><span id="spnEmpName"><input type="text" id="txtEmpName" /></span>
</div>
<div class="col-md-12">
<span>Contact Number: </span><span id="spnContact"><input type="text" id="txtEmpContact" /></span>
</div>
<div class="col-md-12">
<input type="button" id="btnUpdateEmployee" value="Update" />
</div>
</div>
</div>
<div id="DeleteEmployee" class="row">
<div class="col-md-12">
<h3> Delete Employee by Id</h3>
</div>
<div class="col-md-6">
<input type="text" id="txtSearchEmpId" />
</div>
<div class="col-md-6">
<input type="button" id="btnDeleteEmployeeById" value="Delete" />
</div>
<div class="col-md-12" id="msg">
</div>
</div>
</div>
<script src="Scripts/Employee.js"></script>
</body>
</html>
Here is the .js file Codes:-
$(document).ready(function () {
var view = GetParameterValues('View');
//var id = GetParameterValues('ID');
//alert("Hello " + name + " your ID is " + id);
switch(view.toLowerCase())
{
case 'getall':
$('#SingleEmployee').remove();
//$('#SingleEmployee').find('*').prop('disabled', true);
$('#CreateEmployee').remove();
// $('#CreateEmployee').find('*').prop('disabled', true);
$('#UpdateEmployee').remove();
//$('#UpdateEmployee').find("*").prop('disabled',true);
$('#DeleteEmployee').remove();
//$('#DeleteEmployee').find("*").prop('disabled',true);
$('#AllEmployees').show();
break;
case 'get':
$('#SingleEmployee').show();
$('#CreateEmployee').remove();
$('#UpdateEmployee').remove();
$('#DeleteEmployee').remove();
$('#AllEmployees').remove();
break;
case 'create':
$('#SingleEmployee').remove();
$('#CreateEmployee').show();
$('#UpdateEmployee').remove();
$('#DeleteEmployee').remove();
$('#AllEmployees').remove();
break;
case 'update':
$('#SingleEmployee').remove();
$('#CreateEmployee').remove();
$('#UpdateEmployee').show();
$('#DeleteEmployee').remove();
$('#AllEmployees').remove();
break;
case 'delete':
$('#SingleEmployee').remove();
$('#CreateEmployee').remove();
$('#UpdateEmployee').remove();
$('#DeleteEmployee').show();
$('#AllEmployees').remove();
break;
default:
$('#SingleEmployee').show();
$('#CreateEmployee').show();
$('#UpdateEmployee').show();
$('#DeleteEmployee').show();
$('#AllEmployees').show();
break;
}
$('#btnGetEmployeeById').click(function () {
var request = $.ajax({
url: 'http://localhost:55718/api/Employee/GetEmployeeDetails',
type: 'GET',
data: { id: $('#txtSearchEmpId').val() },
});
request.success(function (result) {
console.log(JSON.stringify(result));
$('#spnEmpId').html(result.EmployeeId);
$('#spnContact').html(result.ContactNumber);
$('#spnEmpName').html(result.Name);
});
request.error(function (jqXHR, textStatus) {
alert('An error occurred ' + JSON.stringify(jqXHR) + textStatus);
});
request.fail(function (jqXHR, textStatus) {
alert('Request failed ' + JSON.stringify(jqXHR) + textStatus);
});
});
$('#btnGetAllEmployees').click(function () {
$.ajax({
url: 'http://localhost:55718/api/Employee/GetEmployees',
type:'GET',
data:{},
success: function(result)
{
console.log(JSON.stringify(result));
CreateTable(result);
},
error:function(jqXHR,textStatus){
alert('An error occurred ' + JSON.stringify(jqXHR) + textStatus);
}
});
});
$('#btnAddEmployee').click(function () {
var isValidationPassed = true;
var name = $('#txtEmpName').val();
var contact = $('#txtEmpContact').val();
if ((name == '' || name == null || name == 'undefined') && (contact == '' || contact == null || contact == 'undefined')) {
isValidationPassed = false;
alert('Please enter Employee Name & Contact Number.');
}
else {
var regExpr = new RegExp('/^[a-zA-Z]+$/', 'g');
//if (regExpr.test(name)) {
// isValidationPassed = true;
//}
//else {
// isValidationPassed = false;
// alert('Please enter only alphabets in the Employee Name field.');
//}
//regExpr = new RegExp('/^\d+$/','g');
//if (contact.match(regExpr)) {
// isValidationPassed = true;
//}
//else {
// isValidationPassed = false;
// alert('Please enter only number in the Contact Number field.');
//}
isValidationPassed = true;
}
var model = {
'Name': name,
'ContactNumber': contact,
'EmployeeId': ''
};
if (isValidationPassed) {
$.ajax({
url: 'http://localhost:55718/api/Employee/AddEmployee',
type: 'POST',
data: model,
success: function (result) {
alert(result);
},
error: function (jqXHR, textStatus) {
alert('An error occurred ' + JSON.stringify(jqXHR) + textStatus);
}
});
}
});
$('#btnUpdateEmployee').click(function () {
var model = {
'Name': $('#txtEmpName').val(),
'ContactNumber': $('#txtEmpContact').val(),
'EmployeeId': $('#txtEmpId').val()
};
$.ajax({
url: 'http://localhost:55718/api/Employee/UpdateEmployeeDetails',
type: 'POST',
data: model,
success: function (result) {
alert(result);
},
error: function (jqXHR, textStatus) {
alert('An error occurred ' + JSON.stringify(jqXHR) + textStatus);
}
});
});
$('#btnDeleteEmployeeById').click(function () {
var searchEmpId = $('#txtSearchEmpId').val();
$.ajax({
type: 'POST',
url: 'http://localhost:55718/api/Employee/DeleteEmployee?' + jQuery.param({ id: searchEmpId }),
contentType:'application/json; charset=utf-8',
data: jQuery.param({ id: searchEmpId }),
dataType:'json',
success: function (result) {
alert(result);
//location.href = 'Employee.html?view=getall';
},
error: function (jqXHR, textStatus) {
alert('An error occurred ' + JSON.stringify(jqXHR) + textStatus);
}
});
});
});
function GetParameterValues(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
var urlparam = url[i].split('=');
if (urlparam[0].toLowerCase() == param.toLowerCase()) {
return urlparam[1];
}
}
}
function CreateTable(data) {
var parentDivTable = document.getElementById('tblAllEmployee');
var table = document.createElement('table');
table.setAttribute('border', '1');
table.setAttribute('class', 'CSSTableGenerator');
var thead = document.createElement('thead');
var tbody = document.createElement('tbody');
var tr = document.createElement('tr');
var td = document.createElement('td');
var header = ['Employee Id', 'Employee Name', 'Contact Number'];
var i = 0;
for (i = 0; i < header.length; i++)
{
td = document.createElement('th');
td.innerHTML = header[i];
tr.appendChild(td);
}
thead.appendChild(tr);
for (i = 0; i < data.length; i++)
{
tdEmpId = document.createElement('td');
tdEmpId.innerHTML = data[i].EmployeeId;
tdEmpName = document.createElement('td');
tdEmpName.innerHTML = data[i].Name;
tdContactNumber = document.createElement('td');
tdContactNumber.innerHTML = data[i].ContactNumber;
tr = document.createElement('tr');
tr.appendChild(tdEmpId);
tr.appendChild(tdEmpName);
tr.appendChild(tdContactNumber);
tbody.appendChild(tr);
}
table.appendChild(thead);
table.appendChild(tbody);
while (parentDivTable.hasChildNodes())
{
parentDivTable.removeChild(parentDivTable.firstChild);
}
parentDivTable.appendChild(table);
}
Execute .bat file in a remote Computer using C#
How to Execute a batch file or script in a remote Computer or Server using C#?
Download the sample program file here.
Here are the steps:-
Here are the steps:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
namespace RemoteApplicationExecution
{
class Program
{
static void Main(string[] args)
{
try
{
ConnectionOptions options =new ConnectionOptions();
options.Authority = "ntlmdomain:INDIA";
options.Username = "740471";
options.Password = "Apr@2016";
ManagementScope scope =
new ManagementScope(
"\\\\01hw894947\\root\\cimv2",options);
var processToRun = new[] { @"C:\test\Start-Stop.bat" };
scope.Connect();
//Query system for Operating System information
ObjectQuery query = new ObjectQuery(
"SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
// Display the remote computer information
Console.WriteLine("Computer Name : {0}",
m["csname"]);
Console.WriteLine("Windows Directory : {0}",
m["WindowsDirectory"]);
Console.WriteLine("Operating System: {0}",
m["Caption"]);
Console.WriteLine("Version: {0}", m["Version"]);
Console.WriteLine("Manufacturer : {0}",
m["Manufacturer"]);
}
var wmiProcess = new ManagementClass(scope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
wmiProcess.InvokeMethod("Create", processToRun);
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + Environment.NewLine + ex.Source + Environment.NewLine + ex.InnerException + ex.StackTrace);
throw ex;
}
}
}
}
Sunday, 6 September 2015
Allow only numer input in textbox using jquery
Allow only numer input in textbox
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
var textIdList= ["#tbx", "#tbx1", "#tbx2"];
allowNumberOnly(textIdList);
});
function allowNumberOnly(textIdList){
alert(1);
for(i=0; i<textIdList.length; i++)
{
alert(textIdList[i]);
abc(textIdList[i]);
}
}
function abc(id){
$(function () {
alert(2);
$(id).bind("keypress", function (e) {
var keyCode = e.which ? e.which : e.keyCode
var result = (keyCode >= 48 && keyCode <= 57);
return result;
});
// Restricting the Paste into textbox
$(id).bind("paste", function (e) {
return false;
});
// Restricting the Cut from textbox
$(id).bind("cut", function (e) {
return false;
});
// Restricting the Copy from textbox
$(id).bind("copy", function (e) {
return false;
});
// Restricting the drag and drop any value into textbox
$(id).bind("drop", function (e) {
return false;
});
});
}
</script>
<style>
.important {
font-weight: bold;
font-size: xx-large;
}
.blue {
color: blue;
}
</style>
</head>
<body>
<h2 class="test">This is a heading</h2>
<p class="test">This is a paragraph.</p>
<p>This is another paragraph.</p>
<input type="text" class="blue important" id="tbx"></input>
<input type="text" class="blue important" id="tbx1"></input>
<input type="text" class="blue important" id="tbx2"></input>
</body>
</html>
HTML5 storage
HTML local storage, better than cookies.
What is HTML Local Storage?
With local storage, web applications can store data locally within the user's browser.
Before HTML5, application data had to be stored in cookies, included in every server request. Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance.
Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.
Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.
HTML Local Storage Objects
HTML local storage provides two objects for storing data on the client:
- window.localStorage - stores data with no expiration date
- window.sessionStorage - stores data for one session (data is lost when the browser tab is closed)
Before using local storage, check browser support for localStorage and sessionStorage:
if(typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
} else {
// Sorry! No Web Storage support..
}
// Code for localStorage/sessionStorage.
} else {
// Sorry! No Web Storage support..
}
The localStorage Object
The localStorage object stores the data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.
Example
// StorelocalStorage.setItem("lastname", "Smith");
// Retrievedocument.getElementById("result").innerHTML = localStorage.getItem("lastname");
// Retrievedocument.getElementById("result").innerHTML = localStorage.getItem("lastname");
Try it Yourself »
Example explained:
- Create a localStorage name/value pair with name="lastname" and value="Smith"
- Retrieve the value of "lastname" and insert it into the element with id="result"
The example above could also be written like this:
// Store
localStorage.lastname = "Smith";
// Retrieve
document.getElementById("result").innerHTML = localStorage.lastname;
localStorage.lastname = "Smith";
// Retrieve
document.getElementById("result").innerHTML = localStorage.lastname;
The syntax for removing the "lastname" localStorage item is as follows:
localStorage.removeItem("lastname");
Note: Name/value pairs are always stored as strings. Remember to convert them to another format when needed!
The following example counts the number of times a user has clicked a button. In this code the value string is converted to a number to be able to increase the counter:
Example
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
localStorage.clickcount + " time(s).";
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
localStorage.clickcount + " time(s).";
Try it Yourself »
The sessionStorage Object
The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. The data is deleted when the user closes the specific browser tab.
The following example counts the number of times a user has clicked a button, in the current session:
Example
if (sessionStorage.clickcount) {
sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
sessionStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
sessionStorage.clickcount + " time(s) in this session.";
sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
sessionStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
sessionStorage.clickcount + " time(s) in this session.";
Try it Yourself »
Subscribe to:
Posts (Atom)