So here's the thing, I have two sets of Functions that's supposed to work similarly, but the second one uses structures. I'm trying to set up my mysql connection using structures. The first one works, it uses public variables declared in another module. The 2nd one does not, it uses a structure declared public in another module. It always gives me the NullReferenceException when setting the ConnectionString property.
First (Working)
Public Function CheckConnection(server As String, port As String, username As String, password As String) As Boolean
Try
sqlConnection.ConnectionString = BuildConnectionString(server, port, username, password)
sqlConnection.Open()
Return True
Catch ex As Exception
Return False
Finally
If Not sqlDataReader Is Nothing Then sqlDataReader.Close()
If sqlConnection.State = ConnectionState.Open Then sqlConnection.Close()
End Try
End Function
Second (Not Working)
Public Function CheckConnection(server As String, port As String, username As String, password As String) As Boolean
Try
Dim structQuery As New MyQueryStructure
With structQuery
.Server = server
.Port = port
.Username = username
.Password = password
.Connection.ConnectionString = BuildConnectionString(.Server, .Port, .Username, .Password)
End With
structQuery.Connection.Open()
Return True
Catch ex As Exception
GenerateErrorReport(ex)
Return False
End Try
End Function
Additional codes & variables
Public Function BuildConnectionString(stringHost As String, stringPort As String, stringUsername As String, stringPassword As String)
Return "Server=" & stringHost & ";Port=" & stringPort & ";Uid=" & stringUsername & ";Pwd=" & stringPassword & ";Allow User Variables=True;Persist Security Info=true;SslMode=none;"
End Function
Public sqlConnection As New MySqlConnection
Public sqlQuery As String
Public sqlCommand As New MySqlCommand
Public sqlDataReader As MySqlDataReader
Public sqlDataAdapter As New MySqlDataAdapter
Public sqlDataset As New DataSet
Public sqlDataTable As New DataTable
Public sqlServer, sqlPort, sqlUsername, sqlPassword As String
Public Structure MyQueryStructure
Dim Connection As MySqlConnection
Dim Command As MySqlCommand
Dim DataReader As MySqlDataReader
Dim DataAdapter As MySqlDataAdapter
Dim DataSet As DataSet
Dim DataTablee As DataTable
Dim Line As String
Dim Server As String
Dim Port As String
Dim Username As String
Dim Password As String
End Structure
I can always go back to the first one. But I kind of want to learn how to use Structures. Also, I need my connection string to be changeable during runtime.