I am having a problem with my variables. I am receiving the value of the variable email through an argument from an external program. When inside the For Next loop it contains the correct value but when it exits the loop it suddenly has no value. How would I go about exiting the loop and maintaining the value of the email variable.
Option Strict On
Imports MySql.Data.MySqlClient
Imports System.Runtime.Serialization
Module jrConnect
Sub Main(ByVal cmdArgs() As String)
Dim cs As String = "serverinfo"
Dim conn As New MySqlConnection(cs)
Dim entID As String
Dim email As String
Dim returnValue As Integer = 0
If cmdArgs.Length > 0 Then
For argNum As Integer = 0 To UBound(cmdArgs, 1)
Console.Write("your email address is " & cmdArgs(argNum))
email = cmdArgs(argNum)
'value of email is set
Next argNum
End If
Try
conn.Open()
Console.Write("Connected")
Dim stm As String = "SELECT ###### FROM ###### WHERE email =" & "'" & email & "'"
'the email variable at this point has no value
Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)
Dim reader As MySqlDataReader = cmd.ExecuteReader()
While reader.Read()
entID = reader.GetString(0)
End While
reader.Close()
Dim stm2 = "SELECT value FROM ###### WHERE ###### = " & entID
Dim cmd2 As MySqlCommand = New MySqlCommand(stm2, conn)
Dim reader2 As MySqlDataReader = cmd2.ExecuteReader()
Dim counter As Integer = 0
While reader2.Read() And counter < 3
Console.WriteLine(reader2.GetString(0) & "%")
counter = counter + 1
End While
reader.Close()
Catch ex As MySqlException
Finally
conn.Close()
End Try
End Sub
End Module
the error im getting….
http://hostthenpost.org/uploads/a66f3efbf2eba24cef0f4e8536111b54.png
I changed it up a bit and added all the db processes to a function and tried to just pass the email variable to the function but it is printing the email address but not sending it to the function. Here is the new code.
Option Strict Off
Imports MySql.Data.MySqlClient
Imports System.Runtime.Serialization
Module jrConnect
Sub Main(ByVal cmdArgs() As String)
Dim temp As String = "spkelly86@gmail.com"
'MsgBox("The Main procedure is starting the application.")
Dim returnValue As Integer = 0
' See if there are any arguments.
If cmdArgs.Length > 0 Then
temp = cmdArgs(0)
Console.Write(temp & " IN VB!")
DBConnect(temp)
End If
' Insert call to appropriate starting place in your code.
'MsgBox("The application is terminating.")
End Sub
Function DBConnect(ByVal email As String)
Dim cs As String = "serverinfo"
Dim conn As New MySqlConnection(cs)
Dim entID As String
Try
conn.Open()
Dim stm As String = "SELECT ****** FROM ******** WHERE email =" & "'" & email & "'"
Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)
Dim reader As MySqlDataReader = cmd.ExecuteReader()
While reader.Read()
entID = reader.GetString(0)
End While
reader.Close()
Dim stm2 = "SELECT value FROM ****** WHERE ****** = " & entID
Dim cmd2 As MySqlCommand = New MySqlCommand(stm2, conn)
Dim reader2 As MySqlDataReader = cmd2.ExecuteReader()
Dim counter As Integer = 0
While reader2.Read() And counter < 3
Console.WriteLine(reader2.GetString(0) & "%")
counter = counter + 1
End While
reader.Close()
Catch ex As MySqlException
Finally
conn.Close()
End Try
Return 0
End Function
End Module
You don’t exit the loop when the variable is set.
Try this:
or the equivalent: