none
Updating a table RRS feed

  • Question

  • I use the code below to input information into a table using the datagridview, dgvSignlist.

    This works pretty well for me, or at least I have no problem with this.  However, I would like to do something a little different.

    I have a namespace that I like to use to put routines/functions, to carry out jobs like the one below, that are used with this form class (frmSignList.vb). 

        Private Sub dgvSignList_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSignList.CellDoubleClick
    
                    'Remove Approver/Trainee from list
                    dgvSignList.Rows(e.RowIndex).Cells("chrSignature").Value = CStr("Removed by Change Admin, " & glbstrLogin & " " & CStr(DateAndTime.Now))
                    dgvSignList.Rows(e.RowIndex).Cells("blnRemove").Value = 1
                    Me.Validate()
                    lnkChangeApproveBindingSource.EndEdit()
                    LnkChangeApproveTableAdapter.Update(Me._MasterBase_1_0DataSet)
                    LnkChangeApproveTableAdapter.FillByChangeID(Me._MasterBase_1_0DataSet.lnkChangeApprove, glbintChangeID) 'refresh DGV
    
    LnkChangeApproveTableAdapter.FillByChangeID(Me._MasterBase_1_0DataSet.lnkChangeApprove, glbintChangeID)
    
    End Sub

    For my namespace I created a class and in the class I have the following routine

    Public Class CommonRoutines
    
    Public Sub RemoveFromList()
    
                        'Remove Approver from list
                        CType(frmSignList.lnkChangeApproveBindingSource.Current, DataRowView).Item("chrSignature") = CStr("Removed by Change Admin, " & glbstrAdmin & " " & CStr(DateAndTime.Now))
                        CType(frmSignList.lnkChangeApproveBindingSource.Current, DataRowView).Item("blnRemove") = 1
                        frmSignList.Validate()
                        frmSignList.lnkChangeApproveBindingSource.EndEdit()
                        frmSignList.LnkChangeApproveTableAdapter.Update(frmSignList._MasterBase_1_0DataSet)
                        frmSignList.LnkChangeApproveTableAdapter.FillByChangeID(frmSignList._MasterBase_1_0DataSet.lnkChangeApprove, glbintChangeID) 'refresh DGV
    
    End Sub
    
    End Class
    

    To run the namespace code I just replace the code in the double-click event with a call to the routine in the namespace.

    When I run the code in the namespace the table is not updated with the data.  I really had thought that this should work and am at a loss as to why it does not achieve the same end as the code in the double click event.


    gwboolean

    Sunday, April 29, 2018 5:25 AM

All replies

  • To run the namespace code I just replace the code in the double-click event with a call to the routine in the namespace.

    What is frmSignList?  You haven't shown code that sets that variable to refer to the form that you want to update from.  It is probably executing that code on the wrong instance of the form.   The best style would pass that form as an argument to the method, so that the method is applicable to any instance.

    Sunday, April 29, 2018 5:47 AM
  • The way to extend a TableAdapter is to first open the designer (from the project menu in visual Studio, select show all files) find the namespace for the code e.g. if I have

    We then search for Namespace, in my case I have 

    Add a new class, provide the namespace, within the namespace do a partial class for the class you want to change it's behavior, for example, I want to add error logging to an update method.

    Find the original method in the designer e.g.

    <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
        Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "15.0.0.0"),  _
        Global.System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")>  _
    Public Overloads Overridable Function Update(ByVal dataRow As Global.System.Data.DataRow) As Integer
        Return Me.Adapter.Update(New Global.System.Data.DataRow() {dataRow})
    End Function

    Copy the function signature and alter it, in this case we pass in a file name to write too and end up with something like this.

    Namespace NorthWindAzure1DataSetTableAdapters
        Partial Public Class ShippersTableAdapter
            Public Overridable Overloads Function Update(
                dataRow As DataRow, pFileName As String) As Integer
                Dim lines As String = ""
                If IO.File.Exists(pFileName) Then
                    lines = IO.File.ReadAllText(pFileName)
                End If
    
                Dim results As Integer = 0
                Try
                    results = Adapter.Update(New DataRow() {dataRow})
                Catch ex As Exception
                    lines = String.Concat(lines, Environment.NewLine, ex.Message)
                    IO.File.WriteAllText(pFileName, lines)
                End Try
    
                Return results
    
            End Function
        End Class
    End Namespace
    So the above is the proper way to enhance a class generated by Visual Studio, hope this helps clarifying things.


    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. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, April 29, 2018 10:42 AM
    Moderator
  • frmSignList is the form class in which the code, except that in the namespace (nspSignList) resides.

    gwboolean

    Sunday, April 29, 2018 5:34 PM
  • Karen,

    I have never used the, "partial" before.  Can you explain what that does?

    Other than that, the method appears to be similar to what I have already been doing.  I have been using classes within namespaces for awhile to carry out jobs that can get pretty cluttered in my form classes.  Additionally, I have also carried out jobs that involve the use of tableadapters and bindingsources to add/modify data in tables, and was somewhat surprised when I was unable to update the table from the namespace this time.


    gwboolean

    Sunday, April 29, 2018 5:44 PM
  • In regards to partial class, best to read the docs

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/partial


    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. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Stanly Fan Wednesday, May 2, 2018 1:29 AM
    Sunday, April 29, 2018 6:19 PM
    Moderator
  • frmSignList is the form class in which the code, except that in the namespace (nspSignList) resides.

    You should not use a form class name in this way.  The form class name refers to the default instance of the form, and that is likely not the instance that you are actually using in the code, especially when you have additional modules.

    Change the signature of the method so that it takes a frmSignList object as an argument:

       Public Sub RemoveFromList(ThisForm as frmSignList)

    Change the code in the method to use ThisForm instead of frmSignList.

    In the main code, change the call to the method so that the form instance that you want it to use is included as a parameter to the method call

       RemoveFromList(Me)

    • Proposed as answer by Stanly Fan Wednesday, May 2, 2018 1:29 AM
    Sunday, April 29, 2018 10:12 PM