Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Databound controls not updating when datasource updates
Message
From
23/03/2006 12:24:58
 
 
To
23/03/2006 09:58:47
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
General information
Forum:
ASP.NET
Category:
ADO.NET
Environment versions
Environment:
VB.NET 1.1
Database:
MS SQL Server
Miscellaneous
Thread ID:
01106656
Message ID:
01107041
Views:
19
This message has been marked as the solution to the initial question of the thread.
Hey Mike,

The problem is that you're recreating the DataSet every time (a new instantiation) ... that, in effect, looses all your previous databindings, hence the reason you need to unbind and rebind.

The way to solve the problem is to have your DataSet as a member of the Form and simply clear it prior to re-filling it. Something like this (I only included the things I changed):
   Private DataSet dsRetiree
.
.
.
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshData()
        RetireeView.Table = Me.dsRetiree.Tables("Retiree")
        Call BindFields()
    End Sub

    Private Sub RefreshData()
        gfncRetiree_Load()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call RefreshData()
    End Sub

    ' Change so it doesn't return the DataSet
    Public Sub gfncRetiree_Load()
        ' Get rid of this statement: Dim Retiree As New DataSet
        Dim myDataAdapter As New SqlDataAdapter
        myDataAdapter.SelectCommand = New SqlCommand
        myDataAdapter.SelectCommand.Connection = Conn1
        myDataAdapter.SelectCommand.CommandText = "SELECT * FROM Retiree ORDER BY Counter"
        myDataAdapter.SelectCommand.CommandType = CommandType.Text

        Try
            Conn1.Open()
            Me.dsRetiree.Clear()
            myDataAdapter.Fill(Me.dsRetiree, "Retiree")
        Catch
            MsgBox(Err.Number & " : " & Err.Description)
        Finally
            Conn1.Close()
        End Try
        myDataAdapter.Dispose()
    End Sub
~~Bonnie


>Hey Bonnie,
>I simplified my problem down as must as possible (I think):
>
>
>   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
>        RefreshData()
>        RetireeView.Table = gRetireeDS.Tables("Retiree")
>        Call BindFields()
>    End Sub
>
>    Private Sub RefreshData()
>        gRetireeDS = gfncRetiree_Load()
>    End Sub
>
>    Private Sub BindFields()
>        txtFirstName.DataBindings.Add(New Binding("text", RetireeView, "FirstName"))
>        txtLastName.DataBindings.Add(New Binding("text", RetireeView, "LastName"))
>        txtLastUpdated.DataBindings.Add(New Binding("text", RetireeView, "LastUpdated"))
>    End Sub
>
>    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
>        Call RefreshData()
>    End Sub
>
>    Public Function gfncRetiree_Load() As DataSet
>        Dim Retiree As New DataSet
>        Dim myDataAdapter As New SqlDataAdapter
>        myDataAdapter.SelectCommand = New SqlCommand
>        myDataAdapter.SelectCommand.Connection = Conn1
>        myDataAdapter.SelectCommand.CommandText = "SELECT * FROM Retiree ORDER BY Counter"
>        myDataAdapter.SelectCommand.CommandType = CommandType.Text
>
>        Try
>            Conn1.Open()
>            Retiree.Clear()
>            myDataAdapter.Fill(Retiree, "Retiree")
>        Catch
>            MsgBox(Err.Number & " : " & Err.Description)
>        Finally
>            Conn1.Close()
>        End Try
>        gfncRetiree_Load = Retiree
>        myDataAdapter.Dispose()
>        Retiree.Dispose()
>    End Function 'gfncRetiree_Load
>
>
>Now, to actually make this work, I change BindFields() to clear the databindings, set RetireeView.Table = gRetireeDS.Tables("Retiree"), and rebind the fields. I then have to call BindFields() in RefreshData(). That seems like a lot of hoops to jump through.
>
>Thanks for any guidance! I finally have some real time to devote to a project instead of finding the quickest way to get something done. I would really like to start learning some best practices.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform