Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Function take in value and return value

Reply
Thread Tools

Function take in value and return value

 
 
CSINVA
Guest
Posts: n/a
 
      11-03-2007
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

 
Reply With Quote
 
 
 
 
CSINVA
Guest
Posts: n/a
 
      11-03-2007
On Nov 3, 1:18 pm, CSINVA <(E-Mail Removed)> 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.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

 
Reply With Quote
 
 
 
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      11-03-2007
"CSINVA" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
CSINVA
Guest
Posts: n/a
 
      11-03-2007
On Nov 3, 1:41 pm, "Mark Rae [MVP]" <(E-Mail Removed)> wrote:
> "CSINVA" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) 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'


 
Reply With Quote
 
CSINVA
Guest
Posts: n/a
 
      11-03-2007
On Nov 3, 1:54 pm, CSINVA <(E-Mail Removed)> wrote:
> On Nov 3, 1:41 pm, "Mark Rae [MVP]" <(E-Mail Removed)> wrote:
>
> > "CSINVA" <(E-Mail Removed)> wrote in message

>
> >news:(E-Mail Removed) oups.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)



 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      11-03-2007
"CSINVA" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
Paul Delcogliano
Guest
Posts: n/a
 
      11-07-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
On Nov 3, 1:18 pm, CSINVA <(E-Mail Removed)> 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.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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
typedef'd return type of a template class member function: take 2 Pete Becker C++ 5 11-19-2004 12:50 AM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
How many Zen Buddhists does it take to cast malloc()s return value? Daniel Haude C Programming 1 02-01-2004 02:57 AM



Advertisments