Search This Blog

28 June, 2011

.NET for beginners

Objective to learn :-

CRUD(Create, Retrive, Update and Delete ) Operation and to develop a website.
Please follow some Steps to learn basic work of DotNet.
Install .net on your pc.


1. Create a folder in C - drive (eg. c:\Mayur)
2. Open Microsoft Visual Studio - > 


It will take some times to open, thus wait until getting open.
2.Create a new website using .Net 2010 SDK (Visual C#)
File - > New - > Website - > Visual C# - > Asp.Net




Choose any specified folder:-

You will see such type of form:-

 3. Add a new Item -> "Sql Server Database" from  Solution Explorer. 


 4. Double click to Database to open server explorer

5.Create a New table - Employee (Fields as decided)
id - PrimareKey (int)
Name- Char(20)
City- Char(20)




 6. Add a New Item- > DataSet - >EmpDataSet to the project.

7. Drag & Drop the Employee table on the dataset.
Employee Table Adapter class will be created by the SDK with following methods.
a. Insert
b.Delete
c.Update
d.GetData
We will not have to write SQL statements.








8.Save All
9. Add webForms:-
a. EmployeeAddForm
b. EmployeeEditForm
c. EmployeeViewForm
d. EmployeeDeleteForm




10. Add hyperlink to default.aspx form which is in solution explorer.
 1. Hyperlink - Add
 2. Hyperlink - Edit
 3. Hyperlink - View
 4. Hyperlink - Delete
 and also set their Negivation url (Right Click on Hyperlink property- Nevigation Url) properties to respective webforms.








11. On the Add/ Edit/ View/ Delete forms add a hyperlink (home) to take to the default.aspx page.
12. Set the start page as Default.aspx
13. Check the Navigation system.
14. Design the Employee Add form then Edit form then View then Delete form.

----------------------------------Thinking you----------------------------------- 



27 June, 2011

Exceptions


• An exception is an unusual condition in a
program.
– Example: an illegal mathematical
operation is an exception.
– Eg. integer division by zero will lead to
an exception and to immediate program
termination.


Program :-
public class DivideByZero
{
public static void main (String args[])
{
int a=1;
int b=0;
System.out.println (“a/b = ” + a/b);
}
}


• output:
java.lang.ArithmeticException: / by
zero
at
DivideByZero.main(DivideByZero.java:7)
Application Exit



• Though the stack trace provided by Java is
helpful to the programmer for debugging, such
abrupt termination is likely to leave the user
confused.
• Exceptions allow the programmer to handle such
situations smoothly..


• The classical C approach to exception handling
involves two routes.
– signal and raise
– setjmp and longjmp


• In the signal method, an exceptional condition,
such as division by zero, is indicated by means
of a signal SIGFPE, defined in signal.h
• (Alternatively, a user may raise a signal through
raise.).
• When a signal is raised, a signal handler (a
function) is called.
• A default signal handler is provided, and the user
may override it by registering an alternative
signal handler (pointer to a function).
• A default math error handler is provided, and the
user may override it by redefining the function
matherr.


---------------------------------------


Need of Exception Handling


---------------------------------------

Nested and inner classes (contd)


• Nested classes have access to all of the
variables of the enclosing class.


• However, the enclosing class must access the
nested class variables through an object
reference.


• The key point is that the nested class can inherit
independently of the inheritance hierarchy of the
enclosing class.


• Thus, inner classes permit multiple inheritance in
practice.


• This is illustrated by the following example.


• Note: Java rejects multiple inheritance in
principle, since it permits one class to have only
one superclass.
– Inner classes do NOT negate this rule in
principle.
– However, through inner classes, one can
effectively subvert this principle in
practice.


Program:-
• In file: e:\java\kawa\pkg2\Base2.java
package pkg2;
public class Base2
{
int base2;
protected void base2Method()
{
System.out.println (“In Base 2");
}
}


• In file: e:\java\kawa\pkg1\MultipleInherit.java
package pkg1;
class Base1
{
int base1;
void base1Method()
{
System.out.println (“In Base 1");
}
}
class Derived extends Base1
{
int derivInt=2;


class Inner extends pkg2.Base2
{
void showInner()
{
base1Method();
base2Method();
//access protected method from other
//package
System.out.println (”Though I abused"+
“multiple inheritance, I still use it”);
}
}//end of class Inner
void deriv()
{
Inner i = new Inner();
i.showInner();
}}
public class MultipleInherit {
public static void main(String args[])
{
Derived d = new Derived();
d.deriv();
//Inner i = new Inner(); //illegal
//i.showInner();
} }
• Output:
In Base 1
In Base 2
Though I abused multiple inheritance, I still use it.

------------------------------
Exceptions
------------------------------

Inner classes and multiple inheritance in Java


• Interfaces provide one route to multiple
inheritance.


• Inner classes provide another route.


• This second route is not documented, and sun
documentation speaks of adapter classes.


• However, while inner classes may be used in that
context, adapter classes are irrelevant to the
basic syntax of inner classes.


• One class may be defined within another.


• Such a class is called a nested class.


• A static nested class behaves like a top-level
class (i.e., a class which is not contained in any
class)


• A non-static nested class is called an inner class.


• Such nested classes may be defined within any
expression.


-------------------------------------
Nested and inner classes (contd)
-------------------------------------

Dynamic polymorphism, extending interfaces and adapter classes


– An object reference may be declared to
be of interface type.
– This reference may be assigned at run
time to any one of various classes which
implements the interface.
– The implemented methods of that class
will be called.


• Inheritance of interfaces:
– One interface can extend another
interface.


interface mine extends yours
{
//new methods
]


• A class which now implements interface mine
must implement all the methods in yours and
mine.


• However, there are Adapter classes which enable
one to implement only part of an interface.



Program :-
interface Area
{
final float pi = 3.14f;
float area (float diameter);
}
class Circle implements Area
{
public float area (float diameter)
{
return pi*diameter;
}
}
class Square implements Area
{
public float area (float diameter)
{
return diameter*diameter;
}
}

public class Interf
{
public static void main (String args[])
{
Area a;
a = new Circle();
System.out.println (“Area of circle = ” +
a.area (2.0f));
a = new Square();
System.out.println (“Area of square= ”+
a.area (2.0f));
}
}
• Output:
Area of circle = 6.38
Area of square = 4
---------------------------------------------
Inner classes and multiple inheritance in Java
---------------------------------------------

Interfaces : Java specific


• Java’s first route to multiple inheritance is
provided through interfaces.


• Usually, in multiple inheritance one is interested
only in the methods, and not in duplicate
inheritance of data members.


• An interface contains only
– constants (i.e., final data members), and
– method declarations
– Like a class the interface itself can have
default or public access.


access_type interface i_name
{
return_type method1(param_lst);
return_type method2 (param_lst);
type final var_name1 = value;
...
}


• However, all methods and constants in an
interface are implicitly public.


• A class implements an interface.
– i.e., the class defines the methods
declared in the interface.

– all interface methods must be declared
public in the class implementation.
• A class may implement more than one interface,
by providing a comma separated list of
interfaces.
access class class_name [extends
superclass_name] implements
interface_name1, interface_name 2
{...
]
• If a class which implements an interface, does
not implement all the methods of that interface,
then the class must be declared as abstract.


• Any subclass must either implement all the
methods or be itself declared as abstract.


• Interfaces thus provide a form of multiple
inheritance, in which a class may inherit
methods from a variety of sources.


------------------------------------------
Dynamic polymorphism, extending interfaces and adapter classes
------------------------------------------

Exception handling: Java specific part-5



Part- 5
Catch all exception handler
• Since all exceptions are subclasses of
Exception,
– To catch all exceptions, one uses the
catch-all exception handler with
Exception as the type
catch (Exception e) {
//catch block
}
• Since one exception is handled by at most one
catch block,
– if there is more than one catch block,
– and a catch-all,
• then the catch-all error handler must appear last
– for the first catch block which fits the
exception will be executed.
• Similarly, if there are two catch blocks one
involving a base type and one a derived type,
– then the catch block with base type must
appear last
– since a base type will fit a derived type,
but not vice versa.



Exceptions: Miscellaneous
• Try blocks may be nested.
– In this case, if the first try block does not
have an appropriate handler for the
exception, the exception will be passed
to the outer block, and so on.
– The exception can be handled by both
inner and outer catch block, by making
the inner catch block rethrow what it has
caught.
• A finally clause may be associated with each try
block, and this is guaranteed to execute.
– The JVM achieves this by checking all
possible exits to the try block.


Program:-


import java.io.*;
public class NestedCatch
{
public static void main(String args[])
{
int a;
int b;
try
{
try
{
System.out.println (“a = ”);
DataInputStream in = new
DataInputStream(System.in);
String inString = in.readLine();
a = Integer.parseInt (inString);
System.out.println (“b = ”);
b = Integer.parseInt (in.readLine());
int c = a/b;
}
catch(ArithmeticException e)
{
System.out.println (“Caught Arithmetic
exception”);
throw e; //rethrow
}


catch (IOException i)
{
System.out.println (“Caught i/o
exception”);
}
finally
{
System.out.println (“Exiting inner try”);
}
}
catch(Exception e)
{
System.out.println (“Caught something”);
}
System.out.println (“Program exit”);
}
}


• input:
a=2
b=2
• output:
Exiting inner try
Program exit
• input:
a=*
• output:
Exiting inner try
Caught something
Program exit


-------------- <<<Part- 4 ----------------------------------



Exception handling: Java specific part-4


Part - 4
Exceptions: Throwable and checked exceptions
• Unlike C++, primitive types cannot be thrown.
Throwable types must be subclasses of
Throwable
• In the previous example we saw that an
uncaught exception leads to program
termination.
• Not all exceptions can be uncaught. Exceptions
may be divided into two classes
• Unchecked exceptions are
– subclasses of Error, or
– subclasses of RunTimeException
• checked exceptions: all others
Exceptions: catch all
• if a method can generate checked exceptions,
then these must be either
– specified using a throws clause
– or caught in a try block
else there is a compile-time error
• Unchecked exceptions need not be specified.
– hence lazy programmers tend to
subclass RunTimeException to produce
their exceptions.


Program:-

class MyException extends Exception
{
int exception_num;
}
public class CheckedException
{
int myMethod (int a, int b) throws
MyException
{
MyException e = new MyException();
if (a==0)
{
e.exception_num = 1;
throw e;
}
if (b==0)
{
e.exception_num=2;
throw e;
}
return a/b;}
public static void main (String args[])
{
CheckedException e = new
CheckedException();
try
{
e.myMethod (0, 1);
}

catch (MyException me)
{
System.out.println (me.exception_num);
}
}
}
• The above program will NOT compile if
– the throw clause is removed from
myMethod, or
– the catch clause is removed from main
(and no throws clause is provided to
main)


<<<-Part - 3     ------------------------   Part - 5 >>>   


Exception handling: Java specific part-3


Part - 3
Exceptions: multiple catch blocks
• Catch is not called: program execution is
transferred to it.
– if no exception occurs, then the catch
block will NOT execute.
• There may be more than one catch block.
– Each catch block handles one type of
exception .
– But only that catch block will be
executed which matches the type of the
exception generated.
• Thus, if we rewrite the previous program, so that
– there are two catch blocks:
– one which handles arithmetic exceptions.
– and one which handles i/o exceptions,
– and if an integer is incorrectly entered,
– then the exception will NOT be caught,
and an abnormal program termination
will occur .


Program:-

import java.io.*;
public class MultipleCatch
{
public static void main(String args[])
{ int a;
int b;
try
{
System.out.println (“a = ”);
DataInputStream in = new
DataInputStream(System.in);
String inString = in.readLine();
a = Integer.parseInt (inString);
System.out.println (“b = ”);
b = Integer.parseInt (in.readLine());
int c = a/b;
}
catch(ArithmeticException e)
{
System.out.println (“Caught Arithmetic"+
“exception”);
}
catch (IOException i)
{ System.out.println (“Caught i/o”+
“exception”);
}
System.out.println (“Program exit”);
}
}




• Case 1: input:
a=2
b=3
• output: Program exit
• Case 2: Inputa=2
b=0
• Output:
Caught Arithmetic Exception
Program exit
• Case 3: input: a= *
• output:
java.lang.NumberFormatException: *
at java.lang.Integer.parseInt(Integer.java:231)
at java.lang.Integer.parseInt (Integer.java:278)
at MultipleCatch.main (MultipleCatch.java:15)
• Case1: no catch block is executed, since no
exception is thrown.
• Case 2: the first catch block is executed, since
an ArithmeticException is thrown.
• Case 3: a NumberFormatException is thrown but
there is no matching catch block. Hence the
program terminates.


<<<-Part - 2      ------------------------   Part - 4 >>>    


Exception handling: Java specific part-2


Part - 2




Exceptions: try, throw, catch sequence


• The following code is an example of a basic
try-throw-catch sequence.


public class CatchException
{
public static void main(String args[])
{
int a = 1;
int b = 0;
try //start try block
{
System.out.println (“Trying...”);
int c = a/b; //exception is thrown
}
catch (ArithmeticException e)//and caught
{
System.out.println (“Well caught”);
}
System.out.println (“Normal program exit”);
}
}


• Output: Trying...
Well caught
Normal program exit.


<<<-Part - 1      ------------------------   Part - 3 >>>                                                        

Exception handling: Java specific part-1


Part - 1
In Java exception handling is done through 5 keywords.


– throw
– try
– catch
– throws
– finally


• When an exception is thrown,


– not only is the stack unwound to a
previously saved state, but
– automatic objects are marked for
finalization (destructors are exeecuted in
C++)
– if the exception is thrown from within a
constructor, the finalizer of that object is
not executed.
– key resources may be manually freed
through the use of a finally block which is
guaranteed to execute, whether or not an
exception is thrown.


• The code to be monitored is put into a a try block.


• The action to be taken, when an exception is
thrown is put into various catch blocks.



• Each catch block decides what action is to be
taken when a particular exception is thrown. try{//try block
}
catch (Exception_type1 arg)
{ //catch block
}
catch (Exceptiontype2 arg)
{ //catch block 2
}
.............................................      Part - 2   .....................................

Meetme@