0

VS 2017 user here. I'm trying to connect a C# console app to MySQL 5.7. However there is no connection established. A suggeste solution is to look at undeclared objects, but I do not think that is the case here. Pls advise if it is.

MySqlConnection conn;
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

public Connection()
{
    conn = new MySqlConnection(connStr);
}

<connectionStrings>
<add name="myConnectionString"  
  connectionString="server=localhostdatabase=store;user=root;
  password=pass;port=3306" />
</connectionStrings>

If I run this in the debugger I get the follow information on conn:

IsPasswordExpired' threw an exception of type 'System.NullReferenceException'
ServerThread' threw an exception of type 'System.NullReferenceException'
ServerVersion' threw an exception of type 'System.NullReferenceException'

There is very little to be found on this, just that the password was set one year ago (I do not really believe that is causing this problem) and some guy that solved it by reinstalling MySQL server. I did that, to no avail.

From the command line everything is working fine.

ADBF
  • 93
  • 1
  • 15
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Progman Sep 28 '17 at 21:42
  • sorry, the second public Connection() shouldnt be there – ADBF Sep 28 '17 at 21:42
  • 1
    thanks progman for the reference, but the cause stated there are generally objects not declared before used, I do not believe that is the case here. – ADBF Sep 28 '17 at 21:48
  • 1
    It's possible the connection string is invalid. It appears to be missing a semi-colon between the server name and database `connectionString="server=localhost;database=store;user=root; password=pass;port=3306"` – JConstantine Sep 28 '17 at 21:52
  • The code is not following best practice anyway: the connection should be instantiated each time it is going to be used and then disposed of immediately afterwards. The [`using`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement) construct is useful for that. – Andrew Morton Sep 28 '17 at 21:56
  • LLevet, youre right about the connstr, but I just made an error in pasting it this question, its good in my code. apologies. – ADBF Sep 29 '17 at 05:54
  • @ADBF Please edit your question so it does contain the whole code and the whole error message you get. However https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it might still be relevant since it is a NullReferenceException after all. – Progman Sep 29 '17 at 16:42
  • I feel deeply ashamed. Please kill me. I forgot conn.Open() after initiating the connection. Sorry I wasted your time. – ADBF Sep 29 '17 at 19:26

1 Answers1

1

If you are confirmed that your password expires, you can try to create or alter:

CREATE USER 'jeffrey'@'localhost' PASSWORD EXPIRE NEVER;
ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE NEVER;

Your Guide is here.

Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
  • thanks reds for taking the time to answer me. Pls see my comment above. Will crawl back under my stone now. – ADBF Sep 29 '17 at 19:31