Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Binding Not Writing

Status
Not open for further replies.

fawkes

Technical User
Sep 12, 2003
343
0
0
GB
I have a customised tab control that creates tab pages and controls at run time based upon the data in a xml file.

Within the application I fill a data set, add an instance of the customised tab control to the visible form and call the method BuildTabPages shown below.

The dataset already has data included and the data is presented in detail format (not in a datagrid).

Code:
Public Sub BuildTabPages(ByRef bindingDataSet As DataSet)

...

CType(newControl, TextBox).DataBindings.Add( _
                                    "Text", bindingDataSet.Tables(0), itemRow.Item("Field"))

The xml file is read into a dataset and the itemRow object is the current row of the relevant datatable of the xml based dataset.

When the tab control shows, if the dataset.table(0) has data the first record is shown in the controls correctly.

If I add a row I can move to the row and show the data for that row.

If I change the data in any of the text boxes and call an update of the data set, collecting a new datatable using the dataset.tables(0).getchanges method passing the modified state as the parameter it returns nothing for the new table so no data is updated.

Why is this?
 
If I change the data in any of the text boxes and call an update of the data set, collecting a new datatable using the dataset.tables(0).getchanges method passing the modified state as the parameter it returns nothing for the new table so no data is updated.

I would like to see the code you used for this.


Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I have a class that I populate

Code:
Private Class WorksOrderUpdate

        Public InsertCommand As New OdbcCommand
        Public UpdateCommand As New OdbcCommand
        Public DeleteCommand As New OdbcCommand

        Public InsertTable As New DataTable
        Public UpdateTable As New DataTable
        Public DeleteTable As New DataTable

        Public Connection As New OdbcConnection

        Public Sub New(ByVal dataConnection As OdbcConnection)
            'Set the connection
            Me.Connection = dataConnection
            'Set the command connections
            Me.InsertCommand.Connection = Me.Connection
            Me.UpdateCommand.Connection = Me.Connection
            Me.DeleteCommand.Connection = Me.Connection
        End Sub

        Public Sub InsertData()

            'Provide a dataadapter to insert the data
            Dim dataAdapter As New OdbcDataAdapter("", Me.Connection)

            'Set the insert command of the data adapter
            dataAdapter.InsertCommand = Me.InsertCommand

            'Insert the data
            Try
                If Not Me.InsertTable Is Nothing Then _
                            dataAdapter.Update(Me.InsertTable)
            Catch ex As SystemException
                Throw ex
            End Try

        End Sub 'InsertData

        Public Sub UpdateData()

            'Provide a dataadapter to update the data
            Dim dataAdapter As New OdbcDataAdapter("", Me.Connection)

            'Set the update command of the data adapter
            dataAdapter.UpdateCommand = Me.UpdateCommand

            'Update the data
            Try
                If Not Me.UpdateTable Is Nothing Then _
                            dataAdapter.Update(Me.UpdateTable)
            Catch ex As SystemException
                Throw ex
            End Try

        End Sub 'UpdateDate

        Public Sub DeleteData()

            'Provide a dataadapter to delete the data
            Dim dataAdapter As New OdbcDataAdapter("", Me.Connection)

            'Set the delete command of the data adapter
            dataAdapter.DeleteCommand = Me.DeleteCommand

            'Delete the data
            Try
                If Not Me.DeleteTable Is Nothing Then _
                                    dataAdapter.Update(Me.DeleteTable)
            Catch ex As SystemException
                Throw ex
            End Try

        End Sub 'DeleteData

    End Class   'WorksOrderUpdate

Within another method I have an instance of this class and set the following code

Code:
'Set the tables of the works order update object
            WOUpdate.InsertTable = dataTable.GetChanges(DataRowState.Added)
            WOUpdate.UpdateTable = dataTable.GetChanges(DataRowState.Modified)
            WOUpdate.DeleteTable = dataTable.GetChanges(DataRowState.Deleted)

Then I call the InsertData, UpdateData and DeleteData methods.

The reason I do it this way is that I have text files identifying the tables to update and the columns that hold unique index and other keys to uniquielyt identify the correct row and row status.

I don't think it is a parameters probelm as the code
Code:
 WOUpdate.UpdateTable = dataTable.GetChanges(DataRowState.Modified)
returns nothing.
 
How are you modifying the data in dataTable? How does that data get into dataTable inthe first place?



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
The data table is populated using a select command a data adapter. I hoped that the data was modified by changing the text of the data bound text boxes (see first entry) but it doesn't seem to register as modified.
 
Declare CommandBuilder for Update and Acceptchanges

Private bob As OdbcCommandBuilder = New OdbcCommandBuilder(DataAdapter)



Eric De Decker
Visit The Belgium "Visual Basic Group" at
 
Not sure about this, but don't you want to bind to a column? In the standard examples I have seen, the 3rd argument is a column. The system will automatically grab the correct row becuase behind the scenes I think it is a DataViewRow object. Good Luck!

Have a great day!

j2consulting@yahoo.com
 
Here is the code for setting the data binding, it is held in the customised layout tab control so the Me keyword relates to the tab control.

The tab pages and the controls on them are defined from the data held in an xml file.

Code:
'Collect the layouts and build tab pages for each group
    Public Sub BuildTabPages(ByRef bindingDataSet As DataSet)

        'If there aren't any tables in the data set exit the method
        If bindingDataSet.Tables.Count = 0 Then Exit Sub

        'Collect the xml file
        Me.LayoutDataset1.ReadXml(Application.StartupPath & "/Layouts/MyLayout.xml")

        'Loop through the rows, creating new tab pages if required for each group
        'and adding the correct control with the correct text in the correct position
        Dim groupRow As DataRow
        Dim itemRow As DataRow
        For Each groupRow In Me.LayoutDataset1.Tables(1).Rows
            'Create a new group each time
            Dim newTabPage As New TabPage(groupRow.Item("Name"))
            For Each itemRow In groupRow.GetChildRows(Me.LayoutDataset1.Relations(0))
                'Create a new control
                Dim newControl As Control
                'Check the type of object
                Select Case itemRow.Item("Type")
                    Case Is = "Label"
                        'Create a new label object
                        newControl = New Label
                        'Set the text of the label
                        newControl.Text = itemRow.Item("Description")
                    Case Is = "TextBox"
                        'Create a new text box object
                        newControl = New TextBox
                        'Set the name of the textbox
                        newControl.Name = itemRow.Item("Description")
                        'Set the control readonly if required
                        CType(newControl, TextBox).ReadOnly = itemRow.Item("ReadOnly")
                        'Set the data binding
                        CType(newControl, TextBox).DataBindings.Add( _
                                    "Text", bindingDataSet.Tables(0), itemRow.Item("Field"))
                End Select

                'Set the position of the control
                With newControl
                    .Top = itemRow.Item("Top")
                    .Left = itemRow.Item("Left")
                    .Width = itemRow.Item("Width")
                    .Height = itemRow.Item("Height")
                End With

                'Add the new control to the tab page
                newTabPage.Controls.Add(newControl)
            Next

            'Add the new tab page to the tab control
            Me.TabPages.Add(newTabPage)
        Next

    End Sub

The current application has a dataset with only one table in it.

I don't know anything about command builders but I will look it that.

 
I've looked into command builders and they wouldn't affect the issue. The issue is that I can't obtain a table of modifications using the GetChanges method. I have created an update command and added it to the data adapter so command builders are not necessary.
 
Just thinking out loud, is it possible that your binding
Code:
'Set the data binding
CType(newControl, TextBox).DataBindings.Add( _
   "Text", bindingDataSet.Tables(0), itemRow.Item("Field"))
needs to happen after the control is added to the tab page? Good Luck!

Have a great day!

j2consulting@yahoo.com
 
I moved the code
Code:
'Add the new tab page to the tab control
            Me.TabPages.Add(newTabPage)

to the line above

Code:
'Set the data binding
CType(newControl, TextBox).DataBindings.Add( _
   "Text", bindingDataSet.Tables(0), itemRow.Item("Field"))

but it didn't make any difference, the GetChanges method still returns nothing for modified rows.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top