none
BindingSource has not update. RRS feed

  • Question

  • I have a Department table in database. I can insert a record through this static method and work fine:
        public class DepartmentDAO : BaseDAO<Department>
        {
            public static Department Insert(String deptName,
                Staff contactPerson, String location)
            {
                Department newDept = new Department();

                newDept.id = Guid.NewGuid();
                newDept.name = deptName;
                newDept.contactPerson = contactPerson;
                newDept.location = location;
                GlobalDataContext.Departments.InsertOnSubmit(newDept);

                return newDept;
            }
        }

    The problem is, when I added a record to GlobalDataContext.Departments, the BindingSource which bind to it has no changes, like this:
            public void BindingEntitySetUpdatable()
            {
                // Insert a record to GlobalDataContext.Department first time.
                DepartmentDAO.Insert("_test_DeptName", null, "_test_Location");
                GlobalDataContext.SubmitChanges();       // Database has been update, I can see it on other database tool.

                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = GlobalDataContext.Departments;
                Assert.AreEqual(bindingSource.Count, GlobalDataContext.Departments.Count());  // Work fine~~

                // Insert another record to GlobalDataContext.Department again.
                DepartmentDAO.Insert("_test_DeptName2", null, "_test_Location2");
                GlobalDataContext.SubmitChanges();       // Work fine also, database has been changed.

                BindingSource bindingSource2 = new BindingSource();
                bindingSource2.DataSource = GlobalDataContext.Departments;
                Assert.AreEqual(bindingSource2.Count, GlobalDataContext.Departments.Count());  // Problem here !!!
            }

    So I can't see any changes from the control (such as a ComboBox bind to it) to reflect the updated Department table properly. The only way I can update the bindSource is Re-Start the application.......

    What's wrong of my code?

    Thank you very much.

    Best regards,
    Diviner.

    Wednesday, July 2, 2008 3:02 AM

Answers

  • Thank you. The article is too hard for me. But I still can get some point from that. So I do a small changes on the previous code and work well now:

    I have a Department table in database. I can insert a record through this static method and work fine:
        public class DepartmentDAO : BaseDAO<Department>
        {
            public static Department Insert(String deptName,
                Staff contactPerson, String location)
            {
                Department newDept = new Department();

                newDept.id = Guid.NewGuid();
                newDept.name = deptName;
                newDept.contactPerson = contactPerson;
                newDept.location = location;
                GlobalDataContext.Departments.InsertOnSubmit(newDept);

                return newDept;
            }
        }

    The problem is, when I added a record to GlobalDataContext.Departments, the BindingSource which bind to it has no changes, like this:
            public void BindingEntitySetUpdatable()
            {
                // Insert a record to GlobalDataContext.Department first time.
                DepartmentDAO.Insert("_test_DeptName", null, "_test_Location");
                GlobalDataContext.SubmitChanges();       // Database has been update,
                                                                             //  I can see it on other database tool.
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = GlobalDataContext.Departments;
                Assert.AreEqual(bindingSource.Count, GlobalDataContext.Departments.Count());  // Work fine~~

                // Insert another record to GlobalDataContext.Department again.
                DepartmentDAO.Insert("_test_DeptName2", null, "_test_Location2");
                GlobalDataContext.SubmitChanges();       // Work fine also, database has been changed.

                BindingSource bindingSource2 = new BindingSource();
                IQueryable<Department> depts = from d in GlobalDataContext.Departments
                                                                 select d;
                bindingSource2.DataSource = depts;
                Assert.AreEqual(bindingSource2.Count, GlobalDataContext.Departments.Count());  // Problem Fixed   ^_^
            }


    --
    Diviner.
    Thursday, July 3, 2008 2:04 AM

All replies

  • I suspect you are holding onto your context too long and thus letting the data get stale. After you are done with a unit of work (typicaly after a submit changes), consider refreshing it or disposing it and creating a new one. For more information, you may want to read Dinesh Kulkarni's recent blog post at http://blogs.msdn.com/dinesh.kulkarni/archive/2008/07/01/linq-to-sql-tips-9-understanding-datacontext-s-internal-caching.aspx

     

    Jim Wooley

    www.ThinqLinq.com

    Wednesday, July 2, 2008 2:27 PM
    Moderator
  • Thank you. The article is too hard for me. But I still can get some point from that. So I do a small changes on the previous code and work well now:

    I have a Department table in database. I can insert a record through this static method and work fine:
        public class DepartmentDAO : BaseDAO<Department>
        {
            public static Department Insert(String deptName,
                Staff contactPerson, String location)
            {
                Department newDept = new Department();

                newDept.id = Guid.NewGuid();
                newDept.name = deptName;
                newDept.contactPerson = contactPerson;
                newDept.location = location;
                GlobalDataContext.Departments.InsertOnSubmit(newDept);

                return newDept;
            }
        }

    The problem is, when I added a record to GlobalDataContext.Departments, the BindingSource which bind to it has no changes, like this:
            public void BindingEntitySetUpdatable()
            {
                // Insert a record to GlobalDataContext.Department first time.
                DepartmentDAO.Insert("_test_DeptName", null, "_test_Location");
                GlobalDataContext.SubmitChanges();       // Database has been update,
                                                                             //  I can see it on other database tool.
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = GlobalDataContext.Departments;
                Assert.AreEqual(bindingSource.Count, GlobalDataContext.Departments.Count());  // Work fine~~

                // Insert another record to GlobalDataContext.Department again.
                DepartmentDAO.Insert("_test_DeptName2", null, "_test_Location2");
                GlobalDataContext.SubmitChanges();       // Work fine also, database has been changed.

                BindingSource bindingSource2 = new BindingSource();
                IQueryable<Department> depts = from d in GlobalDataContext.Departments
                                                                 select d;
                bindingSource2.DataSource = depts;
                Assert.AreEqual(bindingSource2.Count, GlobalDataContext.Departments.Count());  // Problem Fixed   ^_^
            }


    --
    Diviner.
    Thursday, July 3, 2008 2:04 AM