Persist DataTable data from Dataset.xsd

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 8
Reputation: eddy_boy is an unknown quantity at this point 
Solved Threads: 0
eddy_boy eddy_boy is offline Offline
Newbie Poster

Persist DataTable data from Dataset.xsd

 
0
  #1
Oct 27th, 2009
Hi,

Any advice please.

I have various forms (e.g. frmCustomers, frmProducts), each having bindingSources and tableAdapters. The forms have the required Fill methods triggered from their constructors. It appears that the DataTable filled only exists in the App for as long as the form does.

What I want is a method to have the DataTable that has been filles, stay in scope within the app so I can use it again without having to call Fill again.

Anybody have a good way of doing this? Maybe copy the DataTables to local variables in the Parent form?

Cheers.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 254
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training
 
1
  #2
Oct 27th, 2009
Hello.
If I understood you correctly - this may be helpful for you: Implementing Singleton in C#
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: eddy_boy is an unknown quantity at this point 
Solved Threads: 0
eddy_boy eddy_boy is offline Offline
Newbie Poster
 
0
  #3
Oct 27th, 2009
Originally Posted by Antenka View Post
Hello.
If I understood you correctly - this may be helpful for you: Implementing Singleton in C#
Hi, Antenka.

No, that isn't what I meant. I am after a good way / technique to persist the datatable's data populated by tabeadapters on particular forms. If form closes the app can't use that datatable/data anymore. I am making copies into a static class for now but wanted to know if there are any other ways.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 254
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training
 
0
  #4
Oct 27th, 2009
Well, the other way could be storing datatable/data in your main form. And pass them to other forms of your application at creation time.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,402
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 613
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast
 
1
  #5
Oct 27th, 2009
You can save the dataset schema (structure) along with table data. This uses a dynamically created DataSet/DataTable but it will work for typed datasets as well.

Creating, Populating, and persisting the dataset:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. DataSet ds = new DataSet();
  4. ds.EnforceConstraints = true; //enforce locally since there is no remote db server
  5.  
  6. #region setup beer table in a limited scope
  7. {
  8. DataTable dtBeer = new DataTable();
  9. dtBeer.TableName = "Beer"; //Very important to set the table name when serializing datatables
  10. dtBeer.Columns.Add(new DataColumn("Brand", typeof(string)));
  11. dtBeer.Columns.Add(new DataColumn("SOH", typeof(int))); //"SOH" - Stock on hand
  12. dtBeer.Columns.Add(new DataColumn("OrderQty", typeof(int))); //amt to pick up next time you're at the store. This could make your list :)
  13. dtBeer.Columns["Brand"].Unique = true;
  14.  
  15. DataRow row = dtBeer.NewRow();
  16. row["Brand"] = "Bud Light";
  17. row["SOH"] = 5;
  18. row["OrderQty"] = 0;
  19. dtBeer.Rows.Add(row);
  20.  
  21. row = dtBeer.NewRow();
  22. //This stuff is $8 for 4 bottles at my local store. I'm sure you can get it much
  23. //cheaper as it is a belgian beer, a lot closer to you!
  24. row["Brand"] = "Allagash White";
  25. row["SOH"] = 8;
  26. row["OrderQty"] = int.MaxValue;
  27. dtBeer.Rows.Add(row);
  28.  
  29. ds.Tables.Add(dtBeer);
  30. }
  31. #endregion
  32.  
  33. #region setup wine
  34. {
  35. DataTable dtWine = new DataTable();
  36. dtWine.TableName = "Wine";
  37. dtWine.Columns.Add(new DataColumn("Brand", typeof(string)));
  38. dtWine.Columns.Add(new DataColumn("SOH", typeof(int)));
  39. dtWine.Columns.Add(new DataColumn("OrderQty", typeof(int)));
  40. dtWine.Columns.Add(new DataColumn("Location", typeof(string)));
  41. dtWine.Columns["Brand"].Unique = true;
  42.  
  43. DataRow row = dtWine.NewRow();
  44. row["Brand"] = "Wine1"; //i'm a beer guy
  45. row["SOH"] = 5;
  46. row["OrderQty"] = 0;
  47. row["Location"] = "Basement Cellar, Rack #1, Third row from the bottom";
  48. dtWine.Rows.Add(row);
  49.  
  50. row = dtWine.NewRow();
  51. row["Brand"] = "Win2";
  52. row["SOH"] = 8;
  53. row["OrderQty"] = 0;
  54. row["Location"] = "Attic collection, on top of the exhaust fan";
  55. dtWine.Rows.Add(row);
  56.  
  57. ds.Tables.Add(dtWine);
  58. }
  59. #endregion
  60.  
  61. #region save the dataset to disk
  62. ds.WriteXml(@"C:\database.xml", XmlWriteMode.WriteSchema); //You have to write the schema or it will fail when loading
  63. #endregion
  64. }

Loading the dataset from disk:
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. DataSet ds = new DataSet();
  4. ds.ReadXml(@"C:\database.xml", XmlReadMode.Auto);
  5. int beerCount = ds.Tables["Beer"].Rows.Count;
  6. int wineCount = ds.Tables["Wine"].Rows.Count;
  7. System.Diagnostics.Debugger.Break();
  8. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: eddy_boy is an unknown quantity at this point 
Solved Threads: 0
eddy_boy eddy_boy is offline Offline
Newbie Poster
 
0
  #6
Oct 28th, 2009
Hi, Sknake.

I think that would be a bit too much code for what I need. I just need to save a number of datatables (about 6) data in memory when they are created on seperate forms.

I think the way Antenka mentioned, placing references in the Main form is what I have done. Wondering really if there was a way / option for the data once loaded into the DataSet.xsd to persist?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,402
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 613
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast
 
0
  #7
Oct 28th, 2009
That depends.

If the dataset is a typed dataset created on a form with the designer then when the form closes it will call dispose since the designer adds it to the controls/components for the form. If you're creating a dataset in code then as long as you hold on to a reference it won't be garbage collected.

You could also use the same code I posted to persist a byte[] array in memory and deserialize that data on another form.

>>I think that would be a bit too much code for what I need.
You do realize that 99% of that code was for creating the dataset and populating it, right? The code to persist and recreate the dataset is minimal:

Persist
  1. ds.WriteXml(@"C:\database.xml", XmlWriteMode.WriteSchema); //You have to write the schema or it will fail when loading

Load from buffer:
  1. DataSet ds = new DataSet();
  2. ds.ReadXml(@"C:\database.xml", XmlReadMode.Auto);
Last edited by sknake; Oct 28th, 2009 at 6:38 am.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: eddy_boy is an unknown quantity at this point 
Solved Threads: 0
eddy_boy eddy_boy is offline Offline
Newbie Poster
 
0
  #8
Oct 31st, 2009
Hi, Sknake.

Yes, I see what you mean. Not sure if I like the idea of doing all the IO, never seen anybody in my team do that. Thinking RAM should be the way to go.

I guess the key question is, is there a way I can add a binding source and somehow the automatically generated tableadapter to a class, then I can manage the exsitance of this class (to persist the data)?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,402
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 613
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast
 
0
  #9
Oct 31st, 2009
RAM doesn't persist your data. It only keeps it in memory.

You don't need a table adapter with this method. Instead of using a table adapter to populate the dataset you are deserializing it. They are two different approaches.

[edit]
I think you are confusing persisting a dataset (saving it to disk) versus sharing a dataset (between forms). Are you trying to have a single instance of a DataSet on multiple forms with multiple binding sources against the same data?
[/edit]
Last edited by sknake; Oct 31st, 2009 at 12:08 pm.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: eddy_boy is an unknown quantity at this point 
Solved Threads: 0
eddy_boy eddy_boy is offline Offline
Newbie Poster
 
0
  #10
Oct 31st, 2009
Persist in RAM ideally. e.g. I have a form for entering new customers / editing existing customers. I have another form that want to have a small listbox listing just the customer's firstnames and secondnames...Didn't want to have to place another bindingsource and tableadapter on this form. would be nice to have persisted the data (customers table in dataset) in memory. I can achieve this by copy it to a variable in a static class or main MDI parent form. was wondering if there are other ways in visual studios to accomplish this.
Last edited by eddy_boy; Oct 31st, 2009 at 12:18 pm.
Reply With Quote Quick reply to this message  
Reply

Tags
dataset, datatable

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for dataset, datatable
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC