944,155 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 4667
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 27th, 2009
0

Persist DataTable data from Dataset.xsd

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eddy_boy is offline Offline
8 posts
since Oct 2009
Oct 27th, 2009
1
Re: Persist DataTable data from Dataset.xsd
Hello.
If I understood you correctly - this may be helpful for you: Implementing Singleton in C#
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Oct 27th, 2009
0
Re: Persist DataTable data from Dataset.xsd
Click to Expand / Collapse  Quote originally posted by Antenka ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eddy_boy is offline Offline
8 posts
since Oct 2009
Oct 27th, 2009
0
Re: Persist DataTable data from Dataset.xsd
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.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Oct 27th, 2009
1
Re: Persist DataTable data from Dataset.xsd
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:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 28th, 2009
0
Re: Persist DataTable data from Dataset.xsd
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eddy_boy is offline Offline
8 posts
since Oct 2009
Oct 28th, 2009
0
Re: Persist DataTable data from Dataset.xsd
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
C# Syntax (Toggle Plain Text)
  1. ds.WriteXml(@"C:\database.xml", XmlWriteMode.WriteSchema); //You have to write the schema or it will fail when loading

Load from buffer:
C# Syntax (Toggle Plain Text)
  1. DataSet ds = new DataSet();
  2. ds.ReadXml(@"C:\database.xml", XmlReadMode.Auto);
Last edited by sknake; Oct 28th, 2009 at 6:38 am.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 31st, 2009
0
Re: Persist DataTable data from Dataset.xsd
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)?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eddy_boy is offline Offline
8 posts
since Oct 2009
Oct 31st, 2009
0
Re: Persist DataTable data from Dataset.xsd
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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 31st, 2009
0
Re: Persist DataTable data from Dataset.xsd
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eddy_boy is offline Offline
8 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: thread.sleep serial port handling
Next Thread in C# Forum Timeline: Help with picking the right audio/video libraries





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC