private void buttonFicheiro_Click(object sender, EventArgs e)
 {
     FTPClass ligar = new FTPClass();
     string caminho = ligar.getfolderfile("para a", "Página inicial");
     string nome_arquivo = System.IO.Path.GetFileName(caminho);
     textBoxFich.Text = caminho;
     pictureBox1.ImageLocation = caminho;
 }
 private void buttonInserir_Click(object sender, EventArgs e)
 {
     FTPClass ftp = new FTPClass();
     DBConnect NewcConnection = new DBConnect();
     NewcConnection.dbConnection();
     MySqlCommand verifica = new MySqlCommand("SELECT * FROM Inicio", DBConnect.db);
     MySqlDataReader read = verifica.ExecuteReader();
     int count = 0;
     string old_file="";
     while (read.Read())
     {
         old_file = read["mainimage"].ToString();                    
         count = count + 1;
     }
     DBConnect.db.Close();
     string nome_arquivo = System.IO.Path.GetFileName(textBoxFich.Text);
     string aux_arquivo = directoria + nome_arquivo;
     if (textBoxFich.TextLength != 0 && textBoxPagIni.TextLength != 0 && textBoxTitulo.TextLength !=0)
     {
         if (count == 0)
         {
             NewcConnection = new DBConnect();
             NewcConnection.dbConnection();
             MySqlCommand inserir = new MySqlCommand("INSERT INTO Inicio (maintitle, mainbody, mainimage) Values ('" + this.textBoxTitulo.Text + "','" + this.textBoxPagIni.Text + "','" + aux_arquivo + "')", DBConnect.db);
             inserir.ExecuteNonQuery();
             ftp.upload(textBoxFich.Text, ftp_url, user, pwd, nome_arquivo, directoria); 
             fillgridIni();
             MessageBox.Show("Sucesso!!");
          }
          else
          {
             try
             {   
                 NewcConnection = new DBConnect();
                 NewcConnection.dbConnection();
                 if (textBoxFich.Text == Http + old_file || textBoxFich.TextLength == 0)
                 {
                     MessageBox.Show("true");
                     MySqlCommand querysql = new MySqlCommand(" UPDATE Inicio set maintitle = @Titulo, mainbody = @Texto where id_Inicio = @Id", DBConnect.db);
                     querysql.Parameters.AddWithValue("@Id", id_ini);
                     querysql.Parameters.AddWithValue("@Titulo", this.textBoxTitulo.Text);
                     querysql.Parameters.AddWithValue("@Texto", this.textBoxPagIni.Text);                      
                     querysql.ExecuteNonQuery();
                 }
                 else
                 {
                     MessageBox.Show("false");
                     MySqlCommand querysql = new MySqlCommand(" UPDATE Inicio set maintitle = @Titulo, mainbody = @Texto, mainimage = @Image where id_Inicio = @Id", DBConnect.db);
                     querysql.Parameters.AddWithValue("@Id", id_ini);
                     querysql.Parameters.AddWithValue("@Titulo", this.textBoxTitulo.Text);
                     querysql.Parameters.AddWithValue("@Texto", this.textBoxPagIni.Text);
                     querysql.Parameters.AddWithValue("@Image", aux_arquivo);
                     querysql.ExecuteNonQuery();
                     if (old_file != null && old_file != "")
                     {
                         ftp.apagar_ficheiro(old_file, ftp_url, user, pwd, "");
                     }
                     ftp.upload(textBoxFich.Text, ftp_url, user, pwd, nome_arquivo, directoria);
                 
                 }
                 
                 DBConnect.db.Close();
                     
                 fillgridIni();
                 MessageBox.Show("Alteração Gravada!!");
                 }
                 catch (Exception ex)
                 {
                     //MessageBox.Show(ex.ToString());
                 }                       
             }
         }
         else
         {
             MessageBox.Show("Falta preencher campos");
         }
     }