-->

Wednesday, January 29, 2014

Draw graphics in ASP.NET

You can draw graphics on browser window using Graphics class. This class exists in System.Drawing namespace. It provide different types of graphics methods, such as DrawArc, DrawImage, DrawIcon, DrawLine and many more. Graphics class hold pen instance for creating graphics onto the screen.

Algorithm Behind the drawing

Step-1 : First define the graphics area on browser window using Bitmap class.
Step-2 : Create instance of Graphics class and initialize with Bitmap class instance.
Step-3 : Create Pen class object with specified color and width.
Step-4 : Create starting point and ending point using Point class.
Step-5 : Draw graphics and save into Bitmap instance.

Lets take an simple example of drawing Line , Rectangle in asp.net

 <%@ 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>
    
    </div>
    <asp:Image ID="Image1" runat="server" Height="83px" Width="95px" />
    </form>
</body>
</html>

CodeBehind Code Draw Rectangle

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

public partial class Default3 : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
         Bitmap bmp = new Bitmap(500,200);  
        Graphics g = Graphics.FromImage(bmp);  
        g.Clear(Color.Green);
        Pen p1 = new Pen(Color.Orange, 3);

        g.DrawRectangle(p1, 20, 20, 80, 40);

        string path = Server.MapPath("~/Image/rect.jpeg");
        bmp.Save(path, ImageFormat.Jpeg);

        Image1.ImageUrl = "~/Image/rect.jpeg";


        g.Dispose();
        bmp.Dispose();

    }
}

Code Generating the following output

Draw Rectangle in asp.net

CodeBehind Code for Draw Line

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

public partial class Default3 : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
         Bitmap bmp = new Bitmap(500,200);  
        Graphics g = Graphics.FromImage(bmp);  
        g.Clear(Color.Green);
        Pen p1 = new Pen(Color.Orange, 3);
        
        g.DrawLine(p1, 20, 20, 100, 100);
        string path = Server.MapPath("~/Image/line.jpeg");
        bmp.Save(path, ImageFormat.Jpeg);

        Image1.ImageUrl = "~/Image/line.jpeg";


        g.Dispose();
        bmp.Dispose();

    }
}

Code Generating the following output

Draw line in asp.net

Operations on Data Structures, C programming

Basically there are six operations one can do on the data structures. They are Traversing, Searching, Sorting, Insertion, Deletion and Merging.

1.  Traversing: Basically to process a data structure if every element of data structure is visited once and only once, such type of operation is called as TRAVERSING. For example to display all the elements of an array, every element is visited once and only once, so it is called as traversing operation.
            Suppose we have an array of 20 students’ average makes, and if we need to calculate the average of group, we visit (read) each individual average and accumulate (sum) them. Then we will find the array are visited once and only once. Then it is traversing of an array.

2.  Insertion: When an element of the same type is added to an existing data structure, the operation we are doing is called as Insertion operation. The element can be added anywhere in the data structure in the data structure. When the element is added in the end it is called as special type addition, Appending. In case of adding an element to the data structure we may come across ‘Overflow’ If the size of the data structure is fixed and it is full, then if we try insertion operation on the data structure it is said to be overflow of data structure or the data structure is full.

           Suppose we have an array of size 20 used to store marks obtained by the students in Computer Science subject. If we have already added 15 students’ marks according to an ascending order, then we can add another student marks at appropriate place, by shifting the already stored element accordingly. Then it is called Insertion operation.

3.  Deletion: When an element is removed from the data structure, the operation we are doing is called as Deletion operation. We can delete an element  from data structure from any position. In case of deleting an element from the data structure we may come across ‘Underflow’. If no elements are stored in the data structure, then if we try deletion operation on the data structure it is said to be underflow of data structure or data structure is empty.

         Suppose we have an array of size 20 used to store marks obtained by the students in Computer Science subject. If we have already added 15 student s’ marks according to an ascending order, then we can delete one student’s marks from any place, by shifting the already  stored elements accordingly. Then it is called Deletion operation.

4.  Searching: When an element is checked for its presence in a data structure, that operation we are doing is called as ‘searching’ operation. The element that is to be searched is called as key element. The searching can be done using either ‘linear search’ or ‘binary search’.

        Suppose we have an array of size 10 and assume elements stored are 1,2,3,23,34,54,56,21,32,33 and assume we want to search the number 23. So here the number ‘23’ is key element. The key is searched through the array starting from the first element till end. When it is compared with the fourth element in it is present in the list (array) of numbers. So search is successful. This type of searching is called as Linear Search because the last element. In order to apply linear search, the list may not be in any particular order but when binary search is applied the list must be an ordered one.

5.  Sorting: When all the elements of array are arranged in either ascending or descending order, the operation used to do this process is called as Sorting. The Sorting can be done using Insertion, Selection or Bubble sort techniques.
           Suppose we have an array of size 10 and assume elements stored are 1,2,3,23,34,54,56,21,32,33. The elements of the array are not in proper order. If they are arranged in ascending order the list (array) becomes 1,2,3,21,23,32,34,54,56.

6.  Merging: When two lists List A and List B of size M and N respectively, of same type of elements, clubbed or joined to produce the third list, List C of size (M+N), and the operation done during the process is called as Merging.

           Suppose we have a list of size 6, containing the elements 1,2,3,4,71,87 and we have another list of size 5, containing the elements 9,13,21,65,67. Here both the lists are in ascending order. We can produce the third list of size 11 that will also be in ascending order, containing the element 1,2,3,4,9,13,21,65,67,71,87. The third list is called the merged list of first and second lists. In order to merge two lists into single list one needs to compare each element from both the lists and one of them will   go the merged list.

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};


© Copyright 2013 Computer Programming | All Right Reserved