Thursday, November 28, 2013

C# OOPS CONCEPTS

FIRST READ EXAMPLE FOR OOPS CONCEPTS

Before going into the OOPS concepts I would like to give you one scenario based on that the oops concept will be well defined for you!

For example if you attending an interview, the company will get your resume and also they will give you a format of paper to be filled out. On the form it may contains the information you gave along with your resume and also other information required for the company.

here the company has some format and they not updating the information with a single format rather than they getting xerox copy of that and providing to everyone and receiving back the information they need.

                  From the above point of view, the single format of classified application form is a CLASS and number of xerox is said to be an OBJECT in the object oriented language programming.

                       If we need any alteration in the form we do not delete the entire form and create a new one, rather than we get the maximum contents of the previous form and create a new form with necessary columns in the new form, thus by inheriting the value and contents of the previous form is said to be a Inheritance.

                       In this scenario the people filling out the form cannot able to see the other form, is called Abstraction. Hiding of irrelevant data.

                     If the same form is distributed with various names for various different sections is called Polymorphism.
                    Take the above example as a reference only. Whenever you are about to learn any new technologies do not think too much at the initial as you will be in a hurry stage and many thing will confuse you take little time to learn day by day and you will understand once your known things are joined and give you the result.
Most importantly do not think, why it is use rather than think how it should be use!!!



Now we going to see what a oops language in .net environment, 

Class:

          A Class is a collection of object. A main template.

Objects:


         An object contains member attributes and member functions.

      Syntax:

       class  form
    {
           form objectname = new form();     
     }
  In asp.net the each time you drag any controls it actually creates a object.




Encapsulation:

Encapsulation is a process of binding the data members and member functions.

Example for encapsulation is class. A class can contain data structures and methods.
Consider the following class

public class Aperture
{
public Aperture ()
{
}
protected double height;
protected double width;
protected double thickness;
public double get volume()
{
Double volume=height * width * thickness;
if (volume<0)
return 0;
return volume;
}
}

In this example we encapsulate some data such as height, width, thickness and method Get Volume. Other methods or objects can interact with this object through methods that have public access modifier.


Abstraction , C# has five access Specifiers

Public -- Accessible outside the class through object reference.

Private -- Accessible inside the class only through member functions.

Protected -- Just like private but Accessible in derived classes also through member 
functions.

Internal -- Visible inside the assembly. Accessible through objects.

Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member functions.


Inheritance:


Using System;
Public class BaseClass
{
    Public BaseClass ()
    {
        Console.WriteLine ("Base Class Constructor executed");
    }
                                 
    Public void Write ()
    {
        Console.WriteLine ("Write method in Base Class executed");
    }
}
                                 
Public class ChildClassBaseClass
{
                                 
    Public ChildClass ()
    {
        Console.WriteLine("Child Class Constructor executed");
    }
   
    Public static void Main ()
    {
        ChildClass CC = new ChildClass ();
        CC.Write ();
    }
}

Note: Classes can inherit from multiple interfaces at the same time. 
Interview Question: How can you implement multiple inheritance in C#? Ans : Using Interfaces. We will talk about interfaces in our later article.


Polymorphism:

When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.
Polymorphism is one of the fundamental concepts of OOP.
Polymorphism provides following features: 

  • It allows you to invoke methods of derived class through base class reference during runtime.
  • It has the ability for classes to provide different implementations of methods that are called through the same name. 
  • Polymorphism is of two types:
    1. Compile time polymorphism/Overloading
    2. Runtime polymorphism/Overriding
    Compile Time Polymorphism
    Compile time polymorphism is method and operators overloading. It is also called early binding.
    In method overloading method performs the different task at the different input parameters.
    Runtime Time Polymorphism

  • Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.
    When overriding a method, you change the behavior of the method for the derived class.  Overloadinga method simply involves having another method with the same prototype.
    Caution: Don't confused method overloading with method overriding, they are different, unrelated concepts. But they sound similar.
      In very simple manner u use the same method name for doing a addition function with different parameters is called overloading we use the same method name for different function is called overriding.

All the best.............................................................................









Monday, November 25, 2013

SQLDATAREADER

SQLDATAREADER

Introduction

The sqldatareader is the object we use in the ado.net for accepting or retrieving the row of a table from the database.
The syntax is,

sqldatareader sqlDr = sqlcommandtype.executereader;

the sqldatareader will read the value row by row and we need to open the connection and close the connection.
As we use the void return type in function call, here we need to specify the return type there are three types of return value,
ExecuteNonQuey
ExecuteScalar 
ExecuteReader

The execute reader is used to get the single value from the data table.

The execute scalar is used for the single row retrieval query like aggregate function().

The ExecuteNonQuery will not return the value like void.

example, if we need to get the value of a data cell from one data table,
        sqlconnection con = new sqlconnection("connection string");

        con.open();
        sqlcommand cmd = new sqlcommand("select * from tablename where empid =                                                                                                                                               '100'",con);
        sqldatareader dr = cmd.executereader();
        
        if(dr.read)                                               //if data reader have any data or record  
        {
                label.text =Convert.ToString( dr[0]["columnName"] );                  
         }

        con.Close();



****************************************************************************************************************

        

        

Sunday, November 24, 2013

OBJECTS OF ADO.NET

OBJECTS OF ADO.NET

1. sqlconnection
2.  sqlcommand
3.  sqldatareader
4.  sqldataadapter
5.  dataset 
6.  datatable

as we see the sqlconnection object in the ado.net section here, http://aspfunlearning.blogspot.in/2013/11/adonet-and-its-objects.html

here we see the other objects and its descriptions.

SQLCOMMAND

     The sqlcommand is used to communicate the with the database. we know that sql query is used with database hence that query we use as a parameter along with the connection string.

ex: 
         sqlcommand sqlCom = new sqlcommand(sql query,connectionstring);

here we create an instance of a sqlcommand object with name sqlCom and it will take two parameters sqlquery and connection string.

SQLDATAREADER

               The sqldatareader is used to read the value row by row from the datatable and when we use this we need to specify the con.open() and con.close() after the function.

ex:

               SqlDataReader dr = cmd.ExecuteReader();

SQLDATAADAPTER 

                  The sqldataadapter is act as a bridge between dataset and database. It gets the whole value of a database and stores in a dataset for our operational use, the dataset object we see in next paragraph.

ex:

              sqldataadapter sqlDa = new dataadapter (sqlquery,connection string);

DATASET

               The dataset is the object which used to store the database value to a n no. of data table , the dataset is a collection of data table.

ex: 

          dataset ds = new dataset();   // declaring dataset

          sqlDa.Fill(ds);    // here we fill the dataset with the database value

DATATABLE

                The datatable is a split content or subset of dataset which means collection of data table we use to store it into datatable.

ex:
           datatable dt = new datatable();     //declaring datatable   for single table
           sqlDa.Fill(dt);     // here we fill the datatable with the database  value 

example with all above said ado.net objects, for retrieving the value from a table,

      sqlconnection sqlCon = new sqlCon("Data Source=G-V-R\\SQLEXPRESS;Initial Catalog = RamAnand;Integrated Security=True");

     sqlcommand sqlCom = new sqlcommand("select * from tablename",sqlCon);

    sqldataadapter sqlDa = new dataadapter (sqCom);

    dataset ds = new dataset();

    sqlDa.Fill(ds);

after this step, we have to set or assign this ds value to the gridview we created in the asp.net control like,

gridview.datasource = ds;

gridview1.databind(); 

for sqldatareader see my post http://aspfunlearning.blogspot.in/2013/11/sql-data-reader.html

Thank you for use my post and if you learned something then like my post!

ADO.NET AND ITS OBJECTS

Ado.net and its Objects

Introduction:

The ado.net is a part of .net framework created for accessing the data from the database. It has some objects for various functions whereas here we will discuss about 6 main types of objects we use in ado.net.

Description:

When we use the asp.net,  there we use some controls for some operation or function if that controls related with accessing the database data we use ado.net.
Here we gonna see some of the main types of ado.net objects 

1.  sqlconnection
2.  sqlcommand
3.  sqldatareader
4.  sqldataadapter
5.  dataset 
6.  datatable

SQLCONNECTION

         The sqlconnection is the object in ado.net used to create the connection path to the required database, normally we can find this on sqrserver database connection field, where we use to open the sqlserver management tools like sqlserver 2008.

         Normally in windows authentication, the Sqlconnection or connection string will be like "Data Source=G-V-R\\SQLEXPRESS;Initial Catalog = RamAnand;Integrated Security=True"

whereas if u use server authentication. the Sqlconnection or connection string will be like 
"Data Source=G-V-R\\SQLEXPRESS;Initial Catalog = RamAnand;Uid=sa;Password=DBMS"

here data source is the name of my system, Initial Catalog is the database I want to access.


for other objects visit: http://aspfunlearning.blogspot.in/2013/11/objects-of-adonet.html

thank you, like and comment if any help needed!!!







Saturday, November 23, 2013

Session in asp.net

Session State in asp.net

Introduction:

Hai friends , here I will explain you about the session state in asp.net with c# example.


Description:

As in my previous post I have explained about the query string http://aspfunlearning.blogspot.in/2013/11/aspnet-query-string.html, as I am working in c# programming all my examples I am working out here are with c# programming and VS2010 and sqlserver 2008.

About Session State:

The session allowed us to get the value or information from one page to the other page and also it support for objects, for example if we log in to  a mail website the input we given in login page (user name) will be display in all the pages.

The session id will be generated each time we log in to that website and it expires when we leave that application or site.

See the samples shown here,


Here, on default page I have created one text box and one button of asp control.


On this second page I have created one label to receive the session value..



In the above image, on Default.aspx.cs  coding page on button click event, I am assigning the textbox value to the session .



In the above image, on Page2.aspx.cs coding page I am retrieving the session value as string and assigning it to label.text



Thanks for visiting my page, if you like my post like in google+ and fb like, thanks!





Thursday, November 21, 2013

ASP.net Query String

Asp.net Query String

Introduction:

Hi, friends here I would like to share few things about the asp.net query string.
In email sign up or login we might have seen in url, which carries our typed text box value suffix with ? value
this type of transferring value from one page to another page in web application from url is called query string.

Description: 

syntax: 
                   in first page,
                         Respond.Redirect("Default2.aspx?UserId="+txtUserName.Text);

                    in second page or required page

                 String x = Request.QueryString["UserId"];

In the first page we concatenating the username of one text box with giving userid name, and in the second page we assigning that value or getting that value from url and assigning it to the string x.

There are two types of methods in web application, get method and post method. 
The get method is to receive the value from the server to client and Post method is to post the value to the server for getting some action to be done. 

The query string is like get method.

If you have any question about asp.net or ado.net pls comment, thanks for learning and visit my page.