none
Storing Images back into a SQL Database (C#) RRS feed

  • Question

  • I had been discussing a datagridview image binding problem with Alex Liang. He suggested a fix and all seemed to work except now when I try to store a new image back into the database, I get no error and the entire record is not saved. If I do not try to add the image all the other data is saved correctly. This new problem occurs whether or not a query is active. It is my guess that the image databindings are now corrupted, though all images that were stored in the table still display correctly but no longer update.

    Reference information can be seen in the thread Datagridview throws an exception if an image is being displayed while a LINQ to SQL query is active.
    Tuesday, March 9, 2010 8:41 PM

Answers

  •         private void pbxCoverFront_MouseClick(object sender, MouseEventArgs e)
            {
                    pbxCoverFront.Load(openCoverDialog.FileName);
            }

    Hi Claude Bell,

    "pbxCoverFront.Load(openCoverDialog.FileName);" does not change the image stored in the data source, so you can not store a new image back into the database when you do the update like that.
    http://msdn.microsoft.com/en-us/library/zyc6weyd.aspx

    Try this:

    pbxCoverFront.Image = Image.FromFile(openCoverDialog.FileName);

    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Claude Bell Monday, March 15, 2010 4:13 PM
    Monday, March 15, 2010 1:31 PM
    Moderator

All replies

  • Hi Claude Bell,

    How did you change your images and how did you do the update ? Could you please provide some sample code about how you did these things?

    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, March 13, 2010 5:56 AM
    Moderator
  • Hi Alex,

    I have made no changes to my code for adding the images to the table. This update problem occured after the change I made using your suggestion of

    In the .dbml where the table lies, right click on the field -> Choose propeties --> Change Type from Binary (System.Data.Linq.Binary) To byte[] (System.Byte[])

    the code I am using for record addition is as follows:

    When a new record is to be added I use
            private void tsBtnCoverAdd_Click(object sender, EventArgs e)
            {
                    coversBindingSource.AddNew();
            }
    When I want to save the new record I use
            private void tsBtnCoverSave_Click(object sender, EventArgs e)
            {
                    coversBindingSource.EndEdit();
                    coversTableAdapter.Update(flightCoversDataSet.Covers);
            }
    When I retreive the Image to be stored I use

            private void pbxCoverFront_MouseClick(object sender, MouseEventArgs e)
            {
                    pbxCoverFront.Load(openCoverDialog.FileName);
            }

    pbxCoverFront is System.Windows.Forms.Picturebox
    the image is databound to coversBindingSource

    Regards,
    Claude Bell

    Saturday, March 13, 2010 2:43 PM
  •         private void pbxCoverFront_MouseClick(object sender, MouseEventArgs e)
            {
                    pbxCoverFront.Load(openCoverDialog.FileName);
            }

    Hi Claude Bell,

    "pbxCoverFront.Load(openCoverDialog.FileName);" does not change the image stored in the data source, so you can not store a new image back into the database when you do the update like that.
    http://msdn.microsoft.com/en-us/library/zyc6weyd.aspx

    Try this:

    pbxCoverFront.Image = Image.FromFile(openCoverDialog.FileName);

    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Claude Bell Monday, March 15, 2010 4:13 PM
    Monday, March 15, 2010 1:31 PM
    Moderator
  • Hi Alex,

    Thanks for all the help. Your last reply was the answer to my problem. The demonstration application now works as intended under Visual Studio 2010.

    I will now be able to begin work on a much more complex application for one of my customers.

    Best regards,
    Claude Bell
    Monday, March 15, 2010 4:19 PM