none
Can I use Partial class to enhance a typed dataset without losing the custom code? RRS feed

  • Question

  • Hi All,

    I wanted to see if I could use a Partial class for one of the datatable classes to add custom code so that when the dataset is regenerated I don't lose my code.

    Partial public Class WA_MMTP_TrackerDataSet1

     'Code for dataset

     Partial Public Class PATIENTSTableAdapter

            <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter"),  _
             Global.System.ComponentModel.DataObjectMethodAttribute(Global.System.ComponentModel.DataObjectMethodType.Fill, true)>  _
            Public Overloads Overridable Function FillbyClinicianId(ByVal dataTable As WA_MMTP_TrackerDataSet1.PATIENTSDataTable, ClinicianId As Integer, sql As string) As Integer
                 Dim dd As System.Data.SqlClient.SqlCommand
                dd = new System.Data.SqlClient.SqlCommand
                dd.CommandText = Sql
                dd.Connection = OpenConnection() '  UsersTableAdapter1.Connection

                Me.Adapter.SelectCommand = dd 'Me.CommandCollection(0)
                If (Me.ClearBeforeFill = true) Then
                    dataTable.Clear
                End If
                Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
                Return returnValue
            End Function

    'Other code for datatable

    end class

    end class

    I'd like to take the function FillbyClinicianId and put it in a separate partial class like so and remove the function by the same name from the original file (dataset file):

    Partial Public Class PATIENTSTableAdapter

            <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter"),  _
             Global.System.ComponentModel.DataObjectMethodAttribute(Global.System.ComponentModel.DataObjectMethodType.Fill, true)>  _
            Public Overloads Overridable Function FillbyClinicianId(ByVal dataTable As WA_MMTP_TrackerDataSet1.PATIENTSDataTable, ClinicianId As Integer, sql As string) As Integer
                 Dim dd As System.Data.SqlClient.SqlCommand
                dd = new System.Data.SqlClient.SqlCommand
                dd.CommandText = Sql
                dd.Connection = OpenConnection() '  UsersTableAdapter1.Connection

                Me.Adapter.SelectCommand = dd 'Me.CommandCollection(0)
                If (Me.ClearBeforeFill = true) Then
                    dataTable.Clear
                End If
                Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
                Return returnValue
            End Function

    end class

    But when I try this, the compiler gives the following errors:

    Error 91 'Adapter' is not a member of 'AttendTrackerFull.WA_MMTP_TrackerDataSet1.PATIENTSTableAdapter'. 

    Error 92 'ClearBeforeFill' is not a member of 'AttendTrackerFull.WA_MMTP_TrackerDataSet1.PATIENTSTableAdapter'. 

    I guess I'm a little confused. once code is moved into the custom partial class it seems to lose any knowledge of the main class file and any references in the class. Am I doing it wrong? or is it a restriction of the .Net since it is in another file. If its because its in another file, my next question would it be better to take the partial class I created to contain my custom code, should I just move my partial class to the bottom of the dataset.designer.vb file? But if I do this, wont it still erase my custom code. Any suggestions?

    Thanks

    Michael

    Thursday, April 2, 2015 5:16 PM

Answers

  • Hello,

    Please remove the Module definition, since the DataAdapter class is not included in any module:

    Namespace DataSet1TableAdapters
    
        Partial Public Class OrderTableAdapter
    
            Inherits Global.System.ComponentModel.Component
    
    
            Public Overridable Overloads Function Fill(ByVal dataTable As DataSet1.OrderDataTable) As Integer
    
                Me.Adapter.SelectCommand = Me.CommandCollection(0)
    
                If (Me.ClearBeforeFill = True) Then
    
                    dataTable.Clear()
    
                End If
    
                Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
    
                Return returnValue
    
            End Function
    
        End Class
    
    End Namespace

    Please marke sure you add the correct namespace, from your original post, the namespace seems to be "'AttendTrackerFull.WA_MMTP_TrackerDataSet1", while in your lasted post, you write "WA_MMTPDataset", I am not sure if you have change it, and please just make sure the namespace should be the same with the adapters.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, April 7, 2015 6:33 AM
    Moderator

All replies

  • By the way, I'm currently using VS 2013
    Thursday, April 2, 2015 6:08 PM
  • Hello Michael,

    >>Error 91 'Adapter' is not a member of 'AttendTrackerFull.WA_MMTP_TrackerDataSet1.PATIENTSTableAdapter'….

    What exception type are them, runtime exception or designer time exception? I created a typed dataset and did similarly what you did, I moved the Fill method to a separate class, and I could still call this function. I am wondering if you have moved this method to a different namespace, because as far as I know the DataSet would contain a default namespace as:

    “namespace ADO.NET.DataSet1TableAdapters {”

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 3, 2015 6:43 AM
    Moderator
  • Thanks for the reply.

    They are compile time errors, but the error shows up even before compiling, so I guess it would be designer time exception also.

    What I did the other day was to create a new class file (didn't add a namespace statement to class) then added the code in prev message. This morning I added a new module and added the prev code in it incased in the namespace namespace ADO.NET.DataSet1TableAdapters statement. But I still have the same errors messages. I tried to set the Custom tool.namespace property of the dataset to WA_MMTPDataset and then did the following in a module:

    namespace WA_MMTPDataset 
    Module Module4
    
    Partial Public Class PATIENTSTableAdapter
    
            <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter"),  _
             Global.System.ComponentModel.DataObjectMethodAttribute(Global.System.ComponentModel.DataObjectMethodType.Fill, true)>  _
            Public Overloads Overridable Function FillbyClinicianId(ByVal dataTable As WA_MMTP_TrackerDataSet1.PATIENTSDataTable, ClinicianId As Integer, sql As string) As Integer
                 Dim dd As System.Data.SqlClient.SqlCommand 
                dd = new System.Data.SqlClient.SqlCommand
                dd.CommandText = Sql
                dd.Connection = OpenConnection() '  UsersTableAdapter1.Connection
    
                Me.Adapter.SelectCommand = dd 'Me.CommandCollection(0)
                If (Me.ClearBeforeFill = true) Then
                    dataTable.Clear
                End If
                Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
                Return returnValue
            End Function
    
    End Class
    
    Partial Public Class UsersTableAdapter
            <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter"),  _
             Global.System.ComponentModel.DataObjectMethodAttribute(Global.System.ComponentModel.DataObjectMethodType.Fill, true)>  _
            Public Overloads Overridable Function FillbyClinicianId(ByVal dataTable As WA_MMTP_TrackerDataSet1.UsersDataTable, ClinicianId As Integer, sql As string) As Integer
                Dim dd3 As System.Data.SqlClient.SqlCommand 
                dd3 = new System.Data.SqlClient.SqlCommand
                dd3.CommandText = Sql
                dd3.Connection = OpenConnection()
                Me.Adapter.SelectCommand = dd3 'Me.CommandCollection(0)
                If (Me.ClearBeforeFill = true) Then
                    dataTable.Clear
                End If
                Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
                Return returnValue
            End Function
    
    End Class
    End Module
    End Namespace
    But I'm still getting the same errors.
    Friday, April 3, 2015 11:05 AM
  • Hello,

    Please remove the Module definition, since the DataAdapter class is not included in any module:

    Namespace DataSet1TableAdapters
    
        Partial Public Class OrderTableAdapter
    
            Inherits Global.System.ComponentModel.Component
    
    
            Public Overridable Overloads Function Fill(ByVal dataTable As DataSet1.OrderDataTable) As Integer
    
                Me.Adapter.SelectCommand = Me.CommandCollection(0)
    
                If (Me.ClearBeforeFill = True) Then
    
                    dataTable.Clear()
    
                End If
    
                Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
    
                Return returnValue
    
            End Function
    
        End Class
    
    End Namespace

    Please marke sure you add the correct namespace, from your original post, the namespace seems to be "'AttendTrackerFull.WA_MMTP_TrackerDataSet1", while in your lasted post, you write "WA_MMTPDataset", I am not sure if you have change it, and please just make sure the namespace should be the same with the adapters.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, April 7, 2015 6:33 AM
    Moderator