none
Dataset / Datatable question - how do I ensure I see updates to my DataTable in real time across parts of my code? RRS feed

  • Question

  • Hi,

    I'm setting up a Dataset and DataTable in one section of my code. I'm having trouble understanding why update data in one area of my code why I don't see this in another area. I'm doing my best to use the same DataTable as far as I can tell (see code below).

    What is the minimum I have to do if I want to see new rows that I add in one section of my code in a DataTable appear in the DataTable in another section of my code?

     

        public partial class ManageLinksForm : Form
        {
            List<ParentUrlDto> _parents = new List<ParentUrlDto>();
            MyDataSet myDataSet = MyDataSet.GetInstance();
    
            public ManageLinksForm()
            {
                InitializeComponent();
            }
    
            private void ManageLinksForm_Load(object sender, EventArgs e)
            {
                bindingSource2.DataSource = myDataSet.DT_Webfiles;
            }
    
            private void addParent_Button_Click(object sender, EventArgs e)
            {
                myDataSet.AddParent(newUrl_TextBox.Text);      
    	     // THIS IS WHERE I ADD A NEW ITEM
            }
        }
    
    
        public class MyDataSet
        {
            public static MyDataSet myDataSet = null;
            private DataSet myData = new DataSet();
            public DataTable DT_Webfiles;
    
            public static MyDataSet GetInstance()
            {
                if (myDataSet == null)
                {
                    return new MyDataSet();
                }
                else
                {
                    return myDataSet;
                }
            }
    
            private MyDataSet()
            {
    	     // all setup & fill code
                this.DT_Webfiles = this.myData.Tables["WEBFILES"];
            }
    
            public void AddParent(string url)
            {
                DT_Webfiles.Rows.Add(<<new row code>>);
                DA_Webfiles.Update(this.myData, "WEBFILES");
            }
    
            public List<Uri> ParentUris()
            {
                var list = new List<Uri>();
                foreach (DataRow dr in DT_Webfiles.Rows)
                {
                    var i = new Item(dr);
                    if (i.isParent)
                    {
                        var uri = new Uri(i.Path);
                        list.Add(uri);
                    }
                }
                return list;
            }
        }
    
    
        public class Coordinator
        {
            internal MyDataSet _myConfig;
    
            public Coordinator()
            {
                if (_myConfig == null)
                {
                    _myConfig = MyDataSet.GetInstance();
                }
            }
    
    	 public void Process()
            {
                // -- RESET STATUS --
                _myConfig.ResetStatus();
    
                // --- EACH PARENT ---
                List<Uri> parentUris = _myConfig.ParentUris();     
                //THIS IS WHERE I DON'T SEE TO SEE THE NEW ITEMS, HOWEVER AFTER RESTARTING THE APP i DO
    	 }
        }

    Tuesday, January 12, 2010 6:32 AM

Answers

  • someone pointed out to me I'd forgot to assign the newly created instance to myDataset

    if(myDataSet == null)
    {
        myDataSet = new MyDataSet();
    }
    return myDataSet
    • Marked as answer by callagga Wednesday, January 13, 2010 5:30 AM
    Wednesday, January 13, 2010 5:30 AM

All replies

  • PS.  Just wanted to highlight again that, whilst I don't see the realtime updates in one area of my code, if I restart the winforms application then I do see it.  So it's as if when I restart it the DataSet/DataTables pickup the updates ok from the database.  So its just the realtime aspect I'm stuck on...
    Tuesday, January 12, 2010 6:34 AM
  • Hello callagga,

     

    Only from the sample codes in your first post, I cannot find any obvious problems.  Could you please provide us with more detailed explanation of your code logic?   If it is convenient for you, would you mind sending me a demo project and db file to repro the issue?   My mail address is v-micsun @ microsoft.com.  (remove the spaces besides @)   Thank you very much!

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, January 12, 2010 9:31 AM
    Moderator
  • It seems that you are adding the row to one instance of the dataset (the one owned by the form) and processing from another instance of the dataset (the one
    owned by the Cooordinator).
    In this setup the Coordinator dataset will only be populated once (when the class is instanciated when the application loads) and since there are two instances
    of the dataset the Coordinator will not be updated.

    This could probably be fixed by changing the GetInstance method to actually assign a dataset to the local variable.
    As it is now, every call to the GetInstance method will return a new dataset. In other words:


            public static MyDataSet GetInstance()
            {
                if (myds == null)
                {
                    myds = new MyDataSet();
                    return myds;
                }
    HTH
    //Michael

    This posting is provided "AS IS" with no warranties.
    Tuesday, January 12, 2010 9:38 AM
  • thanks Michael - but isn't this what I've already done in the "public static MyDataSet GetInstance()" method?

    Also another more specific question I could ask is,

    a) if I have two DataTables from the same DataSet, then does updating DataTable 1 imply that DataTable 2 will be updated in real time with the change?

    b) If not, then I assume that you need the same actual instance of a DataTable then to ensure two different points of the code will see changes in real time?

    thanks
    Wednesday, January 13, 2010 2:09 AM
  • just stepping through the code I noted that:

    - the MainForm creates an instance of Coordinator, which itself then calls the "MyDataSet.GetInstance()"

    - but then the ManageLinksForm form also calls "MyDataSet.GetInstance()", BUT when this method is called I note that at the point it checks to see whether the MyDataSet static variable "myDataSet" is null is it TRUE, whereas I expect at this point it shouldn't be...  That is:

        public class MyDataSet
        {
            public static MyDataSet myDataSet;
            private DataSet myData = new DataSet();
    
            public static MyDataSet GetInstance()
            {
                if (myDataSet == null)
                {
                    return new MyDataSet();
                }
                else
                {
                    return myDataSet;
                }
            }
    
    
    So it almost seems the static "myDataSet" variable isn't working in terms of only having once instance?


    Wednesday, January 13, 2010 2:33 AM
  • someone pointed out to me I'd forgot to assign the newly created instance to myDataset

    if(myDataSet == null)
    {
        myDataSet = new MyDataSet();
    }
    return myDataSet
    • Marked as answer by callagga Wednesday, January 13, 2010 5:30 AM
    Wednesday, January 13, 2010 5:30 AM
  • Hello callagga,

     

    I am glad to hear that the problem has solved. 

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 13, 2010 5:59 AM
    Moderator
  • Hello callagga,

     

    I am glad to hear that the problem is solved.  Thanks for sharing the solution here!

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 13, 2010 6:00 AM
    Moderator