473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Function take in value and return value

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.Connecti onStrings("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

Nov 3 '07 #1
6 2743
On Nov 3, 1:18 pm, CSINVA <mcse_instruc...@yahoo.comwrote:
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.Connecti onStrings("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

Nov 3 '07 #2
"CSINVA" <mc*************@yahoo.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
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?sourc...=ExecuteScalar
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 3 '07 #3
On Nov 3, 1:41 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
"CSINVA" <mcse_instruc...@yahoo.comwrote in message

news:11**********************@19g2000hsx.googlegro ups.com...
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?sourc...-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'
Nov 3 '07 #4
On Nov 3, 1:54 pm, CSINVA <mcse_instruc...@yahoo.comwrote:
On Nov 3, 1:41 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
"CSINVA" <mcse_instruc...@yahoo.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
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?sourc...-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)

Nov 3 '07 #5
"CSINVA" <mc*************@yahoo.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
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

Nov 3 '07 #6
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" <mc*************@yahoo.comwrote in message
news:11*********************@50g2000hsm.googlegrou ps.com...
On Nov 3, 1:18 pm, CSINVA <mcse_instruc...@yahoo.comwrote:
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.Connecti onStrings("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

Nov 8 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
6
by: marcelf3 | last post by:
Hello.. This page opens a window with some information, but everytime the user changes a field in the parent window, the child window needs to be closed. These 2 functions were supposed to do the...
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
4
by: Paul | last post by:
Anyone have code that emulates the Nz function in Microsoft Access? In Access it is: Nz(Value as variant, Optional ValueIfNull as Variant) as Variant
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
8
by: optimistx | last post by:
In excellent YAHOO user interface programs I see often extra parenthesis like this (foo=function(){ alert('I am foo'); })(); instead of bar=function(){
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.