Monday, December 24, 2012

What is Inheritance?


What is Inheritance?

Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (concept) of object-oriented programming Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes The Class whose methods and variables are defined is called super class or base class The Class that inherits methods and variables are defined is called sub class or derived class Sometimes base class known as generalized class and derived class known as specialized class Keyword to declare inheritance is ":" (colon) in visual C#.

Benefits of using Inheritance

Once a behavior (method) or property is defined in a super class(base class),that behavior or property is automatically inherited by all subclasses (derived class). Code reusability increased through inheritance Inheritance provide a clear model structure which is easy to understand without much complexity Using inheritance, classes become grouped together in a hierarchical tree structure Code are easy to manage and divided into parent and child classes

Example of Inheritance

Types of Inheritance in C#: -

  • Implementation Inheritance
  • Multiple Inheritances (Interface Inheritance)

Implementation Inheritance

One base class (super class) and one derived class (sub class).

Example:

 


Interface Inheritance

An interface looks like a class, but has no implementation. It contains definitions of events, indexers, methods and properties. An interface inherited by classes An interface inheritance defined with keyword "interface". In C# Interface Inheritance also known as multiple inheritances.

Inheritance Tree Structure:

 

Sunday, December 23, 2012

Difference between Abstract Class and Interface


  • An abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class.

An example of interface:

interface iSampleInterface
{
  //All methods are automaticall abstract
  int AddNumbers(int Num1, int Num2);
  int MultiplyNumbers(int Num1, int Num2);
}



  • Defining an abstract class with abstract members has the same effect to defining an interface.
  • The members of the interface are public with no implementation. Abstract classes can have protected parts, static methods, etc.
  • A class can inherit one or more interfaces, but only one abstract class.
  • Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes.
  • The selection of interface or abstract class depends on the need and design of your project. You can make an abstract class, interface or combination of both depending on your needs.

What is delegate? how to use Delegates ?


What is delegate?
Delegates are objects you can use to call the methods of other objects.
A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure. The type of a delegate is defined by the name of the delegate.
Once a delegate is assigned a method, it behaves exactly like that method.
The delegate method can be invoked like any other method, with parameters and a return value.
Any method from any accessible class or struct that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate.
The method can be either static or an instance method. This makes it possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the signature of the delegate, you can assign your own delegated method.
Delegates have the following properties:
Delegates are like C++ function pointers but are type safe.
Delegates allow methods to be passed as parameters.
Delegates can be used to define callback methods.
Delegates can be chained together; for example, multiple methods can be called on a single event.
Methods do not have to match the delegate signature exactly.

how to use Delegates ?


or



1. Defining the delegate
 
public delegate int Calculate (int value1, int value2);
 2. Creating methods which will be assigned to delegate object
 
//a method, that will be assigned to delegate objects
//having the EXACT signature of the delegatepublic int add(int value1, int value2)
{
    return value1 + value2;            
}//a method, that will be assigned to delegate objects
//having the EXACT signature of the delegatepublic int sub( int value1, int value2)
{
    return value1 - value2;            
}
 3. Creating the delegate object and assigning methods to those delegate objects
 
//creating the class which contains the methods 
//that will be assigned to delegate objectsMyClass mc = new MyClass();
//creating delegate objects and assigning appropriate methods
//having the EXACT signature of the delegateCalculate add = new Calculate(mc.add);
Calculate sub = new Calculate(mc.sub);
 4. Calling the methods via delegate objects
 
//using the delegate objects to call the assigned methods Console.WriteLine("Adding two values: " + add(10, 6));
Console.WriteLine("Subtracting two values: " + sub(10,4));

Monday, November 19, 2012

3 tier architecture


using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Data;
using System.Collections;
DAL:

namespace DAL
{
   public class DbConnection
    {
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter adp;
        StringBuilder sb;
        SqlDataReader _rdr;

        public DbConnection()
        {
            con = new SqlConnection(ConfigurationSettings.AppSettings["conn"].ToString());
            sb = new StringBuilder("");
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        public void OpenDbConnection()
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
        }
        public void CloseDbConnection()
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
        public SqlCommand GetDbCommand()
        {
            if (cmd != null)
            {
                return cmd;
            }
            else
            {
                cmd = new SqlCommand();
                cmd.Connection = con;
                return cmd;
            }
        }
        public int ExecuteNonQuery(string _spname, SqlParameter[] sp_param)
        {
            OpenDbConnection();
            cmd = GetDbCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = _spname;
            foreach (SqlParameter _parm in sp_param)
            {
                cmd.Parameters.Add(_parm);
            }
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        public ArrayList GetAdminLogininfo(string username, string password)
        {
            OpenDbConnection();
            cmd = GetDbCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Pipe_SP_AdminLogin";
            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Parameters.Add("@returnVar", SqlDbType.Bit);
            cmd.Parameters["@returnVar"].Direction = ParameterDirection.Output;
            cmd.Parameters.Add("@ID", SqlDbType.Int);
            cmd.Parameters["@ID"].Direction = ParameterDirection.Output;
                 
            cmd.ExecuteNonQuery();
            ArrayList result = new ArrayList();
            result.Add(cmd.Parameters["@returnVar"].Value);
         
            result.Add(cmd.Parameters["@ID"].Value);
     

            return result;
       
                }
        public ArrayList GetRepLogininfo(string username, string password)
        {
            OpenDbConnection();
            cmd = GetDbCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Pipe_SP_RepLogin";
            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Parameters.Add("@returnVar", SqlDbType.Bit);
            cmd.Parameters["@returnVar"].Direction = ParameterDirection.Output;
            cmd.Parameters.Add("@ID", SqlDbType.Int);
            cmd.Parameters["@ID"].Direction = ParameterDirection.Output;

            cmd.ExecuteNonQuery();
            ArrayList result = new ArrayList();
            result.Add(cmd.Parameters["@returnVar"].Value);

            result.Add(cmd.Parameters["@ID"].Value);


            return result;

        }
        public ArrayList GetClientLogininfo(string Email, string password)
        {
            OpenDbConnection();
            cmd = GetDbCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SUB_SP_GetClientLoginInfo";
            cmd.Parameters.AddWithValue("@Email", Email);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Parameters.Add("@returnVar", SqlDbType.Bit);
            cmd.Parameters["@returnVar"].Direction = ParameterDirection.Output;
            cmd.Parameters.Add("@ClientID", SqlDbType.Int);
            cmd.Parameters["@ClientID"].Direction = ParameterDirection.Output;
            cmd.Parameters.Add("@OrgId", SqlDbType.VarChar, 50);
            cmd.Parameters["@OrgId"].Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            ArrayList result = new ArrayList();
            result.Add(cmd.Parameters["@returnVar"].Value);
            result.Add(cmd.Parameters["@ClientID"].Value);
            result.Add(cmd.Parameters["@OrgID"].Value);

            return result;
        }



        public DataTable ExecuteDataTable(string _spname, SqlParameter[] sp_param)
        {
            OpenDbConnection();
            cmd = GetDbCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = _spname;
            if (sp_param != null)
            {
                foreach (SqlParameter _parm in sp_param)
                {
                    cmd.Parameters.Add(_parm);
                }
            }
            DataSet dt = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            adp.Dispose();
            cmd.Dispose();
            CloseDbConnection();
            return dt.Tables[0];
        }
        public int ExecuteScalar(string _sql)
        {
            OpenDbConnection();
            cmd = GetDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = _sql;
            int count = int.Parse(cmd.ExecuteScalar().ToString());
            cmd.Dispose();
            CloseDbConnection();
            return count;
        }
        public SqlDataReader ExecuteReader(string _sql)
        {
            OpenDbConnection();
            cmd = GetDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = _sql;
            _rdr = cmd.ExecuteReader();
            return _rdr;
        }
        public DataTable ExecuteDT(string _Sql)
        {
            cmd = GetDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = _Sql;
            DataSet dt = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            adp.Dispose();
            cmd.Dispose();
            return dt.Tables[0];
        }
        //public SqlDataAdapter GetDataAdaptor()
        //{
        //    if (adp != null)
        //    {
        //        return adp;
        //    }
        //    else
        //    {
        //        adp = new SqlDataAdapter(;

        //    }
        //}
    }


}


BAL :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DAL;

namespace BAL
{
   public class ClsAddLeads
    {

        #region "Properties"

        public int ID { get; set; }
        public int RepsID { get; set; }
        public int LeadsID { get; set; }
        public DateTime DateAdded { get; set; }
        public string Fname { get; set; }
        public string Contact { get; set; }
        public string Position { get; set; }
        public DateTime LastContactDate { get; set; }
        public string Phone { get; set; }
        public string LegacyPartner { get; set; }
        public string Investment { get; set; }
        public string Notes { get; set; }  
        public string Mode { get; set; }

        #endregion

        #region "All Insert,Update,Delete operation are done"
        public int Leads()
        {
            DbConnection _clsdb = new DbConnection();
            SqlParameter[] _sp = new SqlParameter[11];
            _sp[0] = new SqlParameter("@ID", ID);
            _sp[1] = new SqlParameter("@RepsID", RepsID);
            _sp[2] = new SqlParameter("@Fname", Fname);
            _sp[3] = new SqlParameter("@Contact", Contact);
            _sp[4] = new SqlParameter("@Position", Position);
            _sp[5] = new SqlParameter("@LastContactDate",Convert.ToDateTime(LastContactDate));
            _sp[6] = new SqlParameter("@Phone", Phone);
            _sp[7] = new SqlParameter("@LegacyPartner", LegacyPartner);
            _sp[8] = new SqlParameter("@Investment", Investment);
            _sp[9] = new SqlParameter("@Notes", Notes);
            _sp[10] = new SqlParameter("@mode", Mode);
            
            int i = _clsdb.ExecuteNonQuery("Pipe_SP_Leads", _sp);
            return i;
        }

        public int AddRepsLeads()
        {
            DbConnection _clsdb = new DbConnection();
            SqlParameter[] _sp = new SqlParameter[5];
            _sp[0] = new SqlParameter("@ID", ID);
            _sp[1] = new SqlParameter("@RepsID", RepsID);
            _sp[2] = new SqlParameter("@LeadsID",LeadsID);
            _sp[3] = new SqlParameter("@DateAdded", DateAdded);
            _sp[4] = new SqlParameter("@mode", Mode);

            int i = _clsdb.ExecuteNonQuery("Pipe_SP_Leads", _sp);
            return i;
        }
        #endregion


        #region "LoadAll Leads Details"
        public DataTable GetLeads()
        {
            string sql = "select * from Pipe_TBL_AddLeads where Fname='" + Fname + "'";
            DbConnection _clsdb = new DbConnection();
            DataTable dt = _clsdb.ExecuteDT(sql);

            return dt;
        }

        public DataTable GetReps()
        {
            string sql = "select * from Pipe_TBL_RepsLeads where LeadsID='"+ LeadsID +"' order By ID ";
            DbConnection _clsdb = new DbConnection();
            DataTable dt = _clsdb.ExecuteDT(sql);

            return dt;
        }

        #endregion

        #region "All Insert,Update,Delete operation are done"
        public int DeleteLeads()
        {
            DbConnection _clsdb = new DbConnection();
            SqlParameter[] _sp = new SqlParameter[11];
            _sp[0] = new SqlParameter("@ID", ID);
            _sp[1] = new SqlParameter("@Fname", Fname);
            _sp[2] = new SqlParameter("@Contact", Contact);
            _sp[3] = new SqlParameter("@Position", Position);
            _sp[4] = new SqlParameter("@LastContactDate", null);
            _sp[5] = new SqlParameter("@Phone", Phone);
            _sp[6] = new SqlParameter("@LegacyPartner", LegacyPartner);
            _sp[7] = new SqlParameter("@Investment", Investment);
            _sp[8] = new SqlParameter("@Notes", Notes);
            _sp[9] = new SqlParameter("@mode", Mode);
            _sp[10] = new SqlParameter("@RepsID", RepsID);
            int i = _clsdb.ExecuteNonQuery("Pipe_SP_Leads", _sp);
            return i;
        }
        #endregion



        #region "LoadAll Leads Details"
        public DataTable LeadsDetails()
        {
            string sql = "select * from Pipe_TBL_AddLeads  order by ID";
            DbConnection _clsdb = new DbConnection();
            DataTable dt = _clsdb.ExecuteDT(sql);

            return dt;
        }

        #endregion

        #region "LoadAll Leads Details"
        public DataTable ShowLeads()
        {
            string sql = "select C.LeadsID,C.RepsID,A.ID,A.fname,A.Position,A.phone from Pipe_TBL_AddLeads as A , Pipe_TBL_RepsLeads as C where C.LeadsID=A.ID and  C.RepsID='" + RepsID + "' order by A.ID";
            //string sql = "select A.ID,A.RepsID,B.Fullname,A.fname,A.Position,A.phone from Pipe_TBL_AddLeads as A , Pipe_TBL_AddReps as B where A.RepsID=B.ID and A.RepsID='" + RepsID + "' order by A.ID";
            DbConnection _clsdb = new DbConnection();
            DataTable dt = _clsdb.ExecuteDT(sql);

            return dt;
        }

        #endregion

  

        #region "LoadAll Leads details by ID"
        public DataTable Leadsdata()
        {
            string sql = "select * from Pipe_TBL_AddLeads where ID='" + ID + "'";
            DbConnection _clsdb = new DbConnection();
            DataTable dt = _clsdb.ExecuteDT(sql);
            return dt;
        }

        #endregion


        #region "LoadAll Leads details by RepsID for Reports"
        public DataTable LeadsReport()
        {
           // string sql = "select ID,RepsID,fname,Position,CONVERT(VARCHAR(12),LastContactDate, 106) as LastContactDate,Phone ,Contact,LegacyPartner,Investment,Notes from Pipe_TBL_AddLeads where RepsID='" + RepsID + "'";

            string sql = "select C.LeadsID,C.RepsID,A.ID,A.fname,A.Position,A.phone,CONVERT(VARCHAR(12),A.LastContactDate, 106) as LastContactDate,Contact,LegacyPartner,Investment,Notes from Pipe_TBL_AddLeads as A , Pipe_TBL_RepsLeads as C where C.LeadsID=A.ID and  C.RepsID='" + RepsID + "' order by A.ID";
            DbConnection _clsdb = new DbConnection();
            DataTable dt = _clsdb.ExecuteDT(sql);
            return dt;
        }

        #endregion

        #region "LoadAll Leads details by RepsID"
        public DataTable LeadsdataByRepsID()
        {
            //string sql = "select A.ID,A.RepsID,B.Fullname,A.fname,A.Position,A.phone from Pipe_TBL_AddLeads as A , Pipe_TBL_AddReps as B where A.RepsID=B.ID and RepsID='" + RepsID + "' order by A.ID";
            string sql = "select C.LeadsID,C.RepsID,A.ID,A.fname,A.Position,A.phone from Pipe_TBL_AddLeads as A , Pipe_TBL_RepsLeads as C where C.LeadsID=A.ID and C.RepsID='" + RepsID + "' order by A.ID";

            DbConnection _clsdb = new DbConnection();
            DataTable dt = _clsdb.ExecuteDT(sql);
            return dt;
           
        }

        #endregion

        #region "Load Reps Name for reports"
        public DataTable RepsName()
        {
            string sql = "SELECT DISTINCT A.FullName as FullName FROM Pipe_TBL_AddReps as A,Pipe_TBL_AddLeads as B where B.RepsID=A.ID ORDER BY A.FullName";
            DbConnection _clsdb = new DbConnection();
            DataTable dt = _clsdb.ExecuteDT(sql);
            return dt;
        }

        #endregion

    }
}


PL:


   ClsAddLeads oRL = new ClsAddLeads();
                            oRL.RepsID = Convert.ToInt16(RepsselectedItem);
                            oRL.LeadsID =Convert.ToInt16(ViewState["ID"]);
                            oRL.DateAdded = DateTime.Now;
                            oRL.Mode = "RepsLeads";
                            int ok = oRL.AddRepsLeads();
                            if (ok == -1)
                            {

                                lblResult.Visible = true;
                                lblResult.ForeColor = Color.Green;
                                lblResult.Text = "Save successful ";
                            }





Friday, November 2, 2012

Remember me functionality in asp.net with VB.net using Cookies


 use the below code to save the userid and password deatils in cookies 

on login button:-

 If CheckBox1.Checked = True Then
                Response.Cookies("UName").Value = txtUserID.Text
                Response.Cookies("PWD").Value = txtPwd.Text
                Response.Cookies("UName").Expires = DateTime.Now.AddMonths(2)
                Response.Cookies("PWD").Expires = DateTime.Now.AddMonths(2)
End If


use the below code to Retrive the userid and password deatils From cookies 


on page load :-

  If Not IsPostBack Then
            If Request.Cookies("UName") IsNot Nothing Then
                txtUserID.Text = Request.Cookies("UName").Value
            End If
            If Request.Cookies("PWD") IsNot Nothing Then
                Dim pass As TextBox = DirectCast(Me.FindControl("txtPwd"), TextBox)
                pass.Attributes.Add("value", Request.Cookies("PWD").Value)
            
            End If
            If Request.Cookies("UName") IsNot Nothing AndAlso Request.Cookies("PWD") IsNot Nothing Then
                CheckBox1.Checked = True
            End If
        End If

Tuesday, May 8, 2012

How to clear a Multiple TextBox values in a single click in C# .NET


void ClearInputs(ControlCollection ctrls)
 {
        foreach (Control ctrl in ctrls)
        {
            if (ctrl is TextBox)
                ((TextBox)ctrl).Text = string.Empty;
            ClearInputs(ctrl.Controls);
        }
 }


protected void BtnSave_Click(object sender, EventArgs e)
{
           ClearInputs(Page.Controls);
}

Thursday, March 22, 2012

How to copy data from one table to another table in sql server

Syntax:- select * into NewTableName from ExistingTableName


Example:- select * into employee1 from employee


Note: where employee is the existing table. this query create a new table (employee1) and copy all the data of employee table in newly created table


        I hope this will help you!!