-->

Tuesday, January 28, 2014

Add Text with day in Calendar, ASP.NET Example

If you want to change the style of the Calendar control, you can do this during its rendering process. DayRender event provides such types of functionality, You can change specific day at run time. Lets take an simple example to add text with day. There are some following steps

Step-1 : Add Calendar control onto the web form
Step-2 : Change Calendar style using show smart tag. In this example, we are selecting Professional1 Style from Auto Format popup.
Step-3 : Handle DayRender Event
Step-4 : Copy this code and replace with your code
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="calendar.aspx.cs" Inherits="calendar" %>

<!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>
        <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
            BorderColor="White" BorderWidth="1px" Font-Names="Verdana" Font-Size="9pt" 
            ForeColor="Black" Height="295px" NextPrevFormat="FullMonth" 
            ondayrender="Calendar1_DayRender" Width="456px">
            <DayHeaderStyle Font-Bold="True" Font-Size="8pt" />
            <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" 
                VerticalAlign="Bottom" />
            <OtherMonthDayStyle ForeColor="#999999" />
            <SelectedDayStyle BackColor="#333399" ForeColor="White" />
            <TitleStyle BackColor="White" BorderColor="Black" BorderWidth="4px" 
                Font-Bold="True" Font-Size="12pt" ForeColor="#333399" />
            <TodayDayStyle BackColor="#CCCCCC" />
        </asp:Calendar>
    </div>
    </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 calendar : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day .DayNumberText =="10")
        {
            e.Cell.Controls.Add(new LiteralControl("<p>Holiday</p>"));
            e.Cell.BorderColor = System.Drawing.Color.Green;
            e.Cell.BorderWidth = 2;
            e.Cell.BorderStyle = BorderStyle.Solid;
        }
    }
}

Code Generate the following output

Add Text with day in Calendar, ASP.NET Example

Introduction of Structure in DataStructure, C Programming

       The standard data types available in any programming language like “C” cannot meet every need of the user his for data handling. It is not possible to implement s all real life activities and entities by using only standard data types. So the help of available standard data type by clubbing them into a single unit. Structures are user defined data types, which are used to club dissimilar data member into a single unit. That unit is the new user defined data type and can be used to declare the data variables according to the requirement.
             Usually the structures are defined to club the similar, related characteristics of an object derived as new data type.

Defining a structure  

            A structure is defined using a keyword struct and giving a name to the structure data type and declaring each individual members (elements) as normal declaration in a block (i.e., within a pair of curly brackets, {}) and ending the block by semicolon; because of data definition. The syntax of structure definition is as follows:
        struct datatype_name
        {
        Member1 declaration;
        …
       Membern declaration;
        };
Here “struct” is a key word. Datatype_name can be any valid identifier, used as a name for new data type. For example, an employee structure may be defined as:
        Struct emp
           {
             Int eno;
             Char ename [21];
             Float salary;
           };
Here “struct emp” is a new user defined data type (note: not a variable) and can be used to declare the variables of that type, like struct emp e1, e2, earr[10] etc. The variables defied inside the struct like eno, ename  and salary are the members of struct emp and are also called as elements of the structure. With the help of a variable of the type struct emp, the individual members (elements) may be referred by means of dot (.) operator also called as member access operator. At the time of defining data type the variables may be declared after closing curly bracket ‘}’, before the semi colon ‘;’ as shown below:
struct emp
{
 Int eno;
 char ename [21];
 float salary;
} e1,e2;
The structure variables can be initialized at the time of declaration as follows:
struct emp
{
  Int eno;
  Char ename [21];
  Float salary;
} e1={101,”Sachin”, 12360.55};


Monday, January 27, 2014

Change the Start Page in MVC application

When programmer runs the MVC application, it shows the default page having the address http://localhost:port/. Here localhost is the computer which is running that application at that time, means the client machine and the port is the number assigned for that application.

Run the application of simply press F5 and look out the default page as shown below:

Change the Start Page in MVC application

Now look out the default code written in the Route.Config file under the App_Start folder, which have some other files also. This code have information about the routes user write in the address bar of the browser.

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

This MapRoute method have three parameters listed above i.e. the name of route, format of url (route may have) and the last one will be the default page to be redirected if any route is not found. The last parameter itself have three values, controller decides the name of controller from which the action is to be called. Action decides about the action to be called from the controller.

And the last one is id, which decide about the id of the record if given in the route. It is an optional parameter for the route. Now change the action from Index to About and run the application, It seems the change the default page shown below:

Change the Start Page in MVC application

Primitive Data Types used in Java Programming

Data can be of many types e.g. character, integer, real, string etc. Anything enclosed in single quotes represents character data. Numbers without fraction represent integer data. Numbers with fractions represent real data and anything enclosed in double quotes represents a string.

Since the data to be dealt with are of many types, a programming language must provide ways and facilities to handle all types of data. Java, like other languages provide ways and facilities to handle different types of data by providing data types.

Java data types are two types:

Primitive

Primitive data types we mean fundamental data types offered by Java programming language. Primitive are the basic data type values. The world ‘Primitive’ means a fundamentals component that may be used to create other large parts. Java supports following four categories of primitive data types:

Numeric Integral Types

The data types that are used to store numeric values fall under this sub category i.e. numeric primitive data types. Four numeric integral types in Java are Byte, Short, Int and Long. All integral numeric types store integer values i.e. whole numbers only.

The storage size and the range of values supported by numeric integral types are being listed below:
  • Byte (Size - 8 bits), Byte-length integer having range -128 to + 128.
  • Short (Size - 16 bits), Short integer having range -32,768 to + 32,767.
  • Int (Size – 32 bits), Integer having range (about) -2 billion to +2 billion.
  • Long (Size – 64 bits), Long Integer having range (about) -10E18 to +10E18.
Please note that an l (small L) or L suffix on an integer means the integer is of long data type e.g., 33L refer to a long integer value 33. Thus, we can say that 33 is an int value but 33L is a long value.

Depending upon your requirement, you can select appropriate data type. Consider this you need to store your phone no 12345678 in your program? What do you think should be the data type? Can you store in a byte or short data type? Well you need to store it in int data type. The reason behind is that the number to be stored does not fall into ranges supported by byte and short types.

One more thing that you must know about data types that all data types are singed i.e., they can store negative as well as positive numbers.

Run JavaScript code in Page_Load method, ASP.NET RegisterClientScriptBlock

The RegisterClientScriptBlock method allows you to run JavaScript function at the top of the browser page. It means Script run on Page_Load method. Lets take an simple example of that in ASP.NET.
<%@ Page Language="C#" %>

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

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        string script = @" function Hello(){alert ('Welcome to dotprogramming.blogspot.com');}";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MY Code", script, true);
        
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body onload ="Hello()">
    <form id="form1" runat="server">
    
    </form>
</body>
</html>

Code Generate the following output

Run JavaScript code in Page_Load method, ASP.NET RegisterClientScriptBlock

Introducation to Pointer DataStructure, C Programming

 Pointer is a special type of variable that is used to store the memory address of another variable of free memory requested by the programmer or user during the execution of  the program. To understand dynamic data structures understanding pointer is definitely must. In simple words pointer is a variable that stores memory address whereas simple variables store data.

              For example, when variable is declared the memory is allocated to that variable by the compiler during the compilation of the program. Once memory is allocated the data can be stored in that memory location. The data is referred by the name of the variable. If the address of that variable is to be stored then the pointer variable is necessary. Consider the declaration statement of “C” language: int age;

            Assuming that during compilation memory address allocated variable age is 1000, and the address 1000 is to be stored in another variable that variable should be of the type pointer.
           In case of ‘C’ language the pointer variables are simply declared by means of prefixing an asterisk (*) to the variable name in a normal declaration statement. For example, int *page; declares a pointer variable page of the type int. Now page can be used to store the address of a simple integer variable. i.e. page = &age.
            When a pointer variable is declared, the memory is not allocated to such variable during the compilation. So it can point to any already allocated address of variable or an address of requested free memory. The type of free memory or variable should match with the type of pointer variable. Pointer variables are advantageous when they are used for dynamic memory allocation and function call by reference.

Introduction of Array in DataStructure, C programming

ARRAY is an ordered collection of similar data elements, so it is called as Linear Data Structure. The elements are arranged in Linear order i.e. one after the other, at consecutive memory locations. Each element of the array is naturally bound together by means of consecutive memory allocation to each respective element.
  ARRAY may be either one-dimensional or two –dimensional or multidimensional depending on the characteristics that are explained by the elements. In one-dimensional the elements simply explain

One Dimensional Arrays
       Array is a Linear Data Structure in which homogeneous (similar)
element are stored. Array is used to represent a real life List. The elements stored explain one and only one characteristic. For example you may find an array of makes of students, height of students, salary of employees, employee structures etc.

Sequential Allocation   
    One-dimensional array is stored consecutively or sequentially in memory. Contiguous memory is allocated for an array to store the elements. The address of an element of an array is the location in memory where that element is stored. The address of first element is the starting address of the array and it is called ‘Base Address’. Total memory needed for storage of an array depends on the number of element (size of array) in it and the size of each element. The size of element is termed as ‘Word Size’.

WS – size of element stored in bytes (basic memory unit)                                               
a1, a2,… a10 are the address of each element                                                          
a1 First element’s address – The Base Address                                                                         a2=a1+word size.                                                                                                                  
a3=a2+word size OR a1+(word size) * 2                                                                                   
a4=a1+(word size) * 3                                                                                           
…                                                                                                                                   
an=a1+(word size) * (n-1)                                                                                                
IF a1=1000, so Base Address=1000 and WS=4 bytes, then                                        
a2=BA+WS *(2-1)                                            a5=BA+WS *(5-1)                                 
=1000+4 * 1                                                        =1000+4 * 4                                       
=1004                                                                  =1016
In case of “C” language one-dimensional array is declared in the declaration part of the program part of the program as follows:
    data _type name _of _the _array[size];
where data _type may be any of the standard data types available in C like int, float, long, char, double etc. 
name _ of _ the _ array is an identifier or user defined variable name to represent  array name. [size] is an integer constant that specifies the size of the array and it represents number element that can be stored in one-dimensional array.
        For example, int marks [5] is an array of size 5 and of the type integer.
In “C” language the lower bound of the array is fixed to 0(Zero) and the upper bound of is size-1. User only has to take care of the array boundary. So in the above example, makes [0] represents the first element whereas marks [4] represents the last element of the array.

Two Dimensional Arrays
       Two-dimensional array are called as Tables in case of business and Matrix in case of Mathematics. The elements stored in a two-dimensional array explain two characteristics. Row and Columns are used to explain the two dimensions of two-dimensional array. Two-dimensional is a collection of multiple columns. Row index and column indexes are used to access an individual element stored in two-dimensional array. 


Row size of the above two-dimensional array is 3 and column size is also 3. So it is called a 3*3 matrix or two-dimensional array.  Two-dimensional array is represented as
         NameOfTheArray (RLB: RUB, CLB:CUB) OR
         NameOfTheArray (M, N)
         NameOfTheArray(M X N)
Where RLB- Lower bound of Row, RUB-Upper bound of Row .
CLB-Lower bound of column, CUB-Upper bound of Column.
M- Row size, N- Column size. (In this case 1 is lower bound, for both row and column).
     When row size M and column size N are not given they are calculated as M=RUB-RLB+1 and N=CUB-CLB+1.
M*N elements are stored in two-dimensional array(TDA).

Memory Representation or Sequential allocation of TDA
  
                   As there are two dimensions of two-dimensional array, it is stored in memory in sequential M*N cells where M and N are row and column size of TDA respectively. The sequential storage may be by ROW major or COLUMN major depending on the compiler.
                  In ROW major all the ROW element are stored sequentially, means first row elements are stored first then second rowed elements and so on. In COLUMN major all the COLUMN elements are stored sequentially, means first the column elements are stored first then second column elements and so on.
                                                   For example consider a matrix MAT (3*5), the element of which are as shown below:  
                              4   5   6   8   9
                              7   9   1   5   3
                              6   5   4   3   2
In case of Row major the stored can be represented as follows:              


11 mean first row’s first column element, similarly 12 and 13 represent second and third column elements first row respectively, so on the elements can be represented.     
        So, in case of row major all the 5.elements of first row are stored first, then second row’s 5 elements and then third row’s 5 elements are stored sequentially in memory. So depending on the word size M*N storage locations are required to store the TDA elements. Here in this example 3*5=15, fifteen storage (memory) locations are needed to store the TDA. As the word size is 2 (assuming integer) bytes, the total memory required is 30 bytes. 
In case of column major the storage can be represented as follows:  


11 means first row’s first column element, similarly 12 , 13 and so on.
     So, in case of column major all the 3 elements of first column are stored first, then second column’s 3 elements, then third column’s 3elements, then fourth column’s 3 elements and fifth column’s 3 elements are stored sequentially in memory. So, depending on the word size M*N storage locations are required to store the TDA elements.
Here in this example 3*5=15, fifteen storage (memory) locations are needed to store the TDA. As the word size is 2 (assuming integer) bytes, the total memory required is 30 bytes (similar to row major).

Address Calculation
              In case of two-dimensional array the address of the individual element is calculated depending on the storage major either row major or column major.  The first element of the TDA is stored at location called Base Address and the location of the next coming elements depends on the word size, similar to one-dimensional arrays.

Row Major      
                          In case of Row major as all the row elements are stored sequentially, by knowing the Base Address and Word Size, it is possible to calculate the location of any element. To reach to the element i.e. to find the location of the element, number of rows elements are skipped to reach to the element in the respective row and number of column elements are skipped to reach to the column element  of that row.
                          For example in a TDA, MAT (5*6), in order to reach the fifth element of fourth row, three row are skipped and each row contains 6 elements plus four elements of fourth are also skipped. So total elements to skip is equal to 3*6+4=22, If word size is 4 then 22*4=88 plus Base Address is the address or location of fifth element of fourth row i.e. MAT(4,5).
      For example consider a TDA, MAT (5 * 6), the element of which are as shown below:
      The elements to be skipped are shown in bold face in order to reach to element 2.
Total number of elements skipped are 3*6+4=22.
If they are stored in memory starting from location 100, then the memory representation of the above TDA is as follows (assuming the word size 2 because of integer data):


So, the formula to find the address is as follows if TDA is given as MAT(M X N) (in which both RLB and CLB are 1):
Where
BA - Base Address,          WS – Word Size
I – Row Number,              J – column Number
N – Column Size

If TDA is given as MAT(RLB : RUB, CLB : CUB), then the formula changes to

Where
                BA – Base Address,        WS – Word Size,   I – Row Number,
                J – Column Number,      CUB – Column Upper Bound, CLB – Column Lower Bound
       In case of “C” language the two-dimensional array is declared by adding another size to one-dimensional array.
The syntax of the declaration of two-dimensional array looks like:  data-type name-of_the_array[rsize][csize];
Where data_type may be any of the standard data types available in C like int, float, char, double etc. name_of_the_array is an identifier or user defined variable name to represent array name. [rsize] is an integer constant that specifies the row size of the array and it represents number rows that can be stored I two-dimensional array and [csize] is also integer constant that shows number elements for each row. For example, int matrix[5][5] is two-dimensional array of size 5*5 and of the type integer. Here matrix whereas matrix [4][4] represents last row’s last column element.
© Copyright 2013 Computer Programming | All Right Reserved