Monday, 16 January 2012

Enable Paging to GridView Control in ASP.NET 4.0

To enable paging to the GridView control we have to specify some of its properties like AllowPaging, PageSize and have to handle its event PageIndexChanging.

First, take a GridViewControl on your .aspx page and specify its following properties... (Your declaration of GridView might be like this.



<asp:GridView ID="GridManageWard" runat="server"
         EnablePersistedSelection="true"
         AllowPaging="True"
         PageSize="7"
         CellPadding="3" GridLines="Vertical"
         Height="46px"  Width="263px"
         BackColor="White" BorderColor="#999999"
         BorderStyle="Solid" BorderWidth="1px"
         ForeColor="Black"
         onpageindexchanging="GridManageWard_PageIndexChanging">

            In the above declaration properties highlighted with different color are necessary for paging, rest of them are for the look of the control.

            Now, after creating the gridview control you have to bind datasource to it. You can get data from datasource and bind datasource to the gridview on the Page's Load event as below.


     protected void Page_Load(object sender, EventArgs e)
    {
          // this check is for checking whether the page is loading for the first time or not
         if (!IsPostBack)
         {
              // here we are making an object of class ClsManageWard which is a user defined class 
             // and provides  function for retrieving records from the database
               ClsManageWard ward = new ClsManageWard();

                // here we are taking DataTable to store the data and getting records by calling getWards() method
                 DataTable dt = ward.getWards();

                 //Specify the datasource for the gridview
                 GridManageWard.DataSource = dt;

                // Specifying DataKeyNames 
            GridManageWard.DataKeyNames = new string[] { "Id" };
            GridManageWard.DataBind();



    }


              Now, handle the PageIndexChanging event of the control as described below:



  protected void GridManageWard_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //here we have furthur taken the DataTable and from DataKeyNames we can allow our gridview
        // to show pages

        ClsManageWard ward = new ClsManageWard();

        DataTable dt = ward.getWards();

        // we have to specify DataSource, PageIndex and have to bind gridview
        GridManageWard.DataSource = dt;
        GridManageWard.PageIndex = e.NewPageIndex;
        GridManageWard.DataBind();
    }

         This will make your gridview showing its contents in different pages.

         You can view the snapshots here:
         
           First Page:



             Second Page:










Wednesday, 11 January 2012

Rotate a Semi Circle in Java applet using Runnable Interface

Create a java file name RotateCircle.java as below



/*  Program to draw  a square and rotate a semi circle*/

// Import this two packages to use applet and awt classes
import java.applet.*;
import java.awt.*;


//The following code is written to run the applet window in 500 height and 500 width

/*   <applet code="RotateCircle.java" height=500 width=500>
</applet>   */

public class RotateCircle extends Applet implements Runnable
{
public void init()
{
                  //Creating thread by passing class which implements Runnable interface as a parameter
         Thread t = new Thread(new RotateCircle());
}

public void paint(Graphics g)
{
        // It draws a square with black color


                   g.setColor(Color.black);
         g.fillRect(15,15,100,100);
     
       // This loop is running for drawing and rotating the semicircles

      for(int ifor=0;ifor<360;ifor+=25)
     {
      //It draws gray color arc on the applet
        g.setColor(Color.gray);
                g.fillArc(200,25,100,100,ifor,360);

      //I have drawn white arc to give a feel that arc (semi circle) is rotating
      g.setColor(Color.white);
      g.fillArc(200,25,100,100,ifor,180);

                      try
     {
           // This code is to make the thread sleep for 2 seconds
               Thread.sleep(2000);
              }
     catch(InterruptedException e)
    {
    //catching exception if the thread is interrupted
    }

            // repaint() method is used here to repaints the window each time the loop iterates
             repaint();
     }             
}

//It is a blank implementation of run method
public void run()   {   }
}

Compile the above java file in the command prompt


C:\Java>javac Ex_Rotate.java


Run the appletviewer

C:\Java>appletviewer Ex_Rotate.java


The applet window will be opened and the circle shown below will be rotating on your applet window








How to implement Reflection in J2SE

How to implement Reflection in J2SE


      In J2SE reflection is implemented using classes like Class, Method, Field, Constructor. Here I have given a simple example to understand the concept.

First make a Java file named TestReflect.java



//This class is made for testing the reflection facility provided by Java


class TestReflect
{
        //private data members
        int a=5,b=10,c=85;
        Integer in = new Integer(50);


        //other data members
        public int j=74;
        protected double m;
        public static int st;
       
        //constructors
        private TestReflect()     {    }
        protected TestReflect(int a, int b)   {    }
        TestReflect(TestReflect tr)    {    }
        public TestReflect(int i)  {   }


        //instance methods
        public void disp()    {  System.out.println("  display function  ");      }
        protected void getA()    {       }
        public void getM(double d)   {  System.out.println("Value passed is : "+d); }  
}

Now, to get the properties and methods of the above defined class dynamically define other java file name MyReflect.java



//This class will  make use of java.lang.reflect package


import java.lang.reflect.*;

public class MyReflect
{
      public static String getModifierNm(int modnm)
      {
String str = new String();
switch(modnm)
{
     case 0:str = "Default";
break;
     case 1:str = "Public";
break;
     case 2:str = "Private";
               break;
     case 4:str = "Protected";
break;
}
return str;
      }
       
      public static void main(String args[])
      {
 int ifor;

try
{
        //getting class name dynamically using static method forName() of class Class
     Class clsNm = Class.forName(args[0]);

      System.out.println(clsNm);
      System.out.println(clsNm.getName());
      System.out.println(clsNm.getSuperclass().getName());

      //getting constructors but only return public constructors
      Constructor cntr[] = clsNm.getConstructors();

      System.out.println("Getting public constructors");
       for(ifor=0;ifor<cntr.length;ifor++)
                               System.out.println(cntr[ifor].getName()+ "   ------------  "+cntr[ifor].getModifiers()+"  -----   "+getModifierNm(cntr[ifor].getModifiers()));

       System.out.println("Getting all the constructors");
              cntr = clsNm.getDeclaredConstructors();

         //displaying all the constructors with modifiers
       for(ifor=0;ifor<cntr.length;ifor++)
                               System.out.println(cntr[ifor].getName()+ "   ------------  "+cntr[ifor].getModifiers()+"  -----   "+getModifierNm(cntr[ifor].getModifiers()));


      //gives a list of only public fields
       Field field[] = clsNm.getFields();
     
       System.out.println("Fields got by method getFields()");
       for(ifor=0;ifor<field.length;ifor++)
              System.out.println(field[ifor].getName());

        //gives a list of all the declared fields private, public, protected and default
       field = clsNm.getDeclaredFields();
       Class tmpCls;
   
       System.out.println("Fields got by getDeclaredFields   -------  Getting type of the fields");
       for(ifor=0;ifor<field.length;ifor++)
       {
 tmpCls =  field[ifor].getType();
                    System.out.println(field[ifor].getName()+"  ----------   "+tmpCls.getName());
       }

        //getting values of the public fields
           field = clsNm.getDeclaredFields();
           int var = field[5].getInt(clsNm);
        System.out.println("Value of the first field is : "+var);

        //getting methods
       Method method[] = clsNm.getDeclaredMethods();

        System.out.println("Methods got by getDeclaredMethods()");
       for(ifor=0;ifor<method.length;ifor++)
              System.out.println(method[ifor].getName()   +   "   ------   "+method[ifor].getReturnType().getName());

    //create an instance of the given class at runtime    
       Object obj = cntr[0].newInstance();

   System.out.println("\nCalling disp method");
     method[0].invoke(obj);

   System.out.println("\nCalling getM() method with argument");
    method[2].invoke(obj,5.3);
}
catch(Exception e)
{
}
      }
}

/*    Compile the program in the command prompt

C:\Trupti\javaprac>javac MyReflect.java

Run the program and give the class name of which details you want using reflection

C:\Trupti\javaprac>java MyReflect TestReflect

The following will be the output,...

class TestReflect
TestReflect
java.lang.Object
Getting public constructors
TestReflect   ------------  1  -----   Public
Getting all the constructors
TestReflect   ------------  2  -----   Private
TestReflect   ------------  4  -----   Protected
TestReflect   ------------  0  -----   Default
TestReflect   ------------  1  -----   Public
Fields got by method getFields()
j
st
Fields got by getDeclaredFields   -------  Getting type of the fiel
a  ----------   int
b  ----------   int
c  ----------   int
j  ----------   int
m  ----------   double
st  ----------   int
in  ----------   java.lang.Integer
Value of the first field is : 0
Methods got by getDeclaredMethods()
disp   ------   void
getA   ------   void
getM   ------   void





How to use Reflection in Visual C# 2010 with framework .NET 4.0

Reflection in C# 2010 with .NET 4.0


       Reflection is a very important technique which is used to extract whole details of a program at run time.

           In .Net 4.0 Framework, when a program is reflected it is done by extracting its metadata from its assembly(which has extension .dll) and then by using this metadata its assemblies, modules and type information can be retrieved.

          We can say that,
    "Reflection is the process of finding out the internal elements, such as metadata, assemblies, modules,
and types, of an application without accessing the source code."

       In .Net 4.0 Framework, we can implement reflection by using classes like Assembly, Type, MethodInfo, PropertyInfo, ConstructorInfo classes which resides in System.Reflection namespace.   

       Here I have given a small example of reflection.


        Form:




     The name of all the controls are given in them itself.. (The above figure is the design mode for the form)

      Code


       using System;
       using System.Collections.Generic;
       using System.ComponentModel;
       using System.Data;
       using System.Drawing;
       using System.Linq;
       using System.Text;
       using System.Windows.Forms;
       using System.Reflection;
       using System.Collections;


        namespace Reflection
       {
               public partial class TestingReflection : Form
              {
                      public TestingReflection()
                     {
                              InitializeComponent();
                      }


                       //making objects of Assembly and Type class to store assembly and type details  
                      Assembly assem;
                      Type[] type;


                      private void btnTypes_Click(object sender, EventArgs e)
                     {
                                //getting info. about all the types available in assembly
                               type = assem.GetTypes();


                                 //displaying info. in the list lstTypes by using for loop
                               for (int ifor = 0; ifor < type.Length; ifor++)
                              {
                                           lstTypes.Items.Add(type[ifor]);
                              }
                        }


                        private void lstTypes_SelectedIndexChanged(object sender, EventArgs e)
                        {
                                     //taking array of class MethodInfo, PropertyInfo and ConstructorInfo to store info
                                      MethodInfo []method;
                                      PropertyInfo[] property;
                                      ConstructorInfo[] contr;
                                     
                                        //this integer variable is for the for loop
                                        int ifor;


                                        //clearing all the three lists


                                       lstContr.Items.Clear();
                                       lstMethods.Items.Clear();
                                       lstProperties.Items.Clear();


                                        //getting all the methods available in the Type (e.g., class, interface, etc.)
                                        method = type[lstTypes.SelectedIndex].GetMethods();


                                        //displaying info. in the list lstMethods by using for loop
                                      for (ifor = 0; ifor < method.Length; ifor++)
                                     {
                                                 lstMethods.Items.Add(method[ifor]);        
                                     }


                                        //getting all the properties available in the Type (e.g., class, interface, etc.)
                                      property = type[lstTypes.SelectedIndex].GetProperties();


                                          //displaying info. in the list lstProperty by using for loop
                                      for (ifor = 0; ifor < property.Length; ifor++)
                                      {
                                              lstProperties.Items.Add(property[ifor]);
                                      }
                                     
                                         //getting all the constructors available in the Type (e.g., class, interface, etc.)
                                      contr = type[lstTypes.SelectedIndex].GetConstructors();


                                         //displaying info. in the list lstConstructor by using for loop
                                      for (ifor = 0; ifor < contr.Length; ifor++)
                                     {
                                              lstContr.Items.Add(contr[ifor]);
                                     }
                       }   


                       private void btnBrowse_Click(object sender, EventArgs e)
                       {
                                  // on the click of btnBrowse button showing the Open file dialog box
                                  openFileDialog.ShowDialog();


                                  // assigning the .dll file name to the Assembly object
                                   assem = Assembly.LoadFile(openFileDialog.FileName);


                                   // setting the full name of the assembly file in the textbox txtFullName
                                   string fullName = assem.FullName;
                                   txtFullName.Text = fullName;
                         }
               }
       }


       Snap Shots


       Run the application..



        Select your assembly file in Open Dialog box.


        From the View Type list select the Type of which information you want to get..

        
    


Monday, 9 January 2012

Computing....

 What do you mean by Computing..??
         
  One can say anything which has input - process - output scenario can be called computing.


  That's right. Computing can include calculation, comparison, gaming, data processing and many more.


  The traditional utilities like water, natural gas, electricity and telephone system is going to add one more name to its list that is "computing".


    But I am gonna discuss computing in a very large scale which includes,..



  • Utility Computing
  • Grid Computing
  • Cluster Computing
  • Distributed Computing
  • Cloud Computing
   In my today's session I learnt the attributes of the above computing.

                     Computing                                          Attributes
    1. Utility Computing              ----         Accessibility 
    2. Cluster Computing            ----         Manageability
    3. Grid Computing                 ----         Autonomic
    4. Cloud Computing               ----         Performance, Scalability, Availability, Quality             of System
    The terms defined above started in 1980's with "Distributed Computing" - which is a computer system in which several interconnected computers share computer task assigned to the system.

      Some of the Computing Terms   

      A cluster is a type of parallel and distributed system, which consists of a collection of interconnected stand-alone computers working together and a single integrated computing resource.

     Parallel Computing is a form of computation in which many calculations are carried out simultaneously like 9*8-6/8*25. In this larger problems are often divided into smaller ones and then are solved concurrently.

     Grid Computing is computing which is mainly used in sharing resources, solving problems in dynamic, multi-instructional virtual organization.

     A virtual organization (VO) refers to a dynamic set of individuals or institutions defined around a set of  resource sharing rules and conditions. All these virtual organizations share some commonality among them, including common concerns and requirements, but may vary in size, scope, duration, sociology, and structure.

     The term "Cloud"  is often used as a metaphor (which makes things easily detectable) for the internet. (A simplified way to represent complicated operations..)

     Thus in simple words, we can say that, the "Cloud Computing" is a new paradigm which provides IT services which makes user's life easy when he is using different IT resources.









     I have given here an overview of different kind of computing,.. I'll post more things about this whenever I'll learn more,..