MySQl in .NET ERROR

AstrixGame

New member
Joined
Aug 11, 2021
Messages
4
Programming Experience
5-10
Hi I am creating a project in .net and I am getting data from mysql but mysql connection is reporting the error "System.InvalidOperationException: Connection must be valid and open." My program:

C#:
private void Form1_Load(object sender, EventArgs e)
        {
            string command = "SELECT coins from users WHERE nick = " + textBox1.Text;
            MySqlConnection dbconn = new MySqlConnection("SERVER=sql4.freemysqlhosting.net;PORT=3306;DATABASE=sql4429796;UID=sql4429796;PWD=************;");
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(command, dbconn);
            dbconn.Open();
            MySqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                coins = dataReader["coins"].ToString();
            }
        }
        }
 
Last edited:
Moving to databases...
 
No, I was noting that I was moving your topic out of the "VS.NET General Discussions" forum and into the one of the database specific forums.
 
My first guess would be a connection string issue. Double check that Open() succeeded by checking the state of the connection object.

My next guess would be a configuration issue with your machine or your network. Some antivirus programs will deny outbound TCP connections. Some network admins will deny outbound TCP connections to specific ports.
 
Back
Top Bottom