locked
How can I make this into reusable code? RRS feed

  • Question

  • User-556379245 posted

    Hello all.

    I am familiar with creating functions and public subs that can be called from different locations but im having trouble making this bit of code that initializes a database driven menu reusable from a class file

     

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim ds As New DataSet()
    Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ToString())
    Dim sql As String = "SELECT MenuID, Text, Description, ParentID FROM Menu ORDER BY OrderBy"
    Dim da As New SqlDataAdapter(sql, conn)
    da.Fill(ds)
    da.Dispose()
    End Using
    ds.DataSetName = "Menus"
    ds.Tables(0).TableName = "Menu"
    Dim relation As New DataRelation("ParentChild", ds.Tables("Menu").Columns("MenuID"), ds.Tables("Menu").Columns("ParentID"), True)

    relation.Nested = True
    ds.Relations.Add(relation)

    xmlDataSource.Data = ds.GetXml()

    If Request.Params("Page") IsNot Nothing Then
    Page.Controls.Add(New System.Web.UI.LiteralControl("You selected " + Request.Params("Page")))
    End If

    End Sub
      Thanks for your help!
    Wednesday, August 13, 2008 9:18 AM

Answers

  • User599164775 posted

    CreateMenu(Page, xmlDataSource), i assume that xmlDataSource will different in all class file.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            CreateMenu(Page, xmlDataSource)
        End Sub
    
        Public Sub CreateMenu(ByRef _Page As Page, ByRef _XmlDataSource As XmlDataSource)
            Dim ds As New DataSet()
            Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ToString())
    
                Dim sql As String = "SELECT MenuID, Text, Description, ParentID FROM Menu ORDER BY OrderBy"
                Dim da As New SqlDataAdapter(sql, conn)
                da.Fill(ds)
                da.Dispose()
            End Using
            ds.DataSetName = "Menus"
            ds.Tables(0).TableName = "Menu"
            Dim relation As New DataRelation("ParentChild", ds.Tables("Menu").Columns("MenuID"), ds.Tables("Menu").Columns("ParentID"), True)
            relation.Nested = True
            ds.Relations.Add(relation)
            _XmlDataSource.Data = ds.GetXml()
            If Request.Params("Page") IsNot Nothing Then
                _Page.Controls.Add(New System.Web.UI.LiteralControl("You selected " + Request.Params("Page")))
            End If
        End Sub
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 13, 2008 9:47 AM