Wednesday, June 24, 2009

Update in Repeater



Code

public partial class Default2 : System.Web.UI.Page
{
Button b1, b2, b3;
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=abhishek;Persist Security Info=True;uid=sa;pwd=com");
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bindgrid();
}


}

public void bindgrid()
{
SqlDataAdapter da = new SqlDataAdapter("select * from ab", conn);
DataSet ds = new DataSet();
da.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
TextBox t1, t2;
if (e.CommandName =="Edit")
{
t1 = (TextBox)Repeater1.Items[e.Item.ItemIndex].FindControl("id1");
t2 = (TextBox)Repeater1.Items[e.Item.ItemIndex].FindControl("name");
t1.ReadOnly = false;
t2.ReadOnly = false;

b1 = (Button)e.Item.FindControl("b1");
b1.CommandName = "update";
b1.Text = "update";

}
else if (e.CommandName == "update")
{
t1 = (TextBox)Repeater1.Items[e.Item.ItemIndex].FindControl("id1");
t2 = (TextBox)Repeater1.Items[e.Item.ItemIndex].FindControl("name");
int i = int.Parse(t1.Text);


cmd.Connection = conn;
conn.Open();
cmd.CommandText="update ab set name='"+t2.Text+"' where id ='"+i+"'";
cmd.ExecuteNonQuery();
Repeater1.DataBind();
conn.Close();
bindgrid();



}
if (e.CommandName == "Insert")
{
b3 = (Button)e.Item.FindControl("b3");
b3.Text = "save";
t1 = (TextBox)Repeater1.Items[e.Item.ItemIndex].FindControl("id1");
t2 = (TextBox)Repeater1.Items[e.Item.ItemIndex].FindControl("name");
t1.ReadOnly = false;
t2.ReadOnly = false;
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "insert into ab values('" + t1.Text + "','" + t2.Text + "')";
cmd.ExecuteNonQuery();
conn.Close();
bindgrid();

}
}




Code

0 comments:

Post a Comment

plzz give the comment