MySqlConnection open object reference not set to an instance of an object
Posted: Fri Aug 07, 2020 12:37 pm
This sounds simple but I've been working on this for hours without a result yet.
From both C# and X# web programs we open MySQL databases residing on the same server as from where the webprogram runs. That works. Now we want to approach the MySQL database from a local Pc or server. That requires the server to be defined as an IP address and a port to be added, documented in the hosters "How to connect to a MySQL database externally" documentation.
We have the following X# program for that:
Method RunSQLCommand(cCommand As String , cConnect As String) As String
Local oSQLconn As MySqlConnection
Local cReturn As String
Local oSQLcmd As MySqlCommand
// e.g. cConnect = "server=123.456.78.9;database=somedb;username=somenname;password=somepw;port=9876"
// cCommand is the query to execute
oSQLconn := MySqlConnection{cConnect}
oSQLcmd:=MySqlCommand{cCommand,oSQLconn}
oSQLconn:Open()
I just show the first 3 lines, because I can see that the instantiation of MySqlConnection has worked, and in the 2nd line oSQLcmd looked good as well; when inspecting the variables within oSQLcmd I can see the connection details, I can see the query, all looks well. But as soon as I run the 3rd line, oSQLconn:Open() , I get an exception object reference not set to an instance of an object . Despite that oSQLconn has been instantiated 2 lines earlier!
Somewhere I read that this error could occur when the connection string is incorrect. In the meantime I checked/tried the following:
1 Tried it in C#. Same error
2 Entered the exact same connection data in MySQLWorkbench. It connected without problems so that would mean that I can indeed approach the database externally with the credentials I use. So why doesn't it work from my program??
3 Added pooling=false;connection reset=false to the connection string. Same error.
4 Removed the database from the connection string and changed the query to UPDATE MyDatabase.Tablename set ... where ... Same error
5 Used the MySqlConnectionStringBuilder (see C# code below). It resulted in the same connection string I already passed, so same error.
6 Replaced the keywords username and passwords with Uid and Pwd. Same resulting connectionstring (as I can see in the debugger) but same error.
7 Tried the connection string we use (working) in the web programs. Then the Open command does not directly give the "object reference" error but "Unable to connect to any of the specified MySQL hosts."
I am currently out of options. The exception details does not learn me anything, mostly null values.
Has anybody approached MySQL databases externally? If so, what should I do differently? Is there any explanation for this error? What else can I try to get this to work?
Dick
C# to construct the connection string
oConStr.Server = "123.456.78.9;
oConStr.UserID = "someuser";
oConStr.Password = "somepw";
oConStr.Database = "somedb";
oConStr.Port = 9876;
cConnect = oConStr.ToString();
From both C# and X# web programs we open MySQL databases residing on the same server as from where the webprogram runs. That works. Now we want to approach the MySQL database from a local Pc or server. That requires the server to be defined as an IP address and a port to be added, documented in the hosters "How to connect to a MySQL database externally" documentation.
We have the following X# program for that:
Method RunSQLCommand(cCommand As String , cConnect As String) As String
Local oSQLconn As MySqlConnection
Local cReturn As String
Local oSQLcmd As MySqlCommand
// e.g. cConnect = "server=123.456.78.9;database=somedb;username=somenname;password=somepw;port=9876"
// cCommand is the query to execute
oSQLconn := MySqlConnection{cConnect}
oSQLcmd:=MySqlCommand{cCommand,oSQLconn}
oSQLconn:Open()
I just show the first 3 lines, because I can see that the instantiation of MySqlConnection has worked, and in the 2nd line oSQLcmd looked good as well; when inspecting the variables within oSQLcmd I can see the connection details, I can see the query, all looks well. But as soon as I run the 3rd line, oSQLconn:Open() , I get an exception object reference not set to an instance of an object . Despite that oSQLconn has been instantiated 2 lines earlier!
Somewhere I read that this error could occur when the connection string is incorrect. In the meantime I checked/tried the following:
1 Tried it in C#. Same error
2 Entered the exact same connection data in MySQLWorkbench. It connected without problems so that would mean that I can indeed approach the database externally with the credentials I use. So why doesn't it work from my program??
3 Added pooling=false;connection reset=false to the connection string. Same error.
4 Removed the database from the connection string and changed the query to UPDATE MyDatabase.Tablename set ... where ... Same error
5 Used the MySqlConnectionStringBuilder (see C# code below). It resulted in the same connection string I already passed, so same error.
6 Replaced the keywords username and passwords with Uid and Pwd. Same resulting connectionstring (as I can see in the debugger) but same error.
7 Tried the connection string we use (working) in the web programs. Then the Open command does not directly give the "object reference" error but "Unable to connect to any of the specified MySQL hosts."
I am currently out of options. The exception details does not learn me anything, mostly null values.
Has anybody approached MySQL databases externally? If so, what should I do differently? Is there any explanation for this error? What else can I try to get this to work?
Dick
C# to construct the connection string
oConStr.Server = "123.456.78.9;
oConStr.UserID = "someuser";
oConStr.Password = "somepw";
oConStr.Database = "somedb";
oConStr.Port = 9876;
cConnect = oConStr.ToString();