Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
code is below attached

C#
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();
               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;
Posted
Updated 24-Apr-15 23:29pm
v2
Comments
Mehdi Gholam 25-Apr-15 2:36am    
...replacement of what?
OriginalGriff 25-Apr-15 4:34am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So don't just throw an unformatted code dump at us, explain your problem!
Use the "Improve question" widget to edit your question and provide better information.
Sinisa Hajnal 25-Apr-15 4:45am    
It is overwriting because you're setting gridview datasource always to the table with single row. See solution.

Griff is right, this is badly formed question. Luckily for you, it is also very simple problem.

1 solution

C#
// create table outside of the loop, at Session start if you need it that long or at page load if not.
DataTable dt = new DataTable
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)));

Session["Data"] = dt; // assign empty table to session
int C = 0; // also not needed in the code

// this code then goes at the point where you're adding data to the gridview (some button handler?
DataTable tmp = CType(Session["Data"], DataTable);// you know it exists (although another check won't hurt
DataRow dr =  tmp.NewRow; 
dr["EMPNAME"] = Txtaddname.Text;
dr["EMPID"] = Txtaddempid.Text;
dr["DEPARTMENT"] = Drpadddept.SelectedItem.Text; // use column names instead of indexes, that way if / when  you add new columns you don't have to change anything

gvEmployees.DataSource = tmp; // it is good practice to give all your controls meaningful names, not leave them at default, otherwise you'll be asking yourself what is in TextBox11 or what does Button44 do in couple of months :)
gvEmployees.DataBind();
Session["Data"] = tmp; // I don't think this is needed anymore


Alternative to this, you could use DataTable.Merge method to add your single row table (the way you're doing it) to gridview datasource.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900