-->

Thursday, December 4, 2014

HTML applet tag and their attributes

Applet is a tag in html that is used to create an applet window or applet in an html document.
  Example
<!DOCTYPE html>
<html>
<head>
<title>HTML applet Tags and their attributes</title>
</head>
<body>
<applet code="classname.class" width="300" height="200">
</applet>
</body>
</html>

  Applet tag has following attributes

  Code : This attribute takes the class name of the applet code that is to be displayed . Class is the compiled code for applet
  e.g.   <applet code="abc.class" width="200" height="200" />

  Height : This attribute takes values in pixels to define the height of the applet.

  Width : This attribute takes values in pixels to define the width of the applet.

  Vspace : This attribute takes values in pixels to specify how much space is to be left above and below the object.

  Hspace : This attribute takes values in pixels to specify how much space is to be left to the right and left of the object.

  Align  : This attribute is used to align the object in the applet.

  Title : This attribute takes a string in input which is a title name or short information about the applet. This information is shown as a tooltip which pops when the move is brought over the applet.


Example
<!DOCTYPE html>
<html>
<head>
<title>Example of Line in the applet</title>
</head>
<body>
<applet code="designline.class" width="300" height="200">
</applet>
</body>
</html>

Here is the designline.java file:
import java.applet.*;
import java.awt.*;

public class designline extends Applet
{
 public void paint (Graphics gh)
   {
      gh.drawLine(10,10,200,200);
   }
}

How to retrieve images from database table in ASP.NET

Database is a storage system, through which we can store data permanently into the secondary storage device. Similar to programming languages, database also take some data types for categorize data. like int, Text, DateTime etc. Now, we will discussed on image. In this example we will retrieve images from database. First we store images into database, actually images is not stored directly, so for that path of the images is stored into the database. Now, bind the GridView with the image field.  

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" Width="280px">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                <asp:ImageField DataImageUrlField="imagepath" HeaderText="Image">
                    <ControlStyle Height="200px" Width="200px" />
                </asp:ImageField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [imagetable]"></asp:SqlDataSource>
    </form>
</body>
</html>
Code Generate the following output

How to retrieve images from database table in ASP.NET

Monday, December 1, 2014

Program to find the larger of two numbers using ternary operator

larger = (a>b)? a:b;
The simple logic behind the program is, when a(variable) is greater than b, the value of a is assigned to larger(variable). Otherwise, the value of b is assigned to larger. The program to find the larger of two numbers using ternary operator is shown below:
#include<conio.h>
#include<stdio.h>
void main()
{
int a,b,larger;
clrscr();
printf("Enter a and b:\n");
scanf("%d%d",&a,&b);
/*The larger of a and b is stored in larger*/
larger=(a>b)?a:b;
printf("Larger=%d\n",larger);
getch();
}

Code generate the Following output


Program to find the larger of two numbers using ternary operator

Debugging C program

Debugging is the process of isolating and correcting the errors. One simple method of debugging is to place printf( ) statements throughout the program to display the values of variables. It displays the dynamics of a C program and allows the programmer to examine and compare the data at various points. Once the location of an error is identified and corrected, the debugging statements should be removed. We can use the conditional compilation statements to switch on or off the debugging statements.

Another approach is to use the process of deduction. Here, we arrive at the errors, using the process of elimination and refinement. This is done using a list of possible causes of the error.
The third error-locating method is to backtrack the incorrect results through the logic of the program until the mistake are located. The program is traced backward until the error is located.

How to create aspx page at run time in ASP.NET

ASPX stands for active server page extension. We know that visual studio create a aspx page for dynamic application. Also one another page is created for business logics. If you want to create both pages(presentation+business logic) through code file then first to create two array for both pages. After that save the file through file handling in same solution.

Source Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 

</head>
<body>
    <form id="form1" runat="server">
      Enter page Name  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

        <br />
        <br />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Created Page" />

    </form>
 
     

</body>
</html>

Code Behind file

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

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



    protected void Button1_Click(object sender, EventArgs e)
    {

        String[] sourcecode ={"<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\""+  TextBox1.Text.Trim()+".aspx.cs\" Inherits=\""+ TextBox1.Text.Trim()+"\" %>",
                                 "<html xmlns=\"http://www.w3.org/1999/xhtml\">",
                                 "<head runat=\"server\">",
                                 "<title></title>",
                                " </head>",
"<body>",
    "<form id=\"form1\" runat=\"server\">",
    "</form>",
    "</body>",
"</html>"
                             };

        String[] codefile = {
                                "using System;",
"using System.Collections.Generic;",
"using System.IO;",
"using System.Linq;",
"using System.Web;",
"using System.Web.UI;",
"using System.Web.UI.WebControls;",

"public partial class "+ TextBox1.Text.Trim()+" : System.Web.UI.Page",
"{",
    "protected void Page_Load(object sender, EventArgs e)",
    "{",
      
    "}",
    "}"};

        File.WriteAllLines(Server.MapPath("" + TextBox1.Text.Trim() + ".aspx"), sourcecode);
        File.WriteAllLines(Server.MapPath("" + TextBox1.Text.Trim() + ".aspx.cs"), codefile);
        Response.Write("Page Created");

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/" + TextBox1.Text);
    }
}

Code generate the following output

Windows Form: How to create child node of selected root node of treeview in c#

Basically Tree View contains two things, first one is root node and second one is child node. If you want to create root node in the Tree View then use Tree Node class. Now in this example, we want to create a child node inside the existing root node. So first to create the root node, Follow these instruction to create the root node inside the tree view control.

  1. Add Tree View control in the design form.
  2. Add Root Node in it by right click on Control.
Windows Form: How to create child node of selected root node of treeview in c#








3. Add one Button control and one TextBox control in the design form.
4. Raise the Button Click event, Copy this code and paste into Button_Click event handler

 private void button1_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;
            node.Nodes.Add(textBox1.Text);
        }

Code generate the following output


Windows Form: How to create child node of selected root node of treeview in c#

Program Testing and Debugging in C

Testing and debugging refer to the tasks of detecting and removing errors in a program, so that the program produces the desired results on all occasions. Every programmer should be aware of the fact that rarely does a program run perfectly for the first time. It does not matter how thoroughly the design is carried out, and how much care is taken in coding. One can never argue that program would be 100 percent errorless. It is therefore, necessary to make efforts to detect, isolate and correct any errors that are likely to be present in the program.
© Copyright 2013 Computer Programming | All Right Reserved