Function take in value and return value

C

CSINVA

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
 
C

CSINVA

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
 
C

CSINVA

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)
 
M

Mark Rae [MVP]

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...
 
P

Paul Delcogliano

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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top