Unni R

Unni R

  • NA
  • 16
  • 11.2k

Save temporary values in gridview to database

Apr 28 2015 2:04 AM
I have one grid view , two text box ,two button and one drop down box ,   while  am entering data to text box and  drop down boxes (inside the grid view),that  data want to store temporally in grid view after that  data want to store in database.My code is attached here 
  public static DataTable dt;
    static int C = 0;


    protected void Buttonadd_Click(object sender, EventArgs e)
    {
 if (Session["Data"] == null)
        {


            C = 1;


            dt = new DataTable();
            DataRow dr = dt.NewRow();
            dt.Columns.Add(new System.Data.DataColumn("EMPNAME", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("EMPID", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("DEPARTMENT", typeof(String)));
            dr[0] = Txtaddname.Text;
            dr[1] = Txtaddempid.Text;
            dr[2] = Drpadddept.SelectedItem.Text;
            dt.Rows.Add(dr);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            Session["Data"] = dt;


        }
        else
        {
            C = C + 1;


            dt = new DataTable();
            dt = (DataTable)Session["Data"];
            DataRow dr = dt.NewRow();
            dr[0] = Txtaddname.Text;
            dr[1] = Txtaddempid.Text;
            dr[2] = Drpadddept.SelectedItem.Text;
            dt.Rows.Add(dr);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            Session.Remove("Data");
            Session["Data"] = dt;



Answers (4)