Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / June 2005

Tip: Looking for answers? Try searching our database.

Programmatically changes in bound controls not updated in bound fi

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adrian - 17 Jun 2005 17:59 GMT
I found out a strange issue in VB.NET 2003 and I am wondering if someone else
had the same problem.
Everything is done programmatically at run-time.
I fill a tableset within a dataset with data from any database (for
simplifying I get only one record).
I bound a textbox to one of the record's fields.
I add a button and on its click event I change the content of the text box.
At this moment, if I am looking at control's Text property, I will see the
new value but if I am looking into the tableset for the bound field I will se
the old value.
If the change in the text-box is performed manually, right after leaving the
text-box the tableset's bound field is updated.
In other words it sounds to me like anytime you programmatically change the
TEXT property of a bound control, the change is not also performed in the
bound field too.
To me this thing sounds like a bug that will affect whoever is trying to
work bound and change data programmatically because all these changes will be
lost.
I would appreciate if someone would have an input regarding this issue.

I am giving the code that should be inserted in the Form1 of a new project.
If you have a SQL Server (or MSDE) with Northwind database on the local
machine, code will work as it is, otherwise the connection string should be
changed
Thanks in advance,

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

  Public Sub New()
     MyBase.New()

     'This call is required by the Windows Form Designer.
     InitializeComponent()

     'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
     If disposing Then
        If Not (components Is Nothing) Then
           components.Dispose()
        End If
     End If
     MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents cmdChange As System.Windows.Forms.Button
  Friend WithEvents Customer As System.Windows.Forms.TextBox
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents City As System.Windows.Forms.TextBox
  Friend WithEvents State As System.Windows.Forms.TextBox
  <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
     Me.cmdChange = New System.Windows.Forms.Button
     Me.Label2 = New System.Windows.Forms.Label
     Me.Customer = New System.Windows.Forms.TextBox
     Me.Label1 = New System.Windows.Forms.Label
     Me.City = New System.Windows.Forms.TextBox
     Me.State = New System.Windows.Forms.TextBox
     Me.SuspendLayout()
     '
     'cmdChange
     '
     Me.cmdChange.Location = New System.Drawing.Point(192, 60)
     Me.cmdChange.Name = "cmdChange"
     Me.cmdChange.Size = New System.Drawing.Size(100, 23)
     Me.cmdChange.TabIndex = 5
     Me.cmdChange.Text = "Change Address"
     '
     'Label2
     '
     Me.Label2.Location = New System.Drawing.Point(4, 32)
     Me.Label2.Name = "Label2"
     Me.Label2.Size = New System.Drawing.Size(72, 16)
     Me.Label2.TabIndex = 2
     Me.Label2.Text = "State/City:"
     '
     'Customer
     '
     Me.Customer.Location = New System.Drawing.Point(76, 4)
     Me.Customer.Name = "Customer"
     Me.Customer.Size = New System.Drawing.Size(220, 20)
     Me.Customer.TabIndex = 1
     Me.Customer.Text = ""
     '
     'Label1
     '
     Me.Label1.Location = New System.Drawing.Point(8, 8)
     Me.Label1.Name = "Label1"
     Me.Label1.Size = New System.Drawing.Size(72, 16)
     Me.Label1.TabIndex = 0
     Me.Label1.Text = "Customer:"
     '
     'City
     '
     Me.City.Location = New System.Drawing.Point(120, 28)
     Me.City.Name = "City"
     Me.City.Size = New System.Drawing.Size(176, 20)
     Me.City.TabIndex = 4
     Me.City.Text = ""
     '
     'State
     '
     Me.State.Location = New System.Drawing.Point(76, 28)
     Me.State.Name = "State"
     Me.State.Size = New System.Drawing.Size(40, 20)
     Me.State.TabIndex = 3
     Me.State.Text = ""
     '
     'Form1
     '
     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
     Me.ClientSize = New System.Drawing.Size(304, 94)
     Me.Controls.Add(Me.State)
     Me.Controls.Add(Me.City)
     Me.Controls.Add(Me.Customer)
     Me.Controls.Add(Me.Label1)
     Me.Controls.Add(Me.cmdChange)
     Me.Controls.Add(Me.Label2)
     Me.Name = "Form1"
     Me.Text = "Form1"
     Me.ResumeLayout(False)

  End Sub

#End Region
  Dim ds As New DataSet
  Dim da As SqlDataAdapter
  Dim g_Conn As New SqlConnection

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
     Try
        g_Conn.ConnectionString = "Server=(local); Integrated
Security=SSPI; Database=Northwind; Connection Timeout=5"
        g_Conn.Open()
        Dim sSQL As String = "SELECT TOP 1 CompanyName, Region, City FROM
Customers (NOLOCK) WHERE Country = 'USA'"
        da = New SqlDataAdapter(sSQL, g_Conn)
        da.Fill(ds, "Customer")
        Call BindingData()
     Catch ex As Exception
        MsgBox("Loading Error: " + ex.Message)
     End Try
  End Sub
  Private Sub BindingData()
     Try
        If Customer.DataBindings.Count > 0 Then
           Customer.DataBindings.Clear()
           City.DataBindings.Clear()
           State.DataBindings.Clear()
        End If
        Customer.DataBindings.Add("Text", ds, "Customer.CompanyName")
        City.DataBindings.Add("Text", ds, "Customer.City")
        State.DataBindings.Add("Text", ds, "Customer.Region")
     Catch ex As Exception
        MsgBox("BindingData Error: " + ex.Message)
     End Try

  End Sub

  Private Sub cmdChange_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdChange.Click
     Try
        Debug.WriteLine(ds.Tables("Customer").Rows(0).Item("City") + ", " +
ds.Tables("Customer").Rows(0).Item("Region"))
        City.Text = "San Jose"
        State.Text = "CA"
        Debug.WriteLine(ds.Tables("Customer").Rows(0).Item("City") + ", " +
ds.Tables("Customer").Rows(0).Item("Region"))
     Catch ex As Exception
        MsgBox("Changing Error: " + ex.Message)
     End Try
  End Sub
End Class
Kevin Yu [MSFT] - 18 Jun 2005 03:37 GMT
Hi

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
Signature

=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

"Jeffrey Tan[MSFT]" - 20 Jun 2005 06:31 GMT
Hi,

Based on my understanding, you programmatically change the content in the
TextBox, which is bound to DataSet, then you want to update the underlying
datasource with the UI TextBox content.

This is frequently asked question, normally, we can force the modified
TextBox content to update back to the underlying datasource using TextBox
bindingmanager's EndCurrentEdit, for more information, please refer to:
"4.22 I programatically change a bound TextBox value, but the value does
not get pushed back into the bound datasource. How can I make sure the
DataSource is updated?"
http://64.78.52.104/FAQ/WinForms/FAQ_c43c.asp#q1017q
===============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

"Jeffrey Tan[MSFT]" - 22 Jun 2005 07:52 GMT
Hi,

Does my reply make sense to you? Is your problem resolved? If you still
have any concern, please feel free to tell me. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.