Thursday, January 12, 2012

Gridview new Row insert using commandbuilder object in ado.net

public DataSet dsa;
SqlDataAdapter da;
public DataSet ds=new DataSet();
public DataSet ds1;
UserRegistration Obj = new UserRegistration();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//binddrop();
bindgrid();
}
}



public void bindgrid()
{
SqlConnection source_conn = new SqlConnection("Data Source=BSZ123\\SQLEXPRESS;Initial Catalog=Ecom_DB;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT * FROM emp", source_conn);
da = new SqlDataAdapter(cmd);


da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
DataBind();
// ViewState["ad"] = da;
ViewState["chan"] = ds;
}



protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//bindgrid();
if (e.CommandName == "add")
{
GridView1.ShowFooter = true;

bindgrid();


}
else if (e.CommandName == "insert")
{
//postbind();
//GridView1.DataBind();

//da = ((SqlDataAdapter)(ViewState["ad"]));
SqlConnection source_conn = new SqlConnection("Data Source=BSZ123\\SQLEXPRESS;Initial Catalog=Ecom_DB;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT * FROM emp", source_conn);
da = new SqlDataAdapter(cmd);


da.Fill(ds);
//ds=((DataSet)(ViewState["chan"]));
DataRow dr = ds.Tables[0].NewRow();

dr[0] = Convert.ToInt32( (((TextBox)GridView1.FooterRow.FindControl("TextBox4")).Text));
dr[1] = ((TextBox)GridView1.FooterRow.FindControl("TextBox40")).Text;
dr[2] = ((TextBox)GridView1.FooterRow.FindControl("TextBox41")).Text;


ds.Tables[0].Rows.Add(dr);


SqlCommandBuilder ba = new SqlCommandBuilder(da);
da.Update(ds.Tables[0]);
GridView1.DataSource = ds.Tables[0].DefaultView;
DataBind();


//bindgrid();



}
GridView1.ShowFooter = false;

}

___________ .aspx page code

__________-snapshot

No comments: