Prince Jea
Prince Jea

Reputation: 5680

Errors saving images to database

I have a OpenFileDialog which will suppose to select photo and save it to database but the problem is when I access the class when the dialog result of the openFileDialog is OK it says that no such table : PhotoFile when the SavePhoto function is called with following arguments:

TODO(P J):put values here

This is what I've tried so far

OpenFileDialog d = new OpenFileDialog();

        d.Filter = ("JPEG Imange (*.jpg|*.jpg|PNG Image (*.png)|All Files*.*");
        if ((d.ShowDialog()) == DialogResult.OK)
        {
           SavePhoto(txtID.text,d.fileName);
        }

Here's the code for the function

        try {
        using (SQLite.SQLiteConnection SQLConnect = new SQLite.SQLiteConnection(g_constring)) {
            byte[] photo = FileImageToByte(PhotoFile);
            SQLConnect.Open();
            if (SQLConnect.State == ConnectionState.Open) {
                SQLiteCommand SQLcommand = new SQLiteCommand(SQLConnect);
                SQLcommand = SQLConnect.CreateCommand;
                SQLcommand.CommandText = "DELETE FROM PhotoFile WHERE PhotoID = '" + PhotoId + "'";
                SQLcommand.ExecuteNonQuery();
                SQLcommand.Parameters.Clear();

                SQLcommand.CommandText = "INSERT INTO PhotoFile(PhotoID, Photo) VALUES(@EmployeeID, @Photo1)";

                SQLiteParameter SQLparmID = new SQLiteParameter("@EmployeeID", PhotoId);
                SQLparmID.DbType = DbType.String;
                SQLparmID.Value = PhotoId;
                SQLcommand.Parameters.Add(SQLparmID);

                SQLiteParameter SQLparm = new SQLiteParameter("@Photo1", photo);
                SQLparm.DbType = DbType.Binary;
                SQLparm.Value = photo;
                SQLcommand.Parameters.Add(SQLparm);

                SQLcommand.ExecuteNonQuery();

                bReturn = true;
            } else {
                bReturn = false;
            }
        }
    } catch (System.Exception eX) {
        MessageBox.Show(eX.Message.ToString(), "Error in database", MessageBoxButtons.OK, MessageBoxIcon.Error);
        bReturn = false;
    }
    return bReturn;
}

PhotoFile table exist in my database in fact I've tried a Windows form and trigger the function if the dialog result = ok but when I used the openFileDialog it always produce the error as stated above.

Upvotes: 0

Views: 163

Answers (2)

Alex
Alex

Reputation: 23300

Your AppMain.ConnectionString is pointing at the wrong database, which does not contain any PhotoTable table. You might want to double check it.

Your code deletes the record matching the provided PhotoID by the way, it's not saving anything to the database.

Upvotes: 1

Grant Thomas
Grant Thomas

Reputation: 45068

This isn't anything to do with the OpenFileDialog per se, it's your SQL query failing and stating that the table doesn't exist (Photofile) - so I would suggest that it doesn't and you should either check your table name, or create it if necessary.

Other than that, there is concern over your query: your method indicates that a photo will be saved yet you use a DELETE. Also, if the table did exist/when you manage to sort the table name correctly, I'd suggest you don't use strings as identifiers. No saving going on anywhere.

Upvotes: 5

Related Questions