Help me in Datagrid with Static Datatable object
-
Hi My boss asked me to develop an application where it uses datagrid and left most corner I will have "+" and "-" image buttons. On click of "+", a blank record need to be added into datagrid, so that user can insert a new record. Onclick of "-", it should delete the particular record. Well I implemented the same. But I used Static datatable to insert new record, so that the value will persist across post backs. Everything worked fine, as soon I started testing I found a big error. While two persons starts working in same page from different PCs, other person's insertions are visible to the first person. I understood its because of static. The static datatable is being shared across the users!!. But I gone so long that I can not think of going back now. I have done around 15 pages in the same way!!!, so going backward I can not even imagine. Is there any alternative?, instead of using static datatable, can I use any other alternative? below is my code snippet //Declared as class variable static DataTable dt; //// When user clicks "+" or "-" button in datagrid private void dgGoals_ItemCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e) { //Each row in datagrid will have + and - imagebuttons at left most column //Updates the datatable updateDataTable(); //IF the user has clicked "+" image in datagrid right most column if(e.CommandName.ToString()=="+") { // Adds a new row to datatable, so that after binding // to datatagrid, it shows a blank row in datagrid DataRow drow=dt.NewRow(); dt.Rows.Add(drow); updateDataTable(); bindData(); } //If the user clicks "-" image button in datagrid's left most column else if(e.CommandName.ToString()=="-") { //The first column of Datagrid has serial number(1,2,3,4...) //If the user clicks "-" it removes row at that position dt.Rows.RemoveAt(Convert.ToInt32(e.Item.Cells[0].Text)-1); } bindData(); } } } /* Update the datatable before moving to some other page in Datagrid etc*/ private void updateDataTable() { for(int k=0;k
-
Hi My boss asked me to develop an application where it uses datagrid and left most corner I will have "+" and "-" image buttons. On click of "+", a blank record need to be added into datagrid, so that user can insert a new record. Onclick of "-", it should delete the particular record. Well I implemented the same. But I used Static datatable to insert new record, so that the value will persist across post backs. Everything worked fine, as soon I started testing I found a big error. While two persons starts working in same page from different PCs, other person's insertions are visible to the first person. I understood its because of static. The static datatable is being shared across the users!!. But I gone so long that I can not think of going back now. I have done around 15 pages in the same way!!!, so going backward I can not even imagine. Is there any alternative?, instead of using static datatable, can I use any other alternative? below is my code snippet //Declared as class variable static DataTable dt; //// When user clicks "+" or "-" button in datagrid private void dgGoals_ItemCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e) { //Each row in datagrid will have + and - imagebuttons at left most column //Updates the datatable updateDataTable(); //IF the user has clicked "+" image in datagrid right most column if(e.CommandName.ToString()=="+") { // Adds a new row to datatable, so that after binding // to datatagrid, it shows a blank row in datagrid DataRow drow=dt.NewRow(); dt.Rows.Add(drow); updateDataTable(); bindData(); } //If the user clicks "-" image button in datagrid's left most column else if(e.CommandName.ToString()=="-") { //The first column of Datagrid has serial number(1,2,3,4...) //If the user clicks "-" it removes row at that position dt.Rows.RemoveAt(Convert.ToInt32(e.Item.Cells[0].Text)-1); } bindData(); } } } /* Update the datatable before moving to some other page in Datagrid etc*/ private void updateDataTable() { for(int k=0;k
Hi there, The simple way to persist user-sepcific data is to use the Session object. To work around the issue with few changes, you may define a property named dt in lieu of the static variable dt.
//static DataTable dt;
DataTable dt
{
get
{
return Session["dt"] as DataTable;
}
set
{
Session["dt"] = value;
}
}