0

i'm having an issue using C# inserting multiple rows into a MySQL database, have the following code;

//Upload to mysql
             string connStr = "server=server;user=username;database=databasae;port=3306;password=password;";
             MySqlConnection conn = new MySqlConnection(connStr);

             conn.Open();

             foreach (Channel chan in results)
             {
                 // Perform databse operations
                 try
                 {
                     //Create sql statment with parameters
                     string sql = "INSERT INTO channels(ID, Name) VALUES (@id,@name)";
                     MySqlCommand cmd = new MySqlCommand(sql, conn);
                     cmd.Parameters.AddWithValue("@id", chan.ID);
                     cmd.Parameters.AddWithValue("@name", chan.Name);
                     cmd.ExecuteNonQuery();
                     updateStatus("Inserted");

                 }


                 catch (Exception ex)
                 {
                     updateStatus(ex.Message.ToString());
                 }
                 conn.Close();

I seem to be getting "connection must be valid and open". From what i can see i'm passing the connection string correctly and i'm using ExecuteNonQuery. And idea's?

thanks

3
  • Check connection status after conn.Open(): is it ok? Commented May 9, 2011 at 9:15
  • 2
    Are you closing the connection at the end of the foreach? If so you should do that after the foreach loop not inside. Aside on this example you are also missing } to close the foreach loop. Commented May 9, 2011 at 9:16
  • It would be more efficient to create your Command and Parameter objects outside the loop. Then you can just change the value of the two parameters and call ExecuteNonQuery inside the loop. Commented May 9, 2011 at 19:05

4 Answers 4

3

conn.Close(); should be outside the foreach. The following would work :

         //Upload to mysql
         string connStr = "server=server;user=username;database=databasae;port=3306;password=password;";
         MySqlConnection conn = new MySqlConnection(connStr);

         conn.Open();

         foreach (Channel chan in results)
         {
             // Perform databse operations
             try
             {
                 //Create sql statment with parameters
                 string sql = "INSERT INTO channels(ID, Name) VALUES (@id,@name)";
                 MySqlCommand cmd = new MySqlCommand(sql, conn);
                 cmd.Parameters.AddWithValue("@id", chan.ID);
                 cmd.Parameters.AddWithValue("@name", chan.Name);
                 cmd.ExecuteNonQuery();
                 updateStatus("Inserted");

             }


             catch (Exception ex)
             {
                 updateStatus(ex.Message.ToString());
             }
          }
          conn.Close();
Sign up to request clarification or add additional context in comments.

Comments

2

Looks like the connection is inside the foreach loop. It should be outside the foreach loop.

conn.Close(); should be outside the foreach loop.

2 Comments

Silly silly mistake by me, what an idiot!!! lol I really need to learn this walk away for 15 minutes if your having an problem!!
It's ok. Happens to everyone and that's the reason we have stackoverflow ;) . Cheers !!
1

How about using

using(MySqlConnection conn = new MySqlConnection(connStr))
{
//your stuff in here
}

This is transformed into a try final block .. so should take care of your connection woes.

Comments

0

add finally block to the try catch code and put conn.close() in it.like

finally
{
     if(conn.ConnectionSTate=Connectionstate.open)
           {
               conn.close()
           }
}

Comments

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.