locked
accessing variables from a public shared subroutine RRS feed

  • Question

  • User1764424651 posted

    Hi,

    I was wondering if someone can help me.

    I have created a public shared subroutine in a class file in asp.net 2 (vb.net) web site app_folder.

    The subroutine retrives some values from a sql database via an sql query and assigns the datareader field values to several variables

    The problem is I can't seem to assign the variables from the subroutine to the variables and textbox controls in the asp.net page I am calling the subroutine from. The textbox text value just appear blank.

    By the way, if I response.write the variables in the subroutine they appear populated by the sql data reader.

    I hope someone can help

     Cheers

    Mark :)

     

     

    Sunday, February 15, 2009 10:32 AM

Answers

  • User-158764254 posted

    your shared subroutine should not be trying to assign values directly to controls or variables in your page.  instead, it should return the data to the page (via a return value, dataset, datatable, struct, class etc...).  then the page that receives those values would assign them to the appropriate control or variable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 15, 2009 10:38 AM
  • User-158764254 posted

    something like this:

    Partial Class Test
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim sd As SomeData = SharedMethods.GetSomeData
            If Not sd Is Nothing Then
                Me.Textbox1.Text = sd.firstname
                Me.Textbox2.Text = sd.lastname
            End If
    
        End Sub
    
    End Class
    
    'in appcode - or better yet - create this in a data access layer component
    Public Class SharedMethods
        Public Shared Function GetSomeData() As SomeData
            Dim result As New SomeData
            'get data from your db here...
            result.firstname = "John"
            result.lastname = "Doe"
    
            Return result
        End Function
    End Class
    
    'a simple class (or struct) to carry the data back
    Public Class SomeData
        Public firstname As String
        Public lastname As String
    End Class
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 15, 2009 5:07 PM
  • User-158764254 posted

    Mostly correct.  The custom class represents a DTO.  The DTO instance actually gets created in the method call (not in the page) and then gets returned to the page and consumed by the page.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 15, 2009 7:01 PM

All replies

  • User-158764254 posted

    your shared subroutine should not be trying to assign values directly to controls or variables in your page.  instead, it should return the data to the page (via a return value, dataset, datatable, struct, class etc...).  then the page that receives those values would assign them to the appropriate control or variable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 15, 2009 10:38 AM
  • User1764424651 posted

    Hi Mike,

    Thanks for your reply.

    How do I return the values and access these return values from my asp.net page?

     Many thanks

     

    Mark :)

    Sunday, February 15, 2009 4:59 PM
  • User-158764254 posted

    something like this:

    Partial Class Test
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim sd As SomeData = SharedMethods.GetSomeData
            If Not sd Is Nothing Then
                Me.Textbox1.Text = sd.firstname
                Me.Textbox2.Text = sd.lastname
            End If
    
        End Sub
    
    End Class
    
    'in appcode - or better yet - create this in a data access layer component
    Public Class SharedMethods
        Public Shared Function GetSomeData() As SomeData
            Dim result As New SomeData
            'get data from your db here...
            result.firstname = "John"
            result.lastname = "Doe"
    
            Return result
        End Function
    End Class
    
    'a simple class (or struct) to carry the data back
    Public Class SomeData
        Public firstname As String
        Public lastname As String
    End Class
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 15, 2009 5:07 PM
  • User1764424651 posted

    Hi,

    Thanks for this

    What should I put in place of SomeData?

    Is this code in vb.net?

    Cheers

    Mark :) 

     

     

    Cheers

    Mark :)

    Sunday, February 15, 2009 5:34 PM
  • User-158764254 posted

    What you name it is completely up to you but should somehow describe the data that it contains.  If it's data about a product, it could be "ProductInfo" or "ProductSummary", or perhaps it data about a person... "MemberInfo"  etc...

    The code is VB, but should convert to C# if needed without any issue using the link in my signature block.

    Sunday, February 15, 2009 5:40 PM
  • User1764424651 posted

    Hi Mike,

     Thanks for this. This solved my problem.

    So basically you told me to create a custom class , a custom object and a method and create a new instance of that object on my asp.net page.

    Is this correct? 

    Cheers

     Mark :)

    Sunday, February 15, 2009 6:33 PM
  • User-158764254 posted

    Mostly correct.  The custom class represents a DTO.  The DTO instance actually gets created in the method call (not in the page) and then gets returned to the page and consumed by the page.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 15, 2009 7:01 PM
  • User1764424651 posted

    Thanks you have been very helpfull

    cheers

    Mark :)

    Sunday, February 15, 2009 7:12 PM
  • User-319574463 posted

     

    Thanks you have been very helpfull

    Please click "Mark As Answer" against one or more of Mike's replies so we may all know that your question has been answered.

    Tuesday, February 17, 2009 4:19 PM