none
Adding Datagrid new rows to the Database table RRS feed

  • Question

  • Hey Guys, New Here. I've been working on a datagridview that can add certain items. i can modify it and add the items with no problem, but i created a save button to save those changes to the database and nothing happens. Here is the code. I am using Visual Basic and WindowsForms.

    Public Class FrmAgregarMenu
    
        Private Sub PedidoBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Me.Validate()
            Me.PedidoBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.EasyPedidos2DataSet)
    
        End Sub
    
        
    
        
    
        Private Sub FrmAgregarMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: esta línea de código carga datos en la tabla 'EasyPedidos2DataSet.Pedido_Consulta' Puede moverla o quitarla según sea necesario.
            Me.Pedido_ConsultaTableAdapter.Fill(Me.EasyPedidos2DataSet.Pedido_Consulta)
            'TODO: esta línea de código carga datos en la tabla 'EasyPedidos2DataSet.Items' Puede moverla o quitarla según sea necesario.
            Me.ItemsTableAdapter.Fill(Me.EasyPedidos2DataSet.Items)
            'TODO: esta línea de código carga datos en la tabla 'EasyPedidos2DataSet.Pedido' Puede moverla o quitarla según sea necesario.
            Me.PedidoTableAdapter.Fill(Me.EasyPedidos2DataSet.Pedido)
            'TxtNumOrden.Enabled = False
            'TxtItemCodigo.Visible = False
            TxtItemCodigo.Enabled = False
            TxtNumPed.Enabled = False
            ' TxtNumPed.Visible = False
            TxtOrdenDePedido.Enabled = False
            'TxtOrdenDePedido.Visible = False
            'TxtOrdenDePedido.Text=TxtNumOrden.text
            TxtNumPedTmp.Enabled = False
            TxtNumPedTmp.Visible = False
    
    
        End Sub
        Public Sub Filter()
            Me.Pedido_ConsultaBindingSource.Filter = String.Format("{0} = '{1}'", PED_Orden.Name, TxtNumOrden.Text)
        End Sub
    
        Private Sub TxtNumOrden_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNumOrden.TextChanged
            Call Filter()
        End Sub
    
        Private Sub Pedido_ConsultaDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Pedido_ConsultaDataGridView.CellContentClick
    
        End Sub
    
    
        Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter
    
        End Sub
    
        Private Sub CmdAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAgregar.Click
            Me.Pedido_ConsultaBindingSource.AddNew()
            Me.Pedido_ConsultaDataGridView.CurrentCell = Me.Pedido_ConsultaDataGridView(2, Me.Pedido_ConsultaDataGridView.CurrentRow.Index)
            Me.Pedido_ConsultaDataGridView.CurrentCell.Value = TxtItemCodigo.Text
            Me.Pedido_ConsultaDataGridView.CurrentCell = Me.Pedido_ConsultaDataGridView(3, Me.Pedido_ConsultaDataGridView.CurrentRow.Index)
            Me.Pedido_ConsultaDataGridView.CurrentCell.Value = CmbNombreItem.Text
            
            'Me.EasyPedidos2DataSet.AcceptChanges()
            'Me.Pedido_ConsultaDataGridView.Refresh()
            Me.TxtOrdenDePedido.Text = FrmAgregarOrden.TxtOrdenId.Text
            Me.TxtNumOrden.Text = Me.TxtOrdenDePedido.Text
            Me.TxtOrdenDePedido.Text = Me.TxtNumPedTmp.Text
    
    
    
    
            
        End Sub
    
        
        Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter
    
        End Sub
    
        Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
            Me.Pedido_ConsultaBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.EasyPedidos2DataSet)
            'Me.EasyPedidos2DataSet.AcceptChanges()
        End Sub
    End Class


    • Edited by ALMIGHTY6 Monday, April 1, 2013 4:18 PM
    Monday, April 1, 2013 4:17 PM

Answers

All replies

  • Hello,

    First off not sure of some of the code being it's not English, secondly what is your database i.e. MS-Access local (on a user's computer), on a file server or perhaps SQL-Server database etc ?

    Generally with MS-Access I suggest add a new record, close the app, open the database usually in the same folder as the app (not the one shown in Solution Explorer in the IDE), look for the new record, is it there? Now run the app again, if the record is not there then in the IDE, solution explorer, select the database, select properties, if the propery Copy to output folder is "Copy Always" change it to "Copy if newer", now run the app, add a record, close the app, run the app, if the record there? A caveat with Copy if Newer is, if you change anything in the database it will invoke Copy if newer and overwrite the database in the app folder.

    Another common reason is that the table or tables you want to add/edit/remove records from do not have a proper primary key so the data wizard may not generate update or delete SQL statements.

    Of course there are other reasons, these are two common reasons listed above.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, April 1, 2013 5:46 PM
    Moderator
  • i am using an MS Access Database on a local computer. I have other forms which add data normally (With textbox and ComboBox) thaty work fine, but when i place the Save Button (Button6) nothing happens.

    Im currently at work so i'll get back to you on that to see if it helpes.

    On an unrelated note, is there a way to set the value of an invisible cell?
    • Edited by ALMIGHTY6 Monday, April 1, 2013 6:06 PM
    Monday, April 1, 2013 6:06 PM
  • In regards to setting a value of a hidden cell, you set a value for the current row of your data source not the DataGridView cell which is data bound.

    In regards to "nothing happening", there is, just not what you expect i.e. generated classes may (as I already pointed out) may not have a proper update or delete commands and if so will not error out as these classes have assertion built in to stop this.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, April 1, 2013 6:34 PM
    Moderator
  • Hi ALMIGHTY6,

    First of all, change you code to 

    int i = TableAdapterManager.UpdateAll

    Therefore, you can get how many rows get updated. 

    Second, do not call DataSet.AcceptChanges() method before you call update method. Otherwise, on data will be update.

    Third, call validate method before you update date. If you doesn't call this method, the data in dataset may not changed,see http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/c816e1dd-ceb7-488b-b392-078fca1555ee/.

    Best Regards,


    Bob Wu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 2, 2013 9:47 AM
    Moderator