Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Help me in Datagrid with Static Datatable object

Help me in Datagrid with Static Datatable object

Scheduled Pinned Locked Moved ASP.NET
helpwpfwcfdesigntesting
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cloudking11966
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • C cloudking11966

      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

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      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;
      }
      }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups