Function take in value and return value

Discussion in 'ASP .Net' started by CSINVA, Nov 3, 2007.

  1. CSINVA

    CSINVA Guest

    I need to be able to pass a variable to a function and then have the
    function return me a variable.

    I need to pass GetUsrID Tom and get back the corresponding UserID
    associated with tom by running a stored procedure. Then pupule a file
    called lblUserID.text with the result_sUserID data.


    Here is the code:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Button1.Click

    some sufff.....

    subdomain = tom

    Dim result_sUserID As Integer
    'GetUserID
    GetUserID(Subdomain, result_sUserID)

    'Populate lblUserId with result_sUserID
    result_sUserID = lblUserID.Text

    End Sub



    Public Function GetUserID(ByVal Subdomain As String, ByRef
    Result_sUserID As Integer) As Integer

    Dim sUserID As Integer
    'Read SQL Server configuration from web.config
    Dim strConnectionString As String =
    System.Configuration.ConfigurationManager.ConnectionStrings("SiteSQLServer").ConnectionString
    'Create new connection based on web.confg file informaiton
    Dim objConnection As SqlClient.SqlConnection = New
    SqlClient.SqlConnection(strConnectionString)
    'Open the Connection
    Try
    objConnection.Open()
    Catch ex As Exception

    End Try
    Dim myCommand As New SqlCommand("P4YS_GetUserID",
    objConnection)
    Try
    myCommand.CommandType = Data.CommandType.StoredProcedure
    'need to pass Subdomain Parameter to the system for the
    Stored Procedure
    myCommand.Parameters.AddWithValue("@UserWebSite",
    Subdomain)
    objConnection.Open()
    Catch ex As Exception

    End Try

    Dim myReader As SqlDataReader = myCommand.ExecuteReader
    'Read in the first record and grab the first column and putin
    lblUserID.text label

    Do While myReader.Read()
    'look at column "userid" and set the value to sUserID
    sUserID = myReader.Item("Userid")
    Loop

    myReader.Close()
    myReader = Nothing
    objConnection.Close()

    'Return sUserID
    Result_sUserID = sUserID

    End Function
     
    CSINVA, Nov 3, 2007
    #1
    1. Advertising

  2. CSINVA

    CSINVA Guest

    On Nov 3, 1:18 pm, CSINVA <> wrote:
    > I need to be able to pass a variable to a function and then have the
    > function return me a variable.
    >
    > I need to pass GetUsrID Tom and get back the corresponding UserID
    > associated with tom by running a stored procedure. Then pupule a file
    > called lblUserID.text with the result_sUserID data.
    >
    > Here is the code:
    >
    > Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Button1.Click
    >
    > some sufff.....
    >
    > subdomain = tom
    >
    > Dim result_sUserID As Integer
    > 'GetUserID
    > GetUserID(Subdomain, result_sUserID)
    >
    > 'Populate lblUserId with result_sUserID
    > result_sUserID = lblUserID.Text
    >
    > End Sub
    >
    > Public Function GetUserID(ByVal Subdomain As String, ByRef
    > Result_sUserID As Integer) As Integer
    >
    > Dim sUserID As Integer
    > 'Read SQL Server configuration from web.config
    > Dim strConnectionString As String =
    > System.Configuration.ConfigurationManager.ConnectionStrings("SiteSQLServer"­).ConnectionString
    > 'Create new connection based on web.confg file informaiton
    > Dim objConnection As SqlClient.SqlConnection = New
    > SqlClient.SqlConnection(strConnectionString)
    > 'Open the Connection
    > Try
    > objConnection.Open()
    > Catch ex As Exception
    >
    > End Try
    > Dim myCommand As New SqlCommand("P4YS_GetUserID",
    > objConnection)
    > Try
    > myCommand.CommandType = Data.CommandType.StoredProcedure
    > 'need to pass Subdomain Parameter to the system for the
    > Stored Procedure
    > myCommand.Parameters.AddWithValue("@UserWebSite",
    > Subdomain)
    > objConnection.Open()
    > Catch ex As Exception
    >
    > End Try
    >
    > Dim myReader As SqlDataReader = myCommand.ExecuteReader
    > 'Read in the first record and grab the first column and putin
    > lblUserID.text label
    >
    > Do While myReader.Read()
    > 'look at column "userid" and set the value to sUserID
    > sUserID = myReader.Item("Userid")
    > Loop
    >
    > myReader.Close()
    > myReader = Nothing
    > objConnection.Close()
    >
    > 'Return sUserID
    > Result_sUserID = sUserID
    >
    > End Function


    I guess I forgot to tell you the error message I get.


    Where this code is:
    'Populate lblUserId with result_sUserID
    result_sUserID = lblUserID.Text

    I get the error: Input string was not in a correct format
     
    CSINVA, Nov 3, 2007
    #2
    1. Advertising

  3. Mark Rae [MVP], Nov 3, 2007
    #3
  4. CSINVA

    CSINVA Guest

    On Nov 3, 1:41 pm, "Mark Rae [MVP]" <> wrote:
    > "CSINVA" <> wrote in message
    >
    > news:...
    >
    > > Dim myReader As SqlDataReader = myCommand.ExecuteReader
    > > 'Read in the first record and grab the first column

    >
    > No need to use a DataReader for that - use ExecuteScalar instead:
    >
    > http://www.google.co.uk/search?sourceid=navclient&hl=en-GB&ie=UTF-8&r...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    I changed to

    Dim myReader As SqlDataReader = myCommand.ExecuteScalar

    And now I get: Unable to cast object of type 'System.Int32' to type
    'System.Data.SqlClient.SqlDataReader'
     
    CSINVA, Nov 3, 2007
    #4
  5. CSINVA

    CSINVA Guest

    On Nov 3, 1:54 pm, CSINVA <> wrote:
    > On Nov 3, 1:41 pm, "Mark Rae [MVP]" <> wrote:
    >
    > > "CSINVA" <> wrote in message

    >
    > >news:...

    >
    > > > Dim myReader As SqlDataReader = myCommand.ExecuteReader
    > > > 'Read in the first record and grab the first column

    >
    > > No need to use a DataReader for that - use ExecuteScalar instead:

    >
    > >http://www.google.co.uk/search?sourceid=navclient&hl=en-GB&ie=UTF-8&r...

    >
    > > --
    > > Mark Rae
    > > ASP.NET MVPhttp://www.markrae.net

    >
    > I changed to
    >
    > Dim myReader As SqlDataReader = myCommand.ExecuteScalar
    >
    > And now I get: Unable to cast object of type 'System.Int32' to type
    > 'System.Data.SqlClient.SqlDataReader'


    Sorry about that, read it wrong

    I changed it to:

    sUserID = myCommand.ExecuteScalar

    Still get the error: Input string was not in a correct format

    Back where I have GetUserID(Subdomian, result_sUserID)
     
    CSINVA, Nov 3, 2007
    #5
  6. "CSINVA" <> wrote in message
    news:...

    > Still get the error: Input string was not in a correct format
    >
    > Back where I have GetUserID(Subdomian, result_sUserID)


    So set a breakpoint on that line and inspect the values of the two
    arguments...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Nov 3, 2007
    #6
  7. It appears as though you've got your assignment statement backwards. From
    what I could tell looking at your code, you want to get a value from the
    database (result_sUserID) and populate a label with that value. The line
    causing your failure is

    > 'Populate lblUserId with result_sUserID
    > result_sUserID = lblUserID.Text


    Try reversing the assignment to this...

    'Populate lblUserId with result_sUserID
    lblUserID.Text = result_sUserID.ToString()

    HTH

    Paul

    "CSINVA" <> wrote in message
    news:...
    On Nov 3, 1:18 pm, CSINVA <> wrote:
    > I need to be able to pass a variable to a function and then have the
    > function return me a variable.
    >
    > I need to pass GetUsrID Tom and get back the corresponding UserID
    > associated with tom by running a stored procedure. Then pupule a file
    > called lblUserID.text with the result_sUserID data.
    >
    > Here is the code:
    >
    > Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Button1.Click
    >
    > some sufff.....
    >
    > subdomain = tom
    >
    > Dim result_sUserID As Integer
    > 'GetUserID
    > GetUserID(Subdomain, result_sUserID)
    >
    > 'Populate lblUserId with result_sUserID
    > result_sUserID = lblUserID.Text
    >
    > End Sub
    >
    > Public Function GetUserID(ByVal Subdomain As String, ByRef
    > Result_sUserID As Integer) As Integer
    >
    > Dim sUserID As Integer
    > 'Read SQL Server configuration from web.config
    > Dim strConnectionString As String =
    > System.Configuration.ConfigurationManager.ConnectionStrings("SiteSQLServer"­).ConnectionString
    > 'Create new connection based on web.confg file informaiton
    > Dim objConnection As SqlClient.SqlConnection = New
    > SqlClient.SqlConnection(strConnectionString)
    > 'Open the Connection
    > Try
    > objConnection.Open()
    > Catch ex As Exception
    >
    > End Try
    > Dim myCommand As New SqlCommand("P4YS_GetUserID",
    > objConnection)
    > Try
    > myCommand.CommandType = Data.CommandType.StoredProcedure
    > 'need to pass Subdomain Parameter to the system for the
    > Stored Procedure
    > myCommand.Parameters.AddWithValue("@UserWebSite",
    > Subdomain)
    > objConnection.Open()
    > Catch ex As Exception
    >
    > End Try
    >
    > Dim myReader As SqlDataReader = myCommand.ExecuteReader
    > 'Read in the first record and grab the first column and putin
    > lblUserID.text label
    >
    > Do While myReader.Read()
    > 'look at column "userid" and set the value to sUserID
    > sUserID = myReader.Item("Userid")
    > Loop
    >
    > myReader.Close()
    > myReader = Nothing
    > objConnection.Close()
    >
    > 'Return sUserID
    > Result_sUserID = sUserID
    >
    > End Function


    I guess I forgot to tell you the error message I get.


    Where this code is:
    'Populate lblUserId with result_sUserID
    result_sUserID = lblUserID.Text

    I get the error: Input string was not in a correct format
     
    Paul Delcogliano, Nov 7, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pete Becker
    Replies:
    5
    Views:
    1,027
    =?iso-8859-1?Q?Ali_=C7ehreli?=
    Nov 19, 2004
  2. Daniel Haude
    Replies:
    1
    Views:
    377
    Les Cargill
    Feb 1, 2004
  3. Seong-Kook Shin
    Replies:
    1
    Views:
    506
    Richard Bos
    Jun 18, 2004
  4. Greenhorn
    Replies:
    15
    Views:
    852
    Keith Thompson
    Mar 6, 2005
  5. Replies:
    4
    Views:
    556
    James Kanze
    Jul 22, 2008
Loading...

Share This Page