I dont know why i have an error that i'am not connected. 2 times function of checkBox_pokazPodglad_CheckedChanged is run good an then when second time it uses ftp connection it makes an error. There is a comentary wich line occures the error.
Here's my code :
public void wczytajNazwyZdjec(int id_galerii, string nazwaFolderuGalerii)
{
ftp = new FTPConnection();
ftp.ServerAddress = "xx.pl";
ftp.UserName = "xxxrwer";
ftp.Password = "xxx841";
ftp.TransferType = FTPTransferType.BINARY;
ftp.ConnectMode = FTPConnectMode.PASV;
ftp.ServerDirectory = PATH_Images + "//" + nazwaFolderuGalerii;
try
{
ftp.Connect();
}
catch (FTPException ex)
{
MessageBox.Show("Wystąpił błąd podczas połączenia z serwerem FTP. \n "
+ "Błąd : " + ex.Message, "Wczytywanie nazw zdjęć.");
}
connection = new MySqlConnection(connString.ToString());
try
{
connection.Open();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString());
}
cmd = new MySqlCommand();
cmd.Connection = connection;
cmd.CommandText = "select count(nazwa) from obrazy where id_gal=?id";
cmd.Parameters.Add(new MySqlParameter("?id", MySqlDbType.Int32));
cmd.Parameters["?id"].Value = id_galerii;
reader = cmd.ExecuteReader();
reader.Read();
filesFtp = new string[reader.GetInt32(0)];
reader.Close();
cmd.CommandText = "select nazwa from obrazy where id_gal=?id";
// cmd.Parameters.Add(new MySqlParameter("?id", MySqlDbType.Int32));
// cmd.Parameters["?id"].Value = id_galerii;
reader = cmd.ExecuteReader();
int i=0;
while (reader.Read())
{
filesFtp[i] = reader["nazwa"].ToString();
i++;
}
reader.Close();
connection.Close();
listBox1.Items.Clear();
for (i = 0; i < filesFtp.Length; i++)
{
string x = i + ": " + filesFtp[i].ToString();
listBox1.Items.Add(x.ToString());
}
if (filesFtp.Length == 0)
{
MessageBox.Show("Brak zdjęc w galerii.");
}
else
{
idMinZdjecia = 0;
idMaxZdjecia = filesFtp.Length - 1;
idAktywnegoZdjecia = idMinZdjecia;
tb_nazwaZdjecia.Text = filesFtp[idMinZdjecia];
tb_nazwaMini.Text = "m" + filesFtp[idMinZdjecia];
if (checkBox_pokazPodglad.Checked == true)
{
string tempDir;
try
{
tempDir = Directory.GetCurrentDirectory() + @"\temp";
Directory.CreateDirectory(tempDir);
}
catch (IOException e)
{
MessageBox.Show("Error : " + e.ToString(),
"Blad tworzenia folderu tymczasowego", MessageBoxButtons.OK);
return;
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ftp.DownloadFile(tempDir, "m"+filesFtp[idMinZdjecia]);
//Here is an error when the checkbox state is changed
and it says that i am not connected...
pb_obrazy.Image = Image.FromFile(tempDir + @"\m" + filesFtp[idMinZdjecia]);
}
}
ftp.Close();
}
//error is at second use of wczytajNazwyZdjec Method
private void checkBox_pokazPodglad_CheckedChanged(object sender, EventArgs e)
{
//if galery of images is selected from the galery_listbox
if (czyWybranaGaleria == true)
{
if (checkBox_pokazPodglad.Checked == true)
//takes from mysql names of files and from ftp downloads the
//image and puts in a picture box
wczytajNazwyZdjec(idAktywnejGalerii, nazwaFolderuAktywnejGalerii);
else
pb_obrazy.Image = null;
}
}
Please give me an answer :)