-->

Friday, January 31, 2014

Accept only numbers by the TextBox in ASP.NET

If you want to do it in asp.net that your TextBox accept only number when user input in it. Use CompareField Validator for this type of problem, Normally we use CompareField Validator for password recheck. Today we use it for another purpose, now, take an example.

Accept Only integer type number by TextBox

Step-1 : Add webform into the project.
Step-2 : Take one TextBox and Button control onto the webform.
Step-3 : Also take one Required field and one compare field validator onto the web form.
Step-4 : Set Property of required field validator, these are

ControlToValidate="TextBox1"
Display="Dynamic"
ForeColor="#CC0000"
Text= "Required"

Step-5 : Set Property of CompareField Validator, these are

ControlToValidate="TextBox1"
 Display="Dynamic"
  ForeColor="#CC0000"
  Operator="DataTypeCheck"
   Type="Integer"
   Text= "Enter Only numbers"

Complete code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        Enter age :
        <asp:TextBox ID="TextBox1" runat="server" Width="172px"></asp:TextBox>
&nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="TextBox1" Display="Dynamic" ForeColor="#CC0000">Required</asp:RequiredFieldValidator>

        <asp:CompareValidator ID="CompareValidator1" runat="server" 
            ControlToValidate="TextBox1" Display="Dynamic" ForeColor="#CC0000" 
            Operator="DataTypeCheck" Type="Integer">Enter Only numbers</asp:CompareValidator>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" />
    
    </div>
    </form>
</body>
</html>

Code Generate the following Output

Accept only numbers by the TextBox in ASP.NET

Examples of some common Algorithms of DataStructure in C programming

1.Algorithm to find the average of  a subject marks of ‘N’ number of students.
             AVG_OF_MARKS (LIST, N)
            [LIST is an array containing marks, N is the size of array]
            SUM 0
            Repeat For I=1,2,3, …N
               SUM  SUM  + LIST[i]
            [End of For I]
            AVG SUM/N
            Write: ‘The average is’,AVG
            Exit.

2.Algorithm to find average of SALARY in an array of EMP struct containing information EMPNO, EMPNAME, and SALARY.
        AVG_SALARY (LIST, SIZE)
        [LIST is an array of EMP, SIZE is size of array]
        SUM 0
        Repeat For I=1,2,3, …SIZE
          SUM SUM  + LIST[I].SALARY
       [End of For I]
       AVG  SUM / SIZE
       Write: ‘The average Salary is’,AVG
       Exit.
An algorithm, that performs sub task, may be called in another algorithm. It is better practice to divide the given problem into sub problems and write the individual algorithms to solve such sub problems and collectively write main algorithm to call the sub algorithms in order to solve the main problem.

3.Algorithm to find maximum of two unequal numbers and use the same to find maximum of four unequal numbers.
          MAX_OF_2(NUM1, NUM2)
          [NUM1 and NUM2 are two numbers]
          If NUM1 > NUM2 Then:
                       Return NUM1
          Else:
                        Return NUM2
          [End of If]
          Exit:
          MAX_OF_4(NUM1, NUM2, NUM3, NUM4)
          [NUM1, NUM2, NUM3, and NUM4 are numbers]
          TEMP1 MAX_OF_2(NUM1, NUM2)
           TEMP2  MAX_OF_2(NUM3, NUM4)
            MAX MAX_OF_2(TEMP1, TEMP2)
            Return MAX
            Exit.

4.Algorithm to find minimum of two distinct numbers and using the same to find minimum of three distinct numbers.
            MIN_OF_2(NUM1, NUM2)
            [NUM1 and NUM2 are two number]
             If NUM1<NUM2 Then:
                 Return NUM1
             Else:
                Return NUM2
               [End of If]
               Exit.
               MIN_OF_3(NUM1, NUM2, NUM3)
               [NUM1, NUM2, and NUM3 are distinct number]
                   Return MIN_OF_2(MIN_OF_2(NUM1, NUM2), NUM3)
                Exit.

5.Algorithm to find the GCD (Greatest Common Divisor)
of two positive numbers and use the same to find LCM of two positive numbers.
GCD(NUM1, NUM2)
Rem    NUM1%NUM2 [%Modulus Operator]
Repeat While Rem<>0
  NUM1 <--- NUM2
   NUM2<--- Rem
   Rem <-- NUM1% NUM2
 [End of While]
Return NUM2
Exit. 
LCM(NUM1,NUM2)
  Return(NUM1*NUM2) / GCD (NUM1,NUM2)
Eixt.
Suppose that the LCM algorithm is called using 12 and 16.
The algorithm calls GCD with 12 and 16.

Working of GCD algorithm:
Rem12%16       i.e.  Rem =12
Rem is not equal to Zero. So, NUM1=16 and NUM2=12
Rem16%12       i.e. Rem=4
Rem is not equal to Zero. So, NUM1=12 and NUM2=4
Rem12%4          i.e. Rem=0
Rem is equal to Zero. So, GCD algorithm returns NUM2 that is 4.
When the algorithm LCM gets the result from GCD algorithm it finds the expression value (NUM1 * NUM2) /GCD(NUM1,NUM2).i.e.(12*16) / 4. Which is equal to 48.48 is the LCM of 12 and 16.
Here in this example algorithm, finding LCM is the main problem that can be divided into sub problem like finding the GCD first and using the same to solve the main problem. So, GCD algorithm is written first and in the order algorithm that can be treated as a main algorithm. In this way a given problem can be divided into small problems and algorithms can be written to solve such small problems.

6. Algorithm to find sum of ‘N’ natural numbers between 
N1 and N2.
SUM1(N1,N2)
[N1 and N2 are two different natural numbers]
If N1> N2 Then:
   MAXN1
    MINN2
Else:
    MAXN2
    MINN1
[End of ]
MIN  MIN – 1[to include MIN in sum]
SUM1 (MIN*(MIN +1))/2
SUM2(MAX*(MAX +1))/2
Return SUM2-SUM1
Exit.
In the above algorithm to find the sum of N natural the formula N(N+1)/2 is used. Instead of formula a repetitive statement can also be used that run from MIN to MAX with setp 1. The algorithm can be re-written as:

SUM2(N1,N2)
[N1 and N2 are two different natural numbers]
If N1>N2 Then:
   MAX N2
   MIN N2
Else:
   MAX N2
   MINN1
[End of If]
SUM 0
Repeat For I= MIN, MIN+1, MIN+2 …MAX
    SUMSUM +1 
[End of For]
Return SUM
Exit.

Dry Run:
Suppose that the above algorithm SUM 1 is called with two number 23 and 11. In the If statement 23>11 condition is TRUE, therefore MAX becomes 23 and MIN becomes 11. Now the loop repeats for 11, 12, 13, 14 up to 23 (I values).Every time when the loop is repeated  I  is added to SUM. So, finally the value of SUM is returned from the algorithm. 

Insert element in Array in PHP Programming

Insert element in PHP array:- 
                                                                             If we wish to insert a new item or element in PHP array then we have three choices first is insert the element  from fist position , second is insert the element  at last position and third is insert the element at any desire position in an array. Here following code is showing that how to implement above three operations.



Insert the element from starting of array: - We can do this we use PHP array_unshift() function. Syntax of this function is follows:-
              Int  array_unshift(array array, mixed variable [, mixed variable...])

So we can use any element to the array which is passed in this function  this can be understand with the help of following code.
:
$names = array("jack”,“jon");
array_unshift($states,"weilems","reacher");
after this statement the array name  becomes
$names = array("jon","jacob","rhett","weilems");

So names jack and jon added to the $names array from its starting index and value of index replace by 2 because two new element we added. The same function can be applied with associative array but the key name does not changed.


Insert the element  at last position:- This can be possible in PHP array by using array_push() method. The syntax of this array is
int array_push(array array, mixed variable [, mixed variable...])
So the element is added to the given array and return true if success and return false if failed. This can be understood with the help of following code.

$names = array("weilems"," reacher");
array_push($states," jon "," jack ");
after this statement the array name  becomes
$names = array("jon","jacob","rhett","weilems");

So names weilems and reacher added to the $names array from its end index and value of index increased by 2 because two new element we added. The same function can be applied with associative array.


Insert the element at any desire position:- This is possible if we have sufficient index is available. This can be possible by using direct assignment for example if we want to add a value at fourth index of $name array the we use following statements.

$name [4] =” weilems”;

If the 4th index is available and free then name “weilems” will be add to this index. Or it replaces the value which is already stored at this index.

Thursday, January 30, 2014

How to Querying Data using Joins in Sql Programming: Part 2

In comparison to an inner join, an outer join displays the result set containing all the rows from one table and the matching rows from another table. For example, if you create an outer join on Table A and Table B, it will show you all the records of Table A and only those records from Table B for which the condition on the common column holds true.

An outer join displays NULL for the columns of the related table where it does not find matching records. The syntax of applying an outer join is:

SELECT column_name, column_name [, column_name]
FROM table1_name [LEFT | RIGHT| FULL] OUTER JOIN table2_name
ON table_name.ref_column_name

An outer join is of three types:

Left Outer Join

A left outer join returns all rows from the table specified on the left side of the LEFT OUTER JOIN keyword and the matching rows from the table specified on the right side. The rows in the table specified on the left side for which matching rows are not found in the table specified on the right side, NULL values are displayed in the column that get data from the table specified on the right side.

Consider an example. The SpecialOfferProduct table contains a list of products that are on special offer. The SalesOrderDetail table stores the details of all the sales transactions. The users at AdventureWorks need to view the transaction details of these products. In addition, they want to view the ProductID of the special offer products for which no transaction has been done.

To perform this task, you can use the LEFT OUTER JOIN keyword, as shown in the following query:

Select p.ProductID, q.SalesOrderID, q.UnitPrice
From Sales.SpecialOfferProduct p
Left outer join Sales.SalesOrderDetail q on p.ProductID= q.ProductID
where SalesOrderID is NULL

The following figure displays the output of the preceding query.

How to Querying Data using Joins in Sql Programming: Part 2

Right Outer Join

A right outer join returns all the rows form the table specified on the right side of the RIGHT OUTER JOIN keyword and the matching rows from the table specified on the left side.

Consider the example of Adventure Works. Inc. The JobCandidate table stores the details of all the job candidates. You need to retrieve a list of all the job candidates. In addition, you need to find which candidate has been employed in Adventure Works, Inc. To perform this task, you can apply a right outer join between the Employee and JobCandidate tables, as shown in the following query:

SELECT e.JobTitle, d.JobCandidateID
FROM HumanResources.Employee e
RIGHT OUTER JOIN HumanResources.JobCandidate d
ON e.BusinessEntityID=d.BusinessEntityID

The result set displays the JobCandidateID column from the JobCandidate table and the BusinessEntityID column from the matching rows of the Employee table.

How to Querying Data using Joins in Sql Programming: Part 2

Full Outer Join

A full outer join is a combination of left outer join and right outer join. This join returns all the matching and non-matching rows from both the tables. However, the matching records are displayed only once. In case of non-matching rows, a NULL value is displayed for the columns for which data is not available.

SELECT e.BusinessEntityID, e.EmployeeName,ed.EmployeeEducationCode,
ed. Education
FROM Employee e FULL OUTER JOIN Education ed
ON e.EmployeeEducationCode = ed.EmployeeEducationCode

According to this query, the employee details and their highest educational qualification is displayed. For non-matching values, NULL is displayed.

Reference Data Types in Java Programming

Any type of storage that stores an address or we can say reference of object in memory, called Reference data type. Java like other language have also some reference data types discussed in the article.

Broadly a reference in Java is a data element whose value is an address of a memory location. Arrays (a group of similar data items), classes (a blue print of some entity e.g. a student) and interfaces are reference type. The value of reference type variable, in contrast to that of primitive type, is a reference to (an address of) the value or set of values represented by variables.

Reference Data Types in Java Programming

A reference is called a pointer, or memory address in other language. The java programming language does not support the explicit use of addresses like other languages do. You use variable’s name instead.

An importance reference type that you use in Java is String type. The String type lets you create variables that can hold textual data e.g. “Mohan”, TZ194”, “194ZB”, “234” etc. Anything that you enclose in double quotes is treated as text in Java.

Create PHP Array in PHP Programming

Create PHP array :-
                               We have following methods to create an array in PHP.
To assign index and key manually to the variable: - In this method we have to provide a specific index value as well as key value to the variable. This can be understood with the help of example.
Let we want an array named “record” to store different information of students such as name, age , id, and marks. Here the variable “record” act as an array and store all these information in following way.

In this example we use an array variable $record to store different elements. Now we have following properties this PHP array.
Index range for this array is 0-3.where minimum index is 0 and maximum index is 3;
Maximum item that this array can store is 4.
A PHP array can store different type of elements like integer or string or double etc. because php takes the type of data automatically corresponding to that.
In above program we use the function print_r() which is used to print values store in array variable.






In this example we used a key in stand of index. PHP provide this facility to make the array calculation much easy and user-friendly. The array which uses keys to hold the value is called associative array. 
To use range() function:- If we want to create an array  with a specific range like for integer such as 100-500 or for float 4.6-90.6 or for string like b-k then we can create an array by using range function. Syntax of this method is following
Range (string value, ending value, difference);
This could be understood with the help of example.









To use array () function: - This functions is used to make an array in php. The syntax of this fictional is follows.
Array array ([ mixed $... ] )
This array function takes mixed type of variables and prepares an array for us.
This can be understand with the help of following codes.
$name=array(“jack”, “jon”,”weilems”);
Or this can be use for associative array such as:-
$record=array(name->”jack” ,roll->5, marks->45.9, address->”new york”);

How to use TextChanged event of TextBox with AutoPostBack in ASP.NET


ASP.NET application works on AutoPostBack model, i mean, if we want to run code behind event like TextChanged event then we must set AutoPostBack property is true.
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Search Item<asp:TextBox ID="TextBox1" runat="server" 
            AutoPostBack="True" Width="174px" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
        <br />
        <br />
    </div>
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>
Code Behind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        string[] arr = { "jacob", "lefore", "martin" };

        foreach (string item in arr)
{
if (item==TextBox1 .Text)
        {
            Label1.Text = item;

        } 
}
        
    }
}

Output

This example shows that TextChanged event occurs when user focus out from the TextBox. You can search item on TextChanged event.
© Copyright 2013 Computer Programming | All Right Reserved