-->

Tuesday, January 28, 2014

Type of Fractional Numeric Data Types used in Java Programming

The integral numbers covered in earlier article could store only whole numbers i.e. they could not have decimal values. Fractional data types can store fractional numbers i.e. numbers having decimal points. There are also called floating-point data types.

Following list displays various floating-point data types available in Java, along with their ranges.

  • Float (Size – 32 bits), Single precision having range -3.4E+38 to +3.4E +38. This data type precision up to 6 digits like temperatures, currency, and percentage or may be some length.
  • Double (Size - 64 bits), Double precision having range -17E+308 to 1.7E+308. This data type precision up to 15 digits like large numbers or high precision, such as for astronomy or subatomic physics.

Now depending upon your requirements for the decimal precision of the data, you can select from float or double types.

For example, you can store the salary values of an employee using a data variable of the float type. To store decimal values with a higher degree of precision, such as values calculated using the functions, sin() and sqrt(), you see the double data type.

By default, Java treats fractional numbers as of double data type e.g. if you write 0.123, it will treated as a double value. To explicitly specify it to be of float type, use suffix F or f i.e. write 1.234f or 1.234F. Similarly if you use suffix d or D, it means double i.e., 1.234D or 1.234d means it is a double value.

How to Querying Data using Joins in Sql Programming

In a normalized database, the data to be viewed can be stored in multiple tables. When you need to view data from related tables together, you can query the data by joining the tables with the help of common attributes. You can also use subqueries where the result of a query is used as an input for the condition of another query.

This article discusses about how to query data from multiple tables by applying various types of joins, such as an inner join, outer join, cross join, equijoin, or self-join.

Querying Data by Using Joins

As a database developer, you may need to retrieve data from more than one table together as a part of a single result set. In such a case, different columns in the result set can obtain data from different tables. To retrieve data from multiple tables, the SQL Server allows you to apply joins. Joins allow you to view data from related tables in a single result set. You can join more than one table based on common attribute.

Depending on the requirements to view data from multiple tables, you can apply different types of joins listed below:

Inner Join

An inner join retrieves records from multiple tables by using a comparison operator on a common column. When an inner join is applied, only rows with values satisfying the join condition in the common column are displayed. Rows in both tables that do not satisfy the join condition are not displayed.

A join is implemented by using the SELECT statement, where the SELECT list contains the name of the columns to be retrieved from the tables. The FROM clause contains the names of the tables from which combined data is to be retrieved. The WHERE clause specifies the condition, with a comparison operator, based on which the tables will be joined.

The syntax of applying an inner join in the SELECT query is:

SELECT column_name, column_name [, column_name]
FROM table1_name JOIN table2_name
ON table1_name.ref_column_name join_operator
Table2_name.ref_column_name

Where
  • Table1_name and table2_name are the name of the tables that are joined join_operator is the comparison operator based on which the join is applied
  • Table1_name.ref_column_name and table2_name.ref_column_name are the names of the columns on which the join is applied.
Whenever a column is mentioned in a join condition, the column should be referred by prefixing it with the table name to which it belongs or with a table alias. A table alias is a name defined in the FROM clause of the SELECT statement to refer to the table with another name or to uniquely identify the table.

The following query displays the Employee ID and Title for each employee from the Emoloyee table and the Rate and PayFrequency columns from the EmployeePayHistory table

SELECT e.BusinessEntityID,e.JobTitle, eph.Rate, eph.PayFrequency
from HumanResources.Employee e
JOIN HumanResources.EmployeePayHistory eph
ON e.BusinessEntityID = eph.BusinessEntityID

In the preceding query, the Employee and EmployeePayHistory tables are joined on the common column, ID. The query also assigns e as the alias of the Employee table and eph as the alias of the EmployeePayHistory table. The column names are also listed with the table alias names.

How to Querying Data using Joins in Sql Programming

Based on the relationship between tables, you need to select the common column to set the join condition. In the preceding example, the common column is ID, which is the primary key of the Employee table and the foreign key of the EmployeePayHistory table.

Observe the result set after the join. The record of the employee with D as 1 from the Employee table is joined with the record of the employee with ID as 1 from the EmployeePayHistory table.

The transfer fails if a user tries to upload a file in ASP.NET

Size restriction protects your web application from malicious users. They will upload numerous large files to your Web server. So Developers take the default size limitation is 4MB. The transfer fails if a user tries to upload a file that is larger then 4096kb. Now, if you want to change the default size limitation, you make some changes in either the root we.config file , which is found in that path (c:\Windows\Microsoft.NET\Framework\v4.0.xx\config) or in your application's web.config file.

How to Increase uploaded file size 

<httpRuntime> section takes different properties but two properties , The maxRequestLength and executionTimeout properties are very nice. If you want to change the default uploaded file size then you must to change the value of maxrequestLength. It take the size in KiloBytes. Lets take a simple syntax here.

maxRequestLength= 11000

The user can upload around 10mb files using file upload control. Also must to change the executionTimeout property, This property sets the time ( in seconds ) for a request to attempt to execute to the server before ASP.NET shuts down the request. try this into your web.config file

<system.web>
    <httpRuntime executionTimeout ="300" maxRequestLength ="11000"/>
</system.web>

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.

© Copyright 2013 Computer Programming | All Right Reserved