Sunday, 8 May 2011

Abstract classes v/s Interfaces

This article explain the scenerios when to use abstract classes and when interfaces.


Use an abstract class



  1. Use an abstract class to define a common base class for a family of types.

  2. Use an abstract class to provide default behavior.

  3. When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning.

  4. Subclass only a base class in a hierarchy to which the class logically belongs.

Use and Interface



  1. When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.

  2. Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.

  3. Use an interface to design a polymorphic hierarchy for value types.

  4. Use an interface when an immutable contract is really intended.

  5. A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.

Monday, 17 September 2007

GridView Functionality :Edit, Update, Delete

To connect to database :

protected void Page_Load(object sender, EventArgs e)
{
string constr = "server=servername;database=databasename;integrated security=sspi";
con = new SqlConnection(constr);
da = new SqlDataAdapter("select * from employee", con);

DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}


To Edit into GridView :

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
t1 = new TextBox();
t2 = new TextBox();
t1.Text = GridView1.Rows[e.NewEditIndex].Cells[0].Text;
t2.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
GridView1.DataBind();
}


To cancel the Gridview changes:

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}


To delete a row in Gridview :

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//da = new SqlDataAdapter("delete * from employee where empid =" + e.RowIndex ,con);
string str = GridView1.Rows[e.RowIndex].Cells[0].Text;
int delVal = Convert.ToInt32(str);
con.Open();
SqlCommand cmd = new SqlCommand("delete from employee where empid = "+ delVal , con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind();
}


To Update the Gridview :

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
if (row != null)
{
TextBox t = new TextBox();
t = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
if (t != null)
{
con.Open();
string str1 = "Update employee set empname = '" + t.Text + "' where empid = 59";
SqlCommand cmd = new SqlCommand( str1 , con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind();
}
}
}