Shalvin Interests

Monday, January 27, 2014

ASP .Net GridView Editing


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
    <Columns>
         <asp:BoundField DataField="Contactid" HeaderText="CId" ReadOnly="True" />
         <asp:BoundField DataField="contactname" HeaderText="name" />
         <asp:BoundField DataField="Phone" HeaderText="Phone" />
         <asp:CommandField ShowEditButton="True" />
         <asp:CommandField ShowDeleteButton="True" />
    </Columns>
</asp:GridView>



using System.Configuration;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection cnn = null;

    protected void Page_Load(object sender, EventArgs e)
    
    {
        if (!IsPostBack)
        {
            GetData();
        }
    }

    private void GetData()
    {
        using (cnn = new SqlConnection(ConfigurationManager.AppSettings.Get("Cnn")))
        {

            SqlDataAdapter da = new SqlDataAdapter("select ContactId, ContactName, Phone from Contacts", cnn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Co");
            GridView1.DataSource = ds.Tables["Co"];
            DataBind();
        }
    } 
    
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GetData();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        using (cnn = new SqlConnection(ConfigurationManager.AppSettings.Get("Cnn")))
        {
            cnn.Open();
            string id = GridView1.Rows[e.RowIndex].Cells[0].Text;
            string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            string phone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            string strSql = "update Contacts set ContactName = '" + name + "', phone = '" +
                phone + "' where ContactId = " + id;

            SqlCommand cmd = new SqlCommand(strSql, cnn);
            cmd.ExecuteNonQuery();
            GridView1.EditIndex = -1;
            GetData();
        }

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

Saturday, January 25, 2014

C# Method Overloading


 public void Display()
  {
    Response.Write("Hello " + "<br/>");
  }
  public void Display(string Name)
  {
    Response.Write("Hello " + Name + "<br/>");
  }
  public void Display(string Name, string Location)
  {
    Response.Write("Hello " + Name + " located at " + Location + "<br/>");
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    Display();
  }
  protected void Button2_Click(object sender, EventArgs e)
  {
    Display("Aiju");
  }
  protected void Button3_Click(object sender, EventArgs e)
  {
    Display("Sarath", "Trissur");
  }

C# Abstract Class


public abstract class CalcAb
{
  public abstract int Add(int i, int j);

  public int Subtract(int i, int j)
  {
    return i - j;
  }
}

public class Calc : CalcAb
{
  public override int Add(int i, int j)
  {
    return i + j;
  }
}

protected void Page_Load(object sender, EventArgs e)
{
  Calc c = new Calc();
  Response.Write(c.Add(78, 56));
  Response.Write("<br/>");
  Response.Write(c.Subtract(78, 67));
}