none
edit values in data table RRS feed

  • Question

  • 'to fill the data table
    
            m_ColourDataAdapter = New OleDb.OleDbDataAdapter("Select CID,ID,Combination_Name from Colour", oledbcon)
            m_ColourDataAdapter.Fill(m_ColourDataTable)
            m_ColourCommandbuilder = New OleDb.OleDbCommandBuilder(m_ColourDataAdapter)
    
    
    'to edit the data
    
                While ColourCount < m_ColourDataTable.Rows.Count
                    If id = m_ColourDataTable.Rows(ColourCount)("ID") And editvalue = _
                    m_ColourDataTable.Rows(ColourCount)("Combination_Name") Then
                        m_ColourDataTable.Rows(ColourCount).BeginEdit()
                        m_ColourDataTable.Rows(ColourCount)("Combination_Name") = txtColourName.Text.Trim
                        m_ColourDataTable.Rows(ColourCount).EndEdit()
                        m_ColourDataTable.AcceptChanges()
                        Exit While
                    End If
                    ColourCount = ColourCount + 1
                End While
    
         m_ColourDataAdapter.Update(m_ColourDataTable)
    m using access 07 and vb.net 05
    no changes are being made to the data base and no error is occuring
    Thursday, October 15, 2009 12:32 PM

Answers

  • While ColourCount < m_ColourDataTable.Rows.Count
                    If id = m_ColourDataTable.Rows(ColourCount)("ID") And editvalue = _
                    m_ColourDataTable.Rows(ColourCount)("Combination_Name") Then
    
                        m_ColourDataTable.Rows(ColourCount).BeginEdit()
                        m_ColourDataTable.Rows(ColourCount)("Combination_Name") = txtColourName.Text.Trim
                        m_ColourDataAdapter.Update(m_ColourDataTable)
                        m_ColourDataTable.Rows(ColourCount).EndEdit()
    
                        Exit While
                    End If
                    ColourCount = ColourCount + 1
                End While



    thnx guys. this is wht worked finally
    • Marked as answer by waheebc Thursday, October 15, 2009 4:35 PM
    Thursday, October 15, 2009 3:56 PM

All replies

  • If you have not yet, I would break before the while and see what value is in ColourCount and also check the condition of the if to see if it is resolving to true. You may not be running that code.

    Formerly MichaelTx
    Thursday, October 15, 2009 1:21 PM
  • Comment the below line and see if it works.

    m_ColourDataTable.AcceptChanges()


    Check following msdn link to get more information on how AcceptChanges works.
    http://msdn.microsoft.com/en-us/library/system.data.datatable.acceptchanges.aspx


    Thursday, October 15, 2009 1:48 PM
  • ive tried breaking it and the if condition resolves to true.
    Thursday, October 15, 2009 3:41 PM
  • accept changes is not updating the database.
    any other mistake tht i am doing pzl help...
    Thursday, October 15, 2009 3:42 PM
  • Don't use AcceptChanges() and Update it's redundant. Use Only Update

    m_ColourDataAdapter.Update(m_ColourDataTable)


    John
    • Proposed as answer by Codernater Thursday, October 15, 2009 4:07 PM
    Thursday, October 15, 2009 3:45 PM
  • tried tht as well. result is the same.

    Thursday, October 15, 2009 3:48 PM
  • While ColourCount < m_ColourDataTable.Rows.Count
                    If id = m_ColourDataTable.Rows(ColourCount)("ID") And editvalue = _
                    m_ColourDataTable.Rows(ColourCount)("Combination_Name") Then
    
                        m_ColourDataTable.Rows(ColourCount).BeginEdit()
                        m_ColourDataTable.Rows(ColourCount)("Combination_Name") = txtColourName.Text.Trim
                        m_ColourDataAdapter.Update(m_ColourDataTable)
                        m_ColourDataTable.Rows(ColourCount).EndEdit()
    
                        Exit While
                    End If
                    ColourCount = ColourCount + 1
                End While



    thnx guys. this is wht worked finally
    • Marked as answer by waheebc Thursday, October 15, 2009 4:35 PM
    Thursday, October 15, 2009 3:56 PM
  • Did you debug inside the loop and looked into your datatable preview to see if the data changed in datatable.
    If Yes, you need to send your update separate to the database. this code below shows how to update you table in db but it's for sql (should be very close to access)

    Dim dtRowsModified As DataTable = myDataTable.GetChanges(DataRowState.Modified)
    
    
                    If Not (dtRowsModified Is Nothing) Then
                        Using Adapter As New SqlDataAdapter()
                            Using Conn As New SqlConnection(My.Settings.IHERPConnection)
                                Adapter.UpdateCommand = New SqlCommand( _
                                        "UPDATE [Northwind].[dbo].[Customers]
       SET [CompanyName] = @CompanyName
          ,[ContactName] = @ContactName
     WHERE CustomerID = @CustomerID", Conn)
                                
                                Adapter.UpdateCommand.CommandType = CommandType.Text
                                'Get the Parameter Return Value
                                Dim myParm As SqlParameter = Adapter.UpdateCommand.Parameters.Add("@RowCount", SqlDbType.Int)
                                myParm.Direction = ParameterDirection.ReturnValue
                                With Adapter.UpdateCommand.Parameters
                                    .Add("@CustomerID", SqlDbType.Int, 4, "CustomerID").SourceVersion = DataRowVersion.Original
                                    .Add("@CompanyName", SqlDbType.nvarchar, 25, "CompanyName").sourceVersion = DataRowVersion.Current
    					  .Add("@CompanyName", SqlDbType.nvarchar, 25, "CompanyName").sourceVersion = DataRowVersion.Current
                                End With
                                Adapter.Update(mydataTable) 'Update Datatable
                            End Using
                        End Using
    

    John
    Thursday, October 15, 2009 4:06 PM