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. General Programming
  3. C#
  4. Persistence

Persistence

Scheduled Pinned Locked Moved C#
questiondatabase
6 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.
  • S Offline
    S Offline
    Sled Dog
    wrote on last edited by
    #1

    Okay, I have a better question. How do you make a dataset persist between page refreshes without executing your query over and over? Thanks. SD

    R 1 Reply Last reply
    0
    • S Sled Dog

      Okay, I have a better question. How do you make a dataset persist between page refreshes without executing your query over and over? Thanks. SD

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Simplest approach I'd have thought was to store the dataset in the Session: private void Page_Load(object sender, System.EventArgs e) { // try to retreive an existing dataset from the Session DataSet dataSet = (DataSet)Session.Contents["myDataset"]; // if there isn't one if (dataSet == null) { // create it dataSet = new DataSet(); // populate the dataset // ... // add it to the session, ready for retrievel next time Session.Contents.Add("myDataset", dataSet); } // do stuff with the dataset here } Rob Philpott

      S 1 Reply Last reply
      0
      • R Rob Philpott

        Simplest approach I'd have thought was to store the dataset in the Session: private void Page_Load(object sender, System.EventArgs e) { // try to retreive an existing dataset from the Session DataSet dataSet = (DataSet)Session.Contents["myDataset"]; // if there isn't one if (dataSet == null) { // create it dataSet = new DataSet(); // populate the dataset // ... // add it to the session, ready for retrievel next time Session.Contents.Add("myDataset", dataSet); } // do stuff with the dataset here } Rob Philpott

        S Offline
        S Offline
        Sled Dog
        wrote on last edited by
        #3

        Rob, thank you for the response. This sounds like exactly what I want. However, I tried this and I keep running into a problem. First, I whipped up a simple dataset in the Global.ASAX file. Then, I debugged it on a page load. When my page loads, the localDS is set to a new DataSet (per the code snippets below); however, line 103 executes and the localDS becomes . So, what do you think is wrong with... ???localDS = (DataSet)Session.Contents["ds"]; Is there something special that is done in the declaration of ds in the GLOBAL file? Thanks. William protected void Session_Start(Object sender, EventArgs e) { DataSet ds = new DataSet(); DataRow dr; ds.Tables.Add("Test"); ds.Tables[0].Columns.Add(); ds.Tables[0].Columns[0].ColumnName = "Name"; ds.Tables[0].Columns.Add(); ds.Tables[0].Columns[1].ColumnName = "ID"; dr = ds.Tables[0].NewRow(); dr[0] = "Test1"; dr[1] = 1; ds.Tables[0].Rows.Add(dr); dr = ds.Tables[0].NewRow(); dr[0] = "Test2"; dr[1] = 2; ds.Tables[0].Rows.Add(dr); ... 100 private void Page_Load(object sender, System.EventArgs e) 101 { 102 DataSet localDS = new DataSet(); 103 localDS = (DataSet)Session.Contents["ds"]; ...

        R 1 Reply Last reply
        0
        • S Sled Dog

          Rob, thank you for the response. This sounds like exactly what I want. However, I tried this and I keep running into a problem. First, I whipped up a simple dataset in the Global.ASAX file. Then, I debugged it on a page load. When my page loads, the localDS is set to a new DataSet (per the code snippets below); however, line 103 executes and the localDS becomes . So, what do you think is wrong with... ???localDS = (DataSet)Session.Contents["ds"]; Is there something special that is done in the declaration of ds in the GLOBAL file? Thanks. William protected void Session_Start(Object sender, EventArgs e) { DataSet ds = new DataSet(); DataRow dr; ds.Tables.Add("Test"); ds.Tables[0].Columns.Add(); ds.Tables[0].Columns[0].ColumnName = "Name"; ds.Tables[0].Columns.Add(); ds.Tables[0].Columns[1].ColumnName = "ID"; dr = ds.Tables[0].NewRow(); dr[0] = "Test1"; dr[1] = 1; ds.Tables[0].Rows.Add(dr); dr = ds.Tables[0].NewRow(); dr[0] = "Test2"; dr[1] = 2; ds.Tables[0].Rows.Add(dr); ... 100 private void Page_Load(object sender, System.EventArgs e) 101 { 102 DataSet localDS = new DataSet(); 103 localDS = (DataSet)Session.Contents["ds"]; ...

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #4

          Your code doesn't show where the newly created Dataset in Session_Start is added to the Contents dictionary (the Session.Contents.Add line in my original reply) - you may have left that bit out the code snippet - how are you doing this? If the dataset isn't being added to the dictionary after its populated, we can't access it later, so that might be the problem. Also, in line 102 it makes no sense to construct a new dataset, because in the next line localDS is either going to end up (hopefully) referencing the pre-created dataset or null if it can't find it, not the new DataSet you create on line 102. Rob Philpott.

          S 1 Reply Last reply
          0
          • R Rob Philpott

            Your code doesn't show where the newly created Dataset in Session_Start is added to the Contents dictionary (the Session.Contents.Add line in my original reply) - you may have left that bit out the code snippet - how are you doing this? If the dataset isn't being added to the dictionary after its populated, we can't access it later, so that might be the problem. Also, in line 102 it makes no sense to construct a new dataset, because in the next line localDS is either going to end up (hopefully) referencing the pre-created dataset or null if it can't find it, not the new DataSet you create on line 102. Rob Philpott.

            S Offline
            S Offline
            Sled Dog
            wrote on last edited by
            #5

            Rob, first, let me really thank you for assisting so much. Problem solved because of your additional questions... Your right, line 102 wasn't useful...I was just grabbing at straws. The answer was, I did not realize that you had to add the dataset to the session dictionary with an "alias" name in order to access it: Session.Contents.Add("Myds", ds); Thank you again...the code below works fine! William private void Page_Load(object sender, System.EventArgs e) { DataSet localDS = (DataSet)Session.Contents["Myds"]; dd1.DataSource = localDS; dd1.DataTextField = "Name"; dd1.DataValueField = "ID"; dd1.DataBind(); ... protected void Session_Start(Object sender, EventArgs e) { DataSet ds = new DataSet(); DataRow dr; ds.Tables.Add("Test"); ds.Tables[0].Columns.Add(); ds.Tables[0].Columns[0].ColumnName = "Name"; ds.Tables[0].Columns.Add(); ds.Tables[0].Columns[1].ColumnName = "ID"; dr = ds.Tables[0].NewRow(); dr[0] = "Test1"; dr[1] = 1; ds.Tables[0].Rows.Add(dr); dr = ds.Tables[0].NewRow(); dr[0] = "Test2"; dr[1] = 2; ds.Tables[0].Rows.Add(dr); Session.Contents.Add("Myds", ds); ...

            R 1 Reply Last reply
            0
            • S Sled Dog

              Rob, first, let me really thank you for assisting so much. Problem solved because of your additional questions... Your right, line 102 wasn't useful...I was just grabbing at straws. The answer was, I did not realize that you had to add the dataset to the session dictionary with an "alias" name in order to access it: Session.Contents.Add("Myds", ds); Thank you again...the code below works fine! William private void Page_Load(object sender, System.EventArgs e) { DataSet localDS = (DataSet)Session.Contents["Myds"]; dd1.DataSource = localDS; dd1.DataTextField = "Name"; dd1.DataValueField = "ID"; dd1.DataBind(); ... protected void Session_Start(Object sender, EventArgs e) { DataSet ds = new DataSet(); DataRow dr; ds.Tables.Add("Test"); ds.Tables[0].Columns.Add(); ds.Tables[0].Columns[0].ColumnName = "Name"; ds.Tables[0].Columns.Add(); ds.Tables[0].Columns[1].ColumnName = "ID"; dr = ds.Tables[0].NewRow(); dr[0] = "Test1"; dr[1] = 1; ds.Tables[0].Rows.Add(dr); dr = ds.Tables[0].NewRow(); dr[0] = "Test2"; dr[1] = 2; ds.Tables[0].Rows.Add(dr); Session.Contents.Add("Myds", ds); ...

              R Offline
              R Offline
              Rob Philpott
              wrote on last edited by
              #6

              No problem - glad to help :) Rob Philpott

              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