1

I've got the following code where i'm basically taking data from mysql and plonking in SQL server.

The SQL server part works fine, but I can't work out how to keep the mysql connection open to do the update. When it does the mysql execute reader in the foreach loop it bums out with the error:

An unhandled exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll

Additional information: Connection must be valid and open.

Code:

 DB db = new DB();
            String sConfig_hostname = 
            String sConfig_dbname = 
            String sConfig_dbusername = 
            String sConfig_dbpassword = 
            string MyConString = "SERVER=" + sConfig_hostname + ";" +
                "DATABASE=" + sConfig_dbname + ";" +
                "UID=" + sConfig_dbusername + ";" +
                "PASSWORD=" + sConfig_dbpassword + ";Allow Zero Datetime=true;";
            MySqlConnection connection = new MySqlConnection(MyConString);
             string sQuery="Select * from inbox where Transferred = 0";

             MySqlDataAdapter myDA = new MySqlDataAdapter(sQuery, connection);
             MySqlCommandBuilder cmb=new MySqlCommandBuilder(myDA);

             DataTable MyDT = new DataTable();
             myDA.Fill(MyDT);

             foreach (DataRow row in MyDT.Rows)
             {
                String SQL = String.Format("Insert into Inbox (Message, Received, Sender) VALUES ('{0}', '{1}', '{2}')", GeneralFunctions.SQLescape(row["TextDecoded"].ToString()), row["ReceivingDateTime"].ToString(), row["SenderNumber"].ToString());
                String mySQL = "Update inbox set Transferred = 1 where ID = " + row["ID"].ToString();
                db.Update(SQL);

                MySqlCommand SQLup = new MySqlCommand(mySQL);
                MySqlDataReader reader = SQLup.ExecuteReader();

             }

2 Answers 2

2
MySqlConnection connection = new MySqlConnection(MyConString);
connection.open(); // insert this line

for more information

Sign up to request clarification or add additional context in comments.

Comments

1

The reason it's failing is because you don't want to execute a reader here, you just want to execute the statement and because you're not initializing the command with the connection:

MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();

...

MySqlCommand SQLup = new MySqlCommand(mySQL, connection);
SQLup.ExecuteNonQuery();

3 Comments

Ah - That makes sense. I've changed that, but unfortunately the same error: Additional information: Connection must be valid and open. connection isnt null at this point and it's the write information
@Tom you could move that line, and probably should move that line, to right below the construction of the MySqlConnection.
Ah perfect. THank you for your help. Will mark this as answer when the time limit allows me to!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.