title: How to insert data into MySQL database using Asp.net
body: <p>I am trying to insert data into MYSQL database using Asp.Net but I am getting the following error message: Failed to connect to the database due to System.InvalidOperationsException.The connection must be valid and open.
  Here is what I am doing: </p>
  
  <pre><code>protected void SU_Button_Click(object sender, EventArgs e)
          {
              string connectionString = @"Data Source=mno; Database=xyz; User ID=abc; Password=abc";
              using (MySqlConnection connection = new MySqlConnection(connectionString))
              {
                  try
                  {
                      string insertData = "insert into signup_table(firstname,surname,mobile_number,email_address,password," +
                                          "confirm_password) values (@F_Name, @S_Name, @M_Number, @E_Address, @Password, @C_Password)";
                      MySqlCommand command = new MySqlCommand(insertData, connection);
  
                      command.Parameters.AddWithValue("@F_Name",FN_TextBox.Text);
                      command.Parameters.AddWithValue("@S_Name", SN_TextBox.Text);
                      command.Parameters.AddWithValue("@M_Number", MN_TextBox.Text);
                      command.Parameters.AddWithValue("@E_Address", EA_TextBox.Text);
                      command.Parameters.AddWithValue("@Password", P_TextBox.Text);
                      command.Parameters.AddWithValue("@C_Password", CP_TextBox.Text);
                      int result = command.ExecuteNonQuery();
                      connection.Open();
                      MessageBox.Show("Connected to database");
                      MessageBox.Show("Data inserted successfully");
                  }
                  catch(Exception ex)
                  {
                      MessageBox.Show("Failed to connect to database due to" + ex.ToString());
                      MessageBox.Show("Failed to insert data due to" + ex.ToString());
                  }
  
              }
          }
  </code></pre>
  
  <p>Please suggest something. Thanks in advance... :) </p>