locked
How to pass variable between Sub RRS feed

  • Question

  • User971301804 posted

    I currently have the following funcion:

        Public Sub GetUserAmount(ByVal ConnectionString As String)
            Dim command1Text As String = "SELECT Wager FROM Donations1 WHERE DonationId = (SELECT COUNT (BidCountHistory) FROM BidCountHistory)"
            Using connection As New SqlConnection(ConnectionString)
                Using command1 As New SqlCommand(command1Text, connection)
                        connection.Open()
                        Dim CurrentWager As Integer = command1.ExecuteScalar
                        connection.Close()
                End Using
            End Using
        End Sub
    which I call via a button click:
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DonateButton.Click
    
            Dim ASPNETDBDataSource As New SqlDataSource()
            ASPNETDBDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString1").ToString()
    
            GetUserAmount(ASPNETDBDataSource.ConnectionString)
            If CurrentWager >= 108 Then
     
    How do I get CurrentWager from the funcion to the button click sub?
     


     

    Thursday, January 14, 2010 7:01 PM

Answers

  • User2069888697 posted

    Couple of ways.

    If the the Sub is in the same code behind you can set the currentwage outside the sub

    Private curentwage As Integer 

    Public Sub GetUserAmount(ByVal ConnectionString As String)   

        Dim command1Text As String = "SELECT Wager FROM Donations1 WHERE DonationId = (SELECT COUNT (BidCountHistory) FROM BidCountHistory)"  

        Using connection As New SqlConnection(ConnectionString)   

            Using command1 As New SqlCommand(command1Text, connection)   

                    connection.Open()   

                     CurrentWager As Integer = command1.ExecuteScalar   

                    connection.Close()   

            End Using   

        End Using   

    End Sub  

     

      If it is in a separate vb code file then you would need to use a function what you have is a sub
       
       Shared Function GetUserAmount(ByVal ConnectionString As String) As Integer  Dim command1Text As String = "SELECT Wager FROM Donations1 WHERE DonationId = (SELECT COUNT (BidCountHistory) FROM BidCountHistory)"  
      
          Using connection As New SqlConnection(ConnectionString)   
      
              Using command1 As New SqlCommand(command1Text, connection)   
      
                      connection.Open()   
      
                       CurrentWager As Integer = command1.ExecuteScalar   
      
                      connection.Close()   
      
              End Using   
      
          End Using  
      
      Return CurrentWager
      
               End Function
      

       
       

    And change to this in click event

    If GetUserAmount(ASPNETDBDataSource.ConnectionString) >= 108 Then

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2010 9:39 PM

All replies

  • User2069888697 posted

    Couple of ways.

    If the the Sub is in the same code behind you can set the currentwage outside the sub

    Private curentwage As Integer 

    Public Sub GetUserAmount(ByVal ConnectionString As String)   

        Dim command1Text As String = "SELECT Wager FROM Donations1 WHERE DonationId = (SELECT COUNT (BidCountHistory) FROM BidCountHistory)"  

        Using connection As New SqlConnection(ConnectionString)   

            Using command1 As New SqlCommand(command1Text, connection)   

                    connection.Open()   

                     CurrentWager As Integer = command1.ExecuteScalar   

                    connection.Close()   

            End Using   

        End Using   

    End Sub  

     

      If it is in a separate vb code file then you would need to use a function what you have is a sub
       
       Shared Function GetUserAmount(ByVal ConnectionString As String) As Integer  Dim command1Text As String = "SELECT Wager FROM Donations1 WHERE DonationId = (SELECT COUNT (BidCountHistory) FROM BidCountHistory)"  
      
          Using connection As New SqlConnection(ConnectionString)   
      
              Using command1 As New SqlCommand(command1Text, connection)   
      
                      connection.Open()   
      
                       CurrentWager As Integer = command1.ExecuteScalar   
      
                      connection.Close()   
      
              End Using   
      
          End Using  
      
      Return CurrentWager
      
               End Function
      

       
       

    And change to this in click event

    If GetUserAmount(ASPNETDBDataSource.ConnectionString) >= 108 Then

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2010 9:39 PM
  • User2069888697 posted

    BTW if the connection string is set up in the web.config you should not have to pass it between subs or functions. 

    Thursday, January 14, 2010 9:42 PM
  • User971301804 posted

    BTW if the connection string is set up in the web.config you should not have to pass it between subs or functions. 

     

    My connectionstrins is setup in the Web.Config, how would I define the sub 'GetCurrentWager' without it?

    Also, I used your first solution because my function is in the same code-behind.  I get no errors but in my buttonclick is not working as planned.  The If statement

    If CurrentWager >= 108 Then


    should fail, CurrentWager should be set to 0 in my current working example, but it still exectues the following commands... How can I check the value of CurrentWager at that point.  If I set a breakpoint it just seems to load forever.

     

    Thursday, January 14, 2010 10:10 PM
  • User971301804 posted

    figured it out.  nevermind 

    Thursday, January 14, 2010 10:16 PM