Hi!!! Somebody knows whats the problem????
If i remove OpenFileDialog and pass the string direct pathDiskCover = @"C:\Documents and Settings\Administrator\My Documents\someImage.jpg"; it works fine

private void toolStripButton2_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.FileName = String.Empty;
    dlg.Title = "Open Images";
    dlg.Filter = "Image Files(*.png; *.jpg; *.bmp)|*.png; *.jpg; *.bmp";
    DialogResult dr = dlg.ShowDialog();
    if (dr == DialogResult.OK)
    {
        pathDiskCover = System.IO.Path.GetFullPath(dlg.FileName) ; 
        MemoryStream ms = new MemoryStream(System.IO.File.ReadAllBytes(pathDiskCover));
        Image img = Image.FromStream(ms);
        img.Save(@"C:\output.png", ImageFormat.Png);
        img.Dispose();
    }
    dlg.Dispose();
    updateCover();
}

private void updateCover()
 {
     //pb_CoverMD.Image = new Bitmap(pathDiskCover);// this works
     byte[] MyData = null;
     FileStream fs = new FileStream(pathDiskCover, FileMode.OpenOrCreate, FileAccess.Read);
     MyData = new byte[fs.Length];
     fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
     fs.Close();

     if (idRecord > 0)
     {
         SQLiteConnection conn = new SQLiteConnection(conecxao);
         if (conn.State == ConnectionState.Closed) { conn.Open(); }
         SQLiteCommand cmd = new SQLiteCommand("UPDATE FilmsDB SET picCoverDB=@picCoverDB WHERE idFilm=@idFilm ", conn);

         cmd.Parameters.AddWithValue("idFilm", idRecord);
         cmd.Parameters.AddWithValue("picCoverDB", MyData);

         try
         {
             cmd.ExecuteNonQuery();
             loadDatabase();
             MessageBox.Show("Cover successfully updated.");
         }
         catch (Exception ex)
         {
             MessageBox.Show("Error updating cover." + ex.Message);
         }
     }
}

thank you for any replay...

Recommended Answers

All 10 Replies

At what point in the code, and what exact error message do you get?

Either close and dispose the dialog after getting the filename, or use dlg.OpenFile() to grab the FileStream

The point is, if i pass a string with a path direct to updateCover() it works and the image is saved in database. if i use OpenFileDialog to get the image path i get erro, trying to save image in database.

Ketsuekiame, sorry i dont follow...
I use dlg.Dispose(); "or use dlg.OpenFile() to grab the FileStream" i tryed filestream, same result.

what it supposed to do: Openfiledialog get image path, show image in pictureBox and save image in database.

Yeah I realise what you're trying but I think that the OpenFileDialog is opening the file, without your knowledge. In that case, what you need to do is grab the filename and then immediately dispose of the dialog. This way all the resource handles that the dialog holds will be released.

The other way is to grab the FileStream from OpenFile, however, if that is also giving you the error, I'm not sure what reasoning there is behind it.

Not working

{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.FileName = String.Empty;
    dlg.Title = "Open Images";
    dlg.Filter = "Image Files(*.png; *.jpg; *.bmp)|*.png; *.jpg; *.bmp";
    DialogResult dr = dlg.ShowDialog();
    if (dr == DialogResult.OK)
    {
        pathDiskCover = System.IO.Path.GetFullPath(dlg.FileName);
        dlg.Dispose();
    }
    dlg.Dispose();
}



private void updateCover()
{
 //outputPath.text = pathDiskCover; this output is OK "c:\someImage.jpg"

 byte[] MyData = null;
 FileStream fs = new FileStream(pathDiskCover, FileMode.OpenOrCreate, FileAccess.Read);
 MyData = new byte[fs.Length];
 fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
 fs.Close();

 if (idRecord > 0) // ID selected record is Ok
 {
     SQLiteConnection conn = new SQLiteConnection(conecxao);
     if (conn.State == ConnectionState.Closed) { conn.Open(); }
     SQLiteCommand cmd = new SQLiteCommand("UPDATE FilmsDB SET picCoverDB=@picCoverDB WHERE idFilm=@idFilm ", conn);

     cmd.Parameters.AddWithValue("idFilm", idRecord);
     cmd.Parameters.AddWithValue("picCoverDB", MyData);

     try
     {
         cmd.ExecuteNonQuery();
         loadDatabase();
         MessageBox.Show("Cover successfully updated.");
     }
     catch (Exception ex)
     {
         MessageBox.Show("Error updating cover." + ex.Message);
     }

 }
 pb_CoverMD.Image = new Bitmap(pathDiskCover); // here displays the image
} 

Error mesage:
"Error updating cover.SQLite error no such table: FilmsDB"

Now if i comment openfile dialog and
pathDiskCover = @"c:\someImage.jpg
it works....

But i need the openfile to select another image
if user wants to update.

FileStream from OpenFile like this????
FileStream fs = new FileStream(dlg.FileName, FileMode.OpenOrCreate, FileAccess.Read);

No FileStream fs = dlg.OpenFile();

Also, the OpenFileDialog has nothing to do with you missing a table from your database unless your loadDatabase method is returning the wrong error.

Hi Ketsuekiame, the database is ok the loadDatabase works fine, because if i use pathDiskCover = @"c:\someImage.jpg the bolob is updated in the database in the write position(idRecord).

private void loadDatabase()
{
SQLiteConnection conn = new SQLiteConnection(conecxao);
if (conn.State == ConnectionState.Closed) { conn.Open(); }

SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM FilmsDB", conn);
SQLiteDataReader dr = cmd.ExecuteReader();
List<movieData> movieList = new List<movieData>();

while (dr.Read())
{
movieList.Add(new movieData
{
(...)
picCoverDB = (System.Byte[]) dr["picCoverDB"],
(...)
});
}

dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = movieList;
}

// Select record and fill textboxes
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
idRecord = 0;
idRecord = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
(...)
byte[] imageBytes = null;
imageBytes = (byte[])dataGridView1.CurrentRow.Cells[10].Value;
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
pb_CoverMD.Image = Image.FromStream(ms, true);
ms.Close();
(...)
}

Sorry, but cant you post a sample for
FileStream fs = dlg.OpenFile();
i dont get it...

using(OpenFileDialog dlg = new OpenFileDialog())
{
    dlg.ShowDialog();
    if(dlg.DialogResult == DialogResult.OK)
    {
        using(Stream fs = dlg.OpenFile())
        {
            // Read data from stream like normal
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.