Click here to Skip to main content
15,888,270 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
All I want to do is add pictures to my database as OLE objects from my Asp.net page via C# and yet be able to open it (the picture) when working in Access itself…

I found code on how to enter it into the database and according to Access the picture has been added to the table, but I can’t open it from within Access itself, saying that I need to restart my OLE Server. (I've searched multiple times on how to restart it, but nothing works)

The example’s database has 3 columns: ‘Imagename”, “Imagesize” and “Imagedata”.
“Imagedata” is an OLEDB-type while the rest is a “Text”-type.

(Ps. I’m using C# 2012 and Access 2010)

The example looks like this:

What I have tried:

ASP.NET side:

ASP.NET
<form runat="server">
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <asp:Button ID="btnSaveImage" runat="server" Text="Save in MS Access" OnClick="btnSaveImage_Click" />
    <br />
    <br />
    <asp:Label ID="lblMessage" runat="server"></asp:Label>
   
    </form>

The C# side

C#
protected void btnSaveImage_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string name = FileUpload1.PostedFile.FileName;
            int length = FileUpload1.PostedFile.ContentLength;

            byte[] imageBytes = new byte[length];
            Stream imageStream = FileUpload1.PostedFile.InputStream;
            imageStream.Read(imageBytes, 0, length);

            string connString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
            OleDbConnection connection = new OleDbConnection(connString);

            string insertQuery = "INSERT INTO Images(ImageData) VALUES(@ImageName, @ImageSize, @ImageData)";
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = insertQuery;
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@ImageName", name);
            command.Parameters.AddWithValue("@ImageSize", length);
            command.Parameters.AddWithValue("@ImageData", imageBytes);

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
                lblMessage.Text = "Image data saved successfully";
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Unable to save image data";
            }
            finally
            {
                connection.Close();
            }
        }
    }

And the web.config side:

C#
<connectionStrings>
		<add name="ConnectionString2" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\Anja\Documents\sitprentein.accdb'"/>
	</connectionStrings>



Even though the code works and embeds the images into my database, I can’t open it from within Access. Images that were embedded directly from within Access display the word “Package” in the cell, whereas the images that were inserted via C# display “long binary data” When I double-click on the “Package” in the file opens and displays my images, but when clicking on , “long binary data” I’m told to restart the OLE Server from outside Access.

I’ve searched the internet but the answers I found doesn’t work.
Is there a way that I can insert my images in my database (via C#) in such a way that I will still be able to open them from within Access? And if there is, can someone please be so kind as to help me adjusting the code?

Not wanting to lose hope...
1step-foward-2steps-back
Posted
Updated 24-Sep-21 10:31am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900