-->

Wednesday, March 5, 2014

HTML server control classes in ASP.NET

ASP.NET uses the HTML server control classes to work with the HTML controls using a programming language. These classes include the HtmlTextArea class for the HTML <textarea> controls, the HtmlInputButton class for HTML buttons created with the <input type="button"> elements, the HtmlInputText class for HTML text fields created with the <input type="text"> elements, and the rest. while working with HTML server control, you are actually working with their supporting classes. You can even work with these classes directly in code.
Most of the attributes of HTML server controls can be specified through the properties window of IDE. These properties are written in lowercase. As soon as you change or specify the value of these properties, the corresponding changes are made to the HTML element in the .aspx page. For example, you can set the caption of an HTML server button at design-time using the Value property, which corresponds to the HTML <Input> element's value attribute. However, at runtime, this attribute is supported by the HtmlInputButton class's Value property. When we work with an object of the HtmlInputButton class on the server, the casing (upper or lower case) of property name in server code adheres to the coding language standard.
It is important to know that the HTML server control classes exactly correspond to the actual HTML controls that you see in the HTML tab in ToolBox. For example, the HtmlInputButton class is used not just for buttons but also for reset buttons. All these buttons are created using an HTML <input> element, and they differ only through the attribute named type; for example, a standard button uses <input type="button">, a reset button uses <input type="reset">, and a submit button uses <input type="submit">.

Note : As per HTML rules, the HTML controls must be in the HTML form(with .htm or .html extension) while they are sent back to the server. However, when you create a web form using the ASP.NET IDE, you need not require to create the HTML form explicitly; ASP.NET does that automatically as soon as you add a new web form to the website.

HTML classes are based on the HtmlControl class. Some server control class are listed in

HtmlAnchor : Creates an <a> element for navigation.

HtmlButton : Creates an HTML button using the <button> element.

HtmlForm : Allows access to the <form> HTML element which is a container for other controls on a web page.

HtmlgenericControl : Defines the methods, properties and events for all HTML server controls not represented by .NET Framework.

HtmlImage : Maps the <img> element for displaying image on a web page.

HtmlInputButton : Creates an Html button using the <input> element.

HtmlInputCheckBox : Creates an Html checkbox.

HtmlInputControl : Defines the methods, properties and events for HTML input controls.

HtmlInputFile : Creates an HTML file upload control.

HtmlInputHidden : Creates an HTML input hidden control.

HtmlInputImage : Creates an HTML button that display image.

HtmlInputRadioButton : Creates an HTML radio button.

HtmlInputText : Creates an HTML text field. You can also use this control to create password fields.

HtmlSelect : Maps <select> element and allow to create list control.

HtmlTable : Creates an HTML table.

HtmlTableCell : Creates an HTML cell in a Table.

Creating grouped elements and attributes in XML

The purpose of an XML schema is to define the building blocks of an XML document. An XML schema defines the following in an XML document:

  • Elements
  • Attributes
  • Child elements 
  • Order of the child elements
  • Number of child elements
  • State of the element, whether it is empty or includes text
  • Data types for the elements and attributes 
  • Default and fixed values for the elements and attributes

An XML schema enables you to combine related elements and attributes into groups. This Feature of creating grouped elements and attributes  enables you to perform the following tasks:
  • Create a reusable group of elements and attributes:  A reusable group can be used to specify the content  model for a complex type of element or attribute. This eliminates the task of declaring the elements that have already been declared in some other context. For example, you can create a group of the FIRSTNAME and LASTNAME elements and reuse the group for customers, suppliers, and employees.
  • Select a single element from a group: Assume that you want to use one element or Attribute, such as the phone number of an employee, from a group. To do so, you can create a group of such elements and allow one of them to be used in the XML document.
  • Specify the sequence of elements: You can create a group of  elements and specify the sequence in which each element in the group should appear in the XML document.
  • XSD provides the following  elements to group user-defined elements and attributes:

  1. sequence
  2. group
  3. choice
  4. all
  5. attributeGroup

The sequence Element

The sequence element ensures that the elements declared within the opening and closing tags  of this element appear in a specific order. For example, to ensure that the employee first name, last name, designation, and department appear in a specific order in an XML
document, you can use the following statements in an XML schema:

<xsd:schema  xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
     <xsd:element name =”EMPLOYEE” type=”emptype”/>
     <xsd:complexType name =”empType name=”emptype”>
             <xsd:sequence>
                    <xsd:element name=”FIRSTNAME” type=”xsd:string”/>
                    <xsd:element name=”LASTNAME” type=”xsd:string”/>
                    <xsd:element name=”DESIGN”  type=”xsd:string”/>
                    <xsd:element name=”DEPARTMENT” type=”xsd:string”/>
             </xsd:sequence>
        </xsd:complexType>
</xsd:schema>

In preceding schema, the EMPLOYEE element is associated with the complex type emptype element. This complex type element contains the declarations for the FIRSTNAME, LASTNAME, DESIG, and DEPARTMENT elements. All these declarations are contained within the sequence element. This ensure that the elements appear in the same sequence in which they are declared.

Therefore, if you validate the following XML document against the preceding schema, it will result in an error because the DEPARTMENT element should come after the DESIG
element according to the schema.

<?xml version=”1.0”?>
<EMPLOYEE>
         <FIRSTNAME> James </FIRSTNAME>
         <LASTNAME > Williams <LASTNAME>
         <DEPARTMENT> MKTG </DEPARTMENT>
         <DESIG> MKTG EXEC </DESIG>
</EMPLOYEE>

The group Element

A set of elements can be grouped together by a common name in an XML schema, and incorporated into a complex data type. Grouping of elements is beneficial when you want a set of related elements to be referred using a common name.
The syntax for declaring a group element is as follows:

<group maxOccurs=”nonNegativeInteger | unbounded”
minOccurs=”nonNegativeInteger” name=”NCName” ref=”QName “> </group>

The following table describes the attributes of the group element.
Attribute
Description
maxOccurs
Used to specify the maximum of times a group can occur in the
XML document. The value of the maxOccurs attribute must be an
integer greater than or equal to zero. If you do not want to set a limit on
the maximum number, you can specify the value of this attribute as
unbounded.
minOccurs
Used to specify the minimum number of times a group can occur in the
XMLdocument. The value of the minOccurs attribute must be an
integer greater than or equal to zero to specify that the group is optional.
name
Used to assign a name for the group element. The name assigned to
the group must not contain a colon.
ref
Used to refer to a group in a complex type element.
Consider the following example:
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
       <xsd:group name=”empname”>
              <xsd:sequence>
                     <xsd:element name=”FIRSTNAME” type=”xsd:string”/>
                     <xsd:element name=”LASTNAME” type=”xsd:string/>
                </xsd:sequence>
          </xsd:group>
<xsd:element name=”EMPLOYEE” type=”emptype”/>
<xsd:ComplexType name=”emptype”>
        <xsd:sequence>
               <xsd:group ref=”empname”/>
               <xsd:element name=”ADDRESS” type=”xsd:string”/>
         </xsd:sequence>
</xsd:complexType>
</xsd:schema>

In the preceding example, the FIRSTNAME and LASTNAME elements are grouped together by a common name called empname by using the group element. The order in which these elements must appear in the XML document is specified using the sequence element. You can refer to the empname group while creating a complex type by using the following Statement:

<xsd:group ref=”empname”/>

After declaring the elements are attributes in the preceding schema, you can create an XML document that conforms to the schema by using the following code snippet:

<?xml version=”1.0?>
<EMPLOYEE>
          <FIRSTNAME> Sam </FIRSTNAME>
<LASTNAME> Peterson </LASTNAME>
<ADDRESS> 10, LIONS STREET, BOSTON </ADDRESS>
</EMPLOYEE>

The choice Element      

XSD enables you to select a a single option from multiple options by using the choice element. The choice element allows only one of the elements contained in the group to be present within the parent element.

The syntax for declaring a choice element is as follows:

<choice id=”ID” maxOccurs=”nonNegativeInteger | unbounded”
minOccurs=”nonNegativeInteger”> </choice>

In the preceding syntax, id, maxOccurs, and minOccurs are attributes of the choice element. These attributes are similar to the attributes of the group element mentioned earlier.

Consider the following example. You may want to store either the office address or the residential  address of a customer in an XML document. You can implement this option by using the choice element, as shown in the following code:

<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
        <xsd:element name=”CUSTOMER” type=”custtype”/>
        <xsd:complexType name=”custtype”>
               <xsd:sequence>
                      <xsd:group ref=”custname”/>
                      <xsd:element name=”ADDRESS” type=”addtype”/>
               </xsd:sequence>
                </xsd:complexType>
                <xsd:complexType name=”addtype”>
                       <xsd:choice>
                              <xsd:element name=”RESIDENCE” type=”xsd:string”/>
                              <xsd:element name=”OFFICE” type=”xsd:string”/>
                        </xsd:choice>
                  </xsd:complexType>
                  <xsd:group name=”custname”>
                         <xsd:sequence>
                                <xsd:element name=”FIRSTNAME” type=”xsd:string”/>
                                <xsd:element name=”LASTNAME” type=”xsd:string/>
                          </xsd:sequence>
                     </xsd:group>
               </xsd:schema> 

In the preceding code, the CUSTOMER element is declared with a reference to the complex
Type, custtype. This type further refers to the custname group, which contains the Declarations for the FIRSTNAME and LASTNAME elements. The custtype complex type also contains the declaration for the ADDRESS element, which refers to the complex type, addtype. This complex type contains the declerations for the RESIDENCE and OFFICE elements. These declerations appear within the choice element. Therefore, only one of these elements can be used at a time within the ADDRESS element.

The following XML doument conforms to the preceding schema:

<?xml version=”1.0”?>
<CUSTOMER>
        <FIRSTNAME> Sam </FIRSTNAME>
        <LASTNAME> Peterson </LASTNAME>
         <ADDRESS>
                  <RESIDENCE>10, LIONS STREET, BOSTON </RESIDENCE>
          </ADDRESS>
         </CUSTOMER>

If you include the OFFICE element within the preceding code, it will result in an error During document validation.

The all Element

In contrast to the sequence element, the all element enables you to use the child elements in any order.

The syntax for using the all element is as follows:

        <all maxOccurs=”posiveInteger” minOccurs=”0|1”> </all>
In the preceding syntax, the maxOccurs and minOccurs attributes have the same meaning as the maxOccurs and minOccurs element elements of the group element.

The following code contains the declaration for the EMPLOYEE element and its child elements. The code is the same as the one given for the group element. However, the sequence element has been replaced by the all element here.

<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xsd:element name=”EMPLOYEE” type=”emptype” />
       <xsd:complexType name=”emptype”>
              <xsd:all>
                      <xsd:element name=”FIRSTNAME” type=”xsd:string”/>
                      <xsd:element name=”LASTNAME” type=”xsd:string”/>
                      <xsd:element name=”DESIG” type=”xsd:string/>
                      <xsd:element name=”DEPARTMENT” type=”xsd:string/>
                </xsd:all>
          </xsd:complexType>
    </xsd:schema>

The attributeGroup Element

XSD enables you to group attributes that can be reused with different elements. You can group attributes by using the attributeGroup element:

The syntax of the attributeGroup element is as follows:

        <attributeGroup>
              attribute1
              attribute2
                  :
          </attributeGroup>
In the syntax, the attribute1 and attribute2 statements declare the various attributes that are to be grouped. The following example illustrates the use of the attributeGroup element. This code is used to declare the EMPLOYEE element, which contains the FIRSTNAME and LASTNAME elements. The EMPLOYEE element also contains the DEPARTMENT and DESIGNATION attributes.
        
        <xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”> 
               <xsd:element name=”EMPLOYEE” type=”emptype”/>
                <xsd:complexType name=”emptype”>
                       <xsd:group ref=”empname”/>
                       <xsd:attributeGroup ref=”depdesig/>
                </xsd:ComplexType>
                <xsd:group name=”empname”>
                       <xsd:sequence>
                              <xsd:element name=”FIRSTNAME” type=”xsd:string”/>
                              <xsd:element name=”LASTNAME” type=”xsd:string”/>
                       </xsd:sequence>
               </xsd:group>
               <xsd:attributeGroup name=”depdesig”>
                      <xsd:attribute name=”DEPARTMENT” type=”xsd:string”/>
                      <xsd:attribute name=”DESIGNATION” type=”xsd:string”/>
                 </xsd:attributeGroup>
        </xsd:schema>

In the preceding example, the emptype complex type has a reference to the depdesig attribute group, which contains the declarations for the DEPARTMENT and DESIGNATION attributes.

You can use the elements and attributes declared in the preceding schema as follows:

        <?xml version=”1.0”?>
         <EMPLOYEE DEPARTMENT=”Mktg” DESIGNATION=”Mgr”>
                  <FIRSTNAME> James </FIRSTNAME>
                   <LASTNAME> Wallace </LASTNAME>
         </EMPLOYEE>

An attributeGroup element is typically used in cases where the same set of attributes is Used in multiple elements within the schema.

Tuesday, March 4, 2014

How to Create or Manage Tables using Query in SQL Programming

As a database developer, you need to create tables to store data. While creating tables in a relational database. You need to specify certain rules and constraints for columns that specify the kind of data to be stored. In addition, you need to specify the relationships between various tables.

If the table that you are creating needs to store a large volume of data, you can create partitioned table. This helps in improving the performance of the queries.

In addition to creating tables, you are responsible for managing tables. The management of tables involves modifying tables to add columns or to change the rules imposed on the table. It also involves deleting tables, when not required.

Creating a Table

In SQL Server, programmer can create a table by using the CREATE TABLE statement. The syntax of the CREATE TABLE statement is:

CREATE TABLE
 [database_name . [ scheme_name ] .] table_name
  ({ <column_definition> | <computed_column_definition> }
  [ <table_constraint> ] [ ,…n])
 [ ON { partition_scheme_name (partition_column_name ) |
Filegroup
 | “default” } ]
 [ {TEXTIMAGE_ON { filegroup | “default” } ]
[ ; ]
Where
  • database_name specifies the name of the database where the table is created if you do not specify a database name, the table is created in the current database.
  • Schema_name specifies the schema name where the new table belongs. Schema is a logical group of database objects in a database. Schemas help in improving manageability of objects in a database.
  • table_name specifies the new table name. The table name can be a maximum of 128 characters.
  • Column_name specifies the name of the column and must be unique in the table. It can be a maximum of 128 characters.
  • Computed_column_definition specifies the expression, which produces the value of the computed column. A computed column does not exist physically in the memory but it is used to generate a computed value. For example, if you have the order quantity stored in one column and the unit price in another column, you can use compute_column_definition to find the total price of the products. The following SQL query displays the use of computed_column_definition: totalPrice AS OrderQty * UnitPrice
  • table_constraint is an optional keyword that specifies the PRIMARY KEY, NOT NULL, UNIQUE, FOREIGN KEY, or CHECK constraint.
  • Partition_scheme_name specifies the partition scheme name that defines the file groups on which the partition of a table is mapped. Ensure that the partition scheme exist within the database.
  • partition_column_name specifies the column name on which a partitioned table will be partitioned.
  • TEXTIME_ON { filegroup | “default”} are keywords that specify that the text, ntext, image, xml, varchar (max), nvarcar (max), varbinary (max), and CLR user-defined type columns are stored on the specified filegroup. If there are no large value columns in the table, TEXTIMAGE_ON is not allowed.

Consider the following example. The management of Adventure Works, Inc. needs to maintain the leave details of the employees. For this, you need to create a table, EmployeeLeave, in the HumanResources schema, with the following details.

EmployeeID int  NOT NULL
LeaveStartDate date  NOT NULL
LeaveEndDate date  NOT NULL
LeaveReason Varchar(100) NOT NULL
LeaveType char(2)  NOT NULL

You can use the following statement to create the table:
(
CREATE TABLE HumanResources.EmployeeLeave
LeaveStartDate datetime NOT NULL,
LeaveEndDate datetime NOT NULL,
LeaveReson varchar (100),
LeaveType char(2) NOT NULL
)

Guidelines to Create Tables

When creating tables, programmer need to consider some guidelines like column names within a table must be unique, but the same column name can be used in different tables within a database. The table name can be of maximum 128 characters.

Apply constraints on columns

Performing Conditional Formatting in XML

Conditional formatting refers to the formatting of data based on a specified criteria or condition . For example, while creating a list of famous sportsmen, you might want the names of the sportsmen who play your favorite sport to appear in red. You can specify this condition in your XML document using XSL Transformations (XSLT).

The two XSLT elements that you can use to specify conditions for formatting XML
Documents are:


  1. The if element
  2. The choose element

The if Element

The XSLT if element provides a simple if-then construct. The syntax of the if element
Is:
<xsl : if test = “condition”>
[ actions to be performed if the condition is true ]
</xsl : if >

In the preceding syntax, the test attribute specifies the criteria for performing the specified action, and condition specifies a Boolean expression that evaluates to true or false. For  example, to display the names of products priced higher than $100, you can use the following code:

<xsl : if test =”PRICE[ . &gt ; 100]”>
<xsl : value-of select=”PRODUCTNAME” />
</xsl : if>

The choose Element

The choose element enables you to choose from two or more possible courses of action. It enables you to test multiple conditions.
The  choose element must contain one or more when elements. It can contain only one optional otherwise element, which must appear after all the when elements. A choose element with only one when element behaves similar to the if element. With two or more choices, the choose element behaves like an if-then-else or switch-case construct. 
The syntax of the choose element is:
<xsl : choose>
      <xsl : when test=”condition”>
      [action to be taken]
      </xsl : when>
        :
        :
       <xsl : otherwise>
        [action to be taken]
        </xsl : otherwise>
</xsl : choose>

Identifying Comparison and Boolean Operators

The choose and if elements can be used with comparison and Boolean operators to narrow down the conditions based on which the XML document is formatted. The following  table lists the various comparison and Boolean operators.
Operator
Meaning
Example
=
Equal to
PRICE [ . = 20]
PRODUCTNAME [ . = ‘Mini Bus’]
!=
Not equal to
PRICE [ .  != 20]
PRODUCTNAME [ . != ‘Barbie Doll’ ]
&lt ;
Less than
PRICE [ . &lt ; 20]
&gt ;
Greater than
PRICE [ . &gt ; 20]
&lt ;=
Less than or equal to
PRICE [ . &lt ; = 20]
&gt ;=
Greater than or equal to
PRICE [ . &gt ; = 20]
and
Logical AND
PRICE [ . &gt 20 and . &lt ; 30]
or
Logical OR
PRICE [ . = 20 or . = 45]
Not
Negation operator
PRICE [not ( . =30) ]

Representing STACK using one-dimensional ARRAY in 'C'

Representing STACK using one-dimensional ARRAY 

             STACK can be represented either horizontally or vertically as consecutive memory cells if it is implemented by Array. A variable TOP is used to store the current index. Initially when the STACK is empty TOP contains a 0 index when the lower bound of array is 1, it can be stored with -1 if the lower bound is 0.
   
Horizontal representation:


When ITEM is to be PUSHed, TOP is incremented by 1 and at TOP position of Array the ITEM is placed. When TOP is equal to N, the size of STACK, and PUSH operation is done, then ‘Overflow’ occurs. If an ITEM is to be POPped from the STACK, the ITEM placed at TOP index is removed (deleted) and TOP is decremented by 1. Remember here when deletion is done, the items stored in array are not changed only the index is changed. When POP operation is applied to an empty STACK (STACK is empty when TOP is 0) ‘Underflow’ occurs. 

Example 1: Consider a STACK of size 6.
PUSH 6 on to STACK. TOP is incremented by 1 and 6 is placed in the STACK at TOP position. TOP<---TOP+1 and STACK [TOP]<---6.
PUSH 7and 3 on STACK. To PUSH 7 TOP is incremented by 1 and 7 is placed at TOP position that is at second position. To PUSH 3 again TOP is incremented by 1 and 3 is placed at TOP position that is at third position. After these two operations the STACK becomes:
POP the STACK. To POP the ITEM stored at TOP index is copied in a variable, ITEM<---STACK[TOP],  and TOP is decremented by 1.
Example 2 Consider a STACK of size 8. The string “INDIA” CAN BE reversed using STACK.
                                                                              
Now repeatedly POP till the STACK is empty and write the POPped characters you will get the reversed string, AIDNI
Example 3: Consider a STACK of size 8. Using the STACK convert a decimal number 52 to binary number.

½=0, STOP. STACK creation is over. Now repeatedly POP till STACK is empty and write the POPped number you will get the binary equivalent of 52 i.e. 110100. 

       STACK is also used to evaluate a POSTFIX expression value and to convert an INFIX expression to POSTFIX expression.

STACK for Data Structure in 'C' Programming

STACK

Stack is a special type of linear data structure, which can be implemented using either one-dimensional array or Linked List. If the Insertion operation and deletion operations are restricted to one end in one-dimensional array or linked list, then it becomes a specialized data structure STACK. The end, from which insertion and deletion are done, is called as TOP end. The insertion operation on the STACK is called as PUSH operation and deletion is called as POP operation. Remember that both the operations are done to the same end in the sense, for insertion and deletion operation is applied to the same index (top) in one-dimensional array and same pointer (top) in Linked List.
             As an ITEM (information) may be a number or character, depending on the type of array or node of linked list, is PUSHed on STACK from the TOP end and deletion (POPping) if any is to be done from the same end. The ITEM coming out after the POPping is the one just PUSHed, so the STACK is also called as LIFO, Last In First Out, structure . The ITEM PUSHed in Last is POPped out First.

       In your home you can find a STACK of cloths, plates, cassettes etc.

             STACK is a specialized linear data structure, which is having so many applications in computer field. STACK is used in Function calls, Recursion, Decimal number to binary number conversion and in expression evaluation.

XML: Presenting Data in Different Formats

Information pertaining to the way in which XML data must appear in a browser is specified using either CSS or XSLT style sheets. In certain situations, these two methods do not have the capability to display data in certain formats. For example, it is not feasible to display data in the form of tables using either of these documents. In such situations, it is both easier and less time-consuming to use other markup languages that are primarily created to display information in different formats in a browser.

Displaying Data in a Tabular Format

HTML is a markup language that is primarily used for data display. It provides a variety of predefined tags that can be used to display information in a web browser. You can combine the features of HTML and XSLT to format the data from an XML document for appropriate display.

Elements Required to Display Data in a Tabular Format

In order to use HTML tags in an XSLT style sheet, you first write the HTML code to display the data in the desired format. You then embed the HTML code in the XSLT document. The following table lists the HTML elements that are required to display data in a tabular format.

HTML Tag
Description
TABLE
Acts as a container for all other tags used to specify the appearance of data in a table. It has attributes, such as border, background color, cell-padding, cell-spacing, and width that enable you to specify the appearance of the table.
THEAD
Used to specify headings for a table.
TBODY
Used as a parent for the TR and TD elements.
TR
Used to represent a row in a table. This tag acts as a container for the TH and TD elements.
TH
Used to add column headings.
TD
Used to specify the data to be displayed in columns.

© Copyright 2013 Computer Programming | All Right Reserved