CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Posts
    7

    DataAdapter does not update a record

    Hi, I have been researching all that is related to DataSources, DataReaders, DataAdapters, etc, with an Access 2010 DB bound to a DataGridView.


    Everything is fine, just one line fails and is driving me nuts!
    Here is the code:

    Code:
    Private Sub ModificaPrecios()
            Dim mensaje As String
            Dim variacion As Integer
            Dim i As Int32
    
            mensaje = "¿Esta seguro que desea "
            If rdbAumento.Checked Then
                mensaje = mensaje & "aumentar un "
            Else
                mensaje = mensaje & "descontar un "
            End If
            If txPorcentaje.Text.Length > 0 Then
                mensaje = mensaje & txPorcentaje.Text & " porciento de sus listas de precios?"
                If MessageBox.Show(mensaje, "Actualizo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
    'I asked if user is sure to change the column
    'rdbAumento states whether the column value is increased or decreased
                    If rdbAumento.Checked Then
                        variacion = Convert.ToInt32(txPorcentaje.Text)
                        For i = 0 To gridPrecios.Rows.Count - 2
                            gridPrecios.Item(5, i).Value = (gridPrecios.Item(5, i).Value * (100 + variacion) / 100)
                        Next
                    Else
                        variacion = Convert.ToInt32(txPorcentaje.Text)
                        For i = 0 To gridPrecios.Rows.Count - 2
                            gridPrecios.Item(5, i).Value = (gridPrecios.Item(5, i).Value * (100 - variacion) / 100)
                        Next
                    End If
                End If
            End If
            UpdateGrilla()
        End Sub
    
        Private Sub UpdateGrilla()
            Dim cb As New OleDb.OleDbCommandBuilder(da)
    
    'If txporcentaje.text is not null, then user increased or decreased ALL THE COLUMN with the For above. Else, The user just changed ONCE CELL MANUALLY
            If txPorcentaje.Text <> nothing Then
                If MsgBox("Desea guardar los cambios realizados?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then
    
    
    'This line is failling. See below for description
                    da.Update(ds, "datos")
    
                    'Hubocambio se usa para meter datos a HistorialPrecios
                    hubocambio = True
                End If
            Else
                If MsgBox("Save data?.", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then
    
    'The odd thing that is killing me is that this line is NOT failing.
                    da.Update(ds, "datos")
    
                    'Hubocambio se usa para meter datos a HistorialPrecios
                    hubocambio = True
                End If
            End If
            If hubocambio Then
                InsertaCambioPrecio()
            End If
    
        End Sub
    
        Private Sub InsertaCambioPrecio()
            'Some routine that saves a record as a log of what is being done.
        End Sub
    The line that fails, actually updates my database, but somehow, the update is not performed on the line that belongs to the first row of the grid. What is more, if I change the value manually, the same update method works fine. What is going on here?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: DataAdapter does not update a record

    Because it's still in this statement?

    Code:
           If txPorcentaje.Text.Length > 0 Then
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2011
    Posts
    7

    Re: DataAdapter does not update a record

    I'm not sure I got your point there. Sorry, English is not my native language

    This is the form at runtime:
    http://img692.imageshack.us/img692/8976/formww.jpg

    In any case, that line works like this:

    if txporcentaje.lenght > 0 states that the textbox has some text. In this case, '10' is telling me that I have to apply a 10&#37; increase to column 'Precio', hence the For loop afterwards.

    If it doesn't have text at all, then I assume the user changed a cell manually (on purpose or by accident) thus, there is no For loop there and I double check the user is sure to do so.

    When all this has been executed (either For loop or cell changed manually), I call the function "UpdateGrilla()" which uses the DataAdapter to save those changes to my DB. If manual change was done, DA works just fine. If the For was executed, DA misses grid's first record. This is what is thrilling me. I mean, da.update is automated, isn't it?

    Thanks for your time!
    Last edited by leansoli; February 16th, 2011 at 12:13 PM.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: DataAdapter does not update a record

    Sure you want a NEW instance here? Why not pass the old one?

    Code:
        Dim cb As New OleDb.OleDbCommandBuilder(da)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Feb 2011
    Posts
    7

    Re: DataAdapter does not update a record

    Nice catch!

    I developed that small code in another project and I am re-using it. Thanks for that mate!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured