11 November 2011

When to Use Abstract class?


    When to Use Abstract class?

·         If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. 
·         Abstract classes should be used primarily for objects that are closely related.
·        If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components

10 November 2011

When to use Interface in C#?

When to use Interface  in C#?



·         Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
·        Structures cannot inherit from classes, but they can implement interfaces.
·         Interfaces are best suited for providing common functionality to unrelated classes.
·    If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. 

Suppose a business man has two accounts:
1.       Business Account
2.        Personal Account
You might create a standard int
erface named IAccount, which included PostInterest and DeductFees methods:
// C#
// Code for the IAccount interface module.
public interface IAccount
{
   void PostInterest();
   void DeductFees();
}

Both Business Account and Personal Account inherit the above interface and implement the method. Both Classes have implement two function and the function change frequently according to bank policies. In those situations Interface is used.
When the function frequently changing and the common function is used for unrelated class, Interfaces is used

References:

Get the Selected row value in Gridview C#


Get the Selected row value in Gridview C#

string cellValue = mydatagrid["columnName",rowindex].Value.ToString();


ConnectionString for Sql Server authentication and Windows Authentication

ConnectionString for Sql Server authentication

      public string connStr = "Server=USER-PC\\SQLEXPRESS;Database=StudentDetails;UId=sa;pwd=sa;Integrated Security=false";



ConnectionString for Windows Authentication



 public string connStr = "Server=USER-PC\\SQLEXPRESS;Database=StudentDetails;Integrated Security=true";

7 November 2011

Delegates in C#

Delegates in C#


A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++.


Declaring Delegates



public delegate void Del(string message);
Once a delegate is instantiated, a method call made to the delegate will be passed by the delegate to that method.



// Create a method for a delegate.
public static void DelegateMethod(string message)
{
    System.Console.WriteLine(message);
}

// Instantiate the delegate.
Del handler = DelegateMethod;

// Call the delegate.
handler("Hello World");

Sources : http://msdn.microsoft.com/en-us/library/ms173172(v=vs.80).aspx

Connectivity in C# and Sql Server Stored Procedure


Connectivity in C# and Sql Server Stored Procedure
Insertion in C#


        /// Used to insert records into database

          public int Insert(string firstName, string lastName, int age)

        {

            SqlConnection conn = new SqlConnection(connStr);

            conn.Open();

            SqlCommand dCmd = new SqlCommand("InsertData", conn);

            dCmd.CommandType = CommandType.StoredProcedure;

            try

            {

                dCmd.Parameters.AddWithValue("@firstName", firstName);

                dCmd.Parameters.AddWithValue("@lastName", lastName);

                dCmd.Parameters.AddWithValue("@age", age);

                return dCmd.ExecuteNonQuery();

            }

            catch

            {

                throw;

            }

            finally

            {

                dCmd.Dispose();

                conn.Close();

                conn.Dispose();

            }

        }
Updation in C#


  /// 

        /// Update record into database


        public int Update(int personID, string firstName, string lastName, int age)

        {

            SqlConnection conn = new SqlConnection(connStr);

            conn.Open();

            SqlCommand dCmd = new SqlCommand("UpdateData", conn);

            dCmd.CommandType = CommandType.StoredProcedure;

            try

            {

                dCmd.Parameters.AddWithValue("@firstName", firstName);

                dCmd.Parameters.AddWithValue("@lastName", lastName);

                dCmd.Parameters.AddWithValue("@age", age);

                dCmd.Parameters.AddWithValue("@personID", personID);

                return dCmd.ExecuteNonQuery();

            }

            catch

            {

                throw;

            }

            finally

            {

                dCmd.Dispose();

                conn.Close();

                conn.Dispose();

            }

        }


Load and Connectivity

        /// 

        /// Load all records from database


        public DataTable Load()

        {

            SqlConnection conn = new SqlConnection(connStr);

            SqlDataAdapter dAd = new SqlDataAdapter("LoadAll", conn);

            dAd.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataSet dSet = new DataSet();

            try

            {

                dAd.Fill(dSet, "PersonTable");

                return dSet.Tables["PersonTable"];

            }

            catch

            {

                throw;

            }

            finally

            {

                dSet.Dispose();

                dAd.Dispose();

                conn.Close();

                conn.Dispose();

            }

        }


Deletion in C# 



        /// Delete record from database

        public int Delete(int personID)

        {

            SqlConnection conn = new SqlConnection(connStr);

            conn.Open();

            SqlCommand dCmd = new SqlCommand("DeleteData", conn);

            dCmd.CommandType = CommandType.StoredProcedure;

            try

            {

                dCmd.Parameters.AddWithValue("@personID", personID);

                return dCmd.ExecuteNonQuery();

            }

            catch

            {

                throw;

            }

            finally

            {

                dCmd.Dispose();

                conn.Close();

                conn.Dispose();

            }

        }



    }
Resources:http://www.dotnetfunda.com/articles/article71.aspx


Edit the Gridview in Asp.net with List Collection Example

   Edit the Gridview in Asp.net with List Collection Example

           
       
  ASPX.CS

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    public void BindData()
    {
        peopleGridView.DataSource = GetPersonList();
        peopleGridView.DataBind();
    }

    protected void selectButton_Click(object sender, EventArgs e)
    {
        LinkButton selectButton = (LinkButton)sender;
        //the button is contained in a TableCell which is contained in a GridViewRow
        GridViewRow row = (GridViewRow)selectButton.Parent.Parent;
        //get the row index of the selected row
        int rowIndex = row.RowIndex;

        //call our populate function
        PopulateLabelsByRowIndex(rowIndex);

    }
    protected void peopleGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //get our new selected index
        int rowIndex = e.NewSelectedIndex;

        //call our populate function
        PopulateLabelsByRowIndex(rowIndex);
    }
    private void PopulateLabelsByRowIndex(int rowIndex)
    {
        //assign the row number to our message label
        messageLabel.Text = String.Format("{0}", rowIndex + 1);

        //display the value on column 2 of the selected row
        nameLabel.Text = peopleGridView.Rows[rowIndex].Cells[2].Text;
        //display the value on column 3 of the selected row
        emailLabel.Text = peopleGridView.Rows[rowIndex].Cells[3].Text;
        //display the value on column 4 of the selected row
        ageLabel.Text = peopleGridView.Rows[rowIndex].Cells[4].Text;
    }

    public List GetPersonList()
    {
        //create a new list of person
        List people = new List();
     
        people.Add(new Person(1, "Arun Prakash", "apkmca@gmail.com", 28));
        people.Add(new Person(2, "Sachin", "sachin@Cricketgod.org", 38));
        people.Add(new Person(3, "Ivy Rull", "ivy@devpinoy.org", 24));
        people.Add(new Person(4, "Orlando Rull", "orlando@devpinoy.org", 52));
        people.Add(new Person(5, "Benilda Rull", "benilda@devpinoy.org", 49));
        people.Add(new Person(6, "Ria Rull", "ria@devpinoy.org", 22));
        people.Add(new Person(7, "Renz Rull", "renz@devpinoy.org", 20));

        //return our list
        return people;
    }
}

public class Person
{
 
    public int PersonID
    {
        get;

        set;

    }
    public string Name
    {
        get;

        set;

    }
    public string Email
    {
        get;
 
       set;
     
    }
    public int Age
    {
        get;

        set;
   
    }

    public Person(int personID, string name, string email, int age)
    {
        this.PersonID = personID;
        this.Name = name;
        this.Email = email;
        this.Age = age;
    }
}

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...