none
Refreshing BindingSource using Linq To SQL RRS feed

  • Question

  • I couldn't refresh a BindingSource bound to a Linq To Sql table. Here's a test code I've used for a DataContext with a single TmpTable :

    DataClasses1DataContext db = new DataClasses1DataContext();
        
                private void Form1_Load(object sender, EventArgs e)
                {
                    LoadBinding();
                }
        
                private void LoadBinding()
                {
                    tmpTableBindingSource.DataSource = db.TmpTables;   
                }
        
                private void AddNewRecord_Click(object sender, EventArgs e)
                {
                    TmpTable newRecord = new TmpTable()
                    {
                        ad = "asd"
                    };
        
                    db.TmpTables.InsertOnSubmit(newRecord);
                    db.SubmitChanges();
                    LoadBinding();
                }
        
                private void ShowRecordCounts_Click(object sender, EventArgs e)
                {
                    MessageBox.Show("Context Table: " + db.TmpTables.Count().ToString()+" BindingSource: "+ tmpTableBindingSource.Count.ToString());
                }


    Let's say I had 3 records. ShowRecordCounts displays "Context Table: 3 BindingSource: 3 ", which is correct. After insert it displays "Context Table: 4 BindingSource: 3 " which means BindingSource didn't recieve the new record. No matter what I did I couldn't refresh it. I'd appreciate any help with this..

    Tuesday, October 20, 2009 2:39 PM

Answers

  • Hi armagane,

    You can try to refresh the datasource by this code,

    tmpTableBindingSource.DataSource = null;
    tmpTableBindingSource.DataSource = db.TmpTables;

    Does this works for you?

    Best Regards
    Yichun Feng
    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.

    Thanks but unfortunately this doesn't work.

    I have solved the problem but not in a way I wanted.

    Turns out that DataContext and Linq To SQL is best for unit-of-work operations. Means you create a DataContext, get your job done, discard it. If you need another operation, create another one.

    For this problem only thing I had to do was recreate my DataContext like this.dx = new MyDataContext();. If you don't do this you always get stale/cached data. From what I've read from various blog/forum posts that DataContext is lightweight and doing this A-OK. This was the only way I've found after searching for a day.
    • Marked as answer by deastr Friday, October 23, 2009 8:57 AM
    Friday, October 23, 2009 8:57 AM

All replies

  • Hi armagane,

    You can try to refresh the datasource by this code,

    tmpTableBindingSource.DataSource = null;
    tmpTableBindingSource.DataSource = db.TmpTables;

    Does this works for you?

    Best Regards
    Yichun Feng
    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.
    Friday, October 23, 2009 8:29 AM
  • Hi armagane,

    You can try to refresh the datasource by this code,

    tmpTableBindingSource.DataSource = null;
    tmpTableBindingSource.DataSource = db.TmpTables;

    Does this works for you?

    Best Regards
    Yichun Feng
    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.

    Thanks but unfortunately this doesn't work.

    I have solved the problem but not in a way I wanted.

    Turns out that DataContext and Linq To SQL is best for unit-of-work operations. Means you create a DataContext, get your job done, discard it. If you need another operation, create another one.

    For this problem only thing I had to do was recreate my DataContext like this.dx = new MyDataContext();. If you don't do this you always get stale/cached data. From what I've read from various blog/forum posts that DataContext is lightweight and doing this A-OK. This was the only way I've found after searching for a day.
    • Marked as answer by deastr Friday, October 23, 2009 8:57 AM
    Friday, October 23, 2009 8:57 AM
  • After a couple of days of researching I can confirm that the provided solution is the only way I was able to fix this problem.

    Specifically I have a table that is updated by an external program.  I usually declare a DataContext global to the windows form and reuse that datacontext like this (Which does NOT pick up the new values) :

    Public Class Form_IXPSwitchCallStatus
        Dim IXP1DC As New IXPProtocal1DataContext(Common.GetOrSaveSettingConnStr("myDatabase", "myServer"))
        Dim callList1 As IQueryable(Of CallState1)

        '****************************************************************************
        '*  Form_ELMEmailBlocking_Load
        '****************************************************************************
        Private Sub Form_ELMEmailBlocking_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            BindGrid1()
        End Sub

        '****************************************************************************
        '*  BindGrid1
        '****************************************************************************
        Public Sub BindGrid1()
            callList1 = From CS In IXP1DC.CallState1s
            CallState1BindingSource.DataSource = callList1
        End Sub

        '****************************************************************************
        '*  Timer1_Tick
        '****************************************************************************
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            ProgressBarControl1.PerformStep()
            'ProgressBarControl1.Update()
            If ProgressBarControl1.EditValue >= ProgressBarControl1.Properties.Maximum Then
                Timer1.Enabled = False
                BindGrid1()
                ProgressBarControl1.EditValue = ProgressBarControl1.Properties.Minimum
                Timer1.Enabled = True
            End If

        End Sub
    THIS DOES NOT PROPERLY UPDATE THE GRID WITH NEW VALUES!!!!


    So I changed it to:
    Public Class Form_IXPSwitchCallStatus
        Dim callList1 As IQueryable(Of CallState1)

        '****************************************************************************
        '*  Form_ELMEmailBlocking_Load
        '****************************************************************************
        Private Sub Form_ELMEmailBlocking_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            BindGrid1()
        End Sub

        '****************************************************************************
        '*  BindGrid1
        '****************************************************************************
        Public Sub BindGrid1()
            Dim IXP1DC As New IXPProtocal1DataContext(Common.GetOrSaveSettingConnStr("myDatabase", "myServer"))
            callList1 = From CS In IXP1DC.CallState1s
            CallState1BindingSource.DataSource = callList1
            IXP1DC.Dispose()
        End Sub

        '****************************************************************************
        '*  Timer1_Tick
        '****************************************************************************
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            ProgressBarControl1.PerformStep()
            'ProgressBarControl1.Update()
            If ProgressBarControl1.EditValue >= ProgressBarControl1.Properties.Maximum Then
                Timer1.Enabled = False
                BindGrid1()
                ProgressBarControl1.EditValue = ProgressBarControl1.Properties.Minimum
                Timer1.Enabled = True
            End If

        End Sub
    Which does display the externally updated data properly!!!!!


    I am a little dismayed with having to attack the problem this way BUT now that I know and understand this approach it IS livable.  I tried refreshing different components in all different manner... specifically:
             IXP1DC.Refresh(Data.Linq.RefreshMode.OverwriteCurrentValues, callList1)
    but this did NOT pickup the new values!

    Mike
    Monday, December 21, 2009 8:54 PM