Solved

Problem to save picture file

Posted on 2013-11-27
6
695 Views
Last Modified: 2013-11-28
Hi,
when saving the picture file to Access DB, like

                    fl_nm = "c:\\inetpub\\VS2012\\App17\\" + lb_ph_file2.Text;
                    cmd = new OleDbCommand("update ite_tab set photo_file=@image where ite_abbr=@abbr", conn);

                    cmd.Parameters.Add("@abbr", OleDbType.VarChar).Value = tb_iteabbr.Text.Trim();
                    cmd.Parameters.Add("@image", OleDbType.Binary).Value = ImageToStream(fl_nm);
                    if (cmd.ExecuteNonQuery() >= 1) 
                    ...

        private byte[] ImageToStream(string fileName)
        {
            Bitmap image = new Bitmap(fileName);
            MemoryStream stream = new MemoryStream();
            image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            return stream.ToArray();
        }

Open in new window


I get this exception. why?
An UPDATE or DELETE query cannot contain a multi-valued field.Microsoft Access Database Engine
0
Comment
Question by:HuaMinChen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 21
ID: 39682782
AFAIK, you can use an append query to add an file to a field that ius a attachment data type.

Here is an VBA example using recordsets:

On Error GoTo Err_AddImage

Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2

Set db = CurrentDb
Set rsParent = Me.Recordset

rsParent.Edit

' open a second recordset on the multi-value (attachment) field
Set rsChild = rsParent.Fields("AttachmentTest").Value

rsChild.AddNew
rsChild.Fields("FileData").LoadFromFile ("c:\Sunset.jpg")

rsChild.Update
rsParent.Update

Exit_AddImage:

Set rsChild = Nothing
Set rsParent = Nothing
Exit Sub

Err_AddImage:

If Err = 3820 Then
MsgBox ("File already part of the multi-valued field!")
Resume Next

Else
MsgBox "Some Other Error occured!", Err.Number, Err.Description
Resume Exit_AddImage

End If

Open in new window

0
 
LVL 25

Expert Comment

by:chaau
ID: 39682788
I think you need to use OleDbType.LongVarBinary, like this:
QueryParameter par = new OleDbParameter("@image", OleDbType.LongVarBinary,
PictureByteArray.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current,
ImageToStream(fl_nm));
cmd.Parameters.Add(par);

Open in new window

0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39682797
Sorry Chaau, I then get these 2 errors
Error      3      The type or namespace name 'QueryParameter' could not be found (are you missing a using directive or an assembly reference?)      C:\App17\App17\Default.aspx.cs      668      21      App17
Error      4      The name 'PictureByteArray' does not exist in the current context      C:\App17\App17\Default.aspx.cs      669      25      App17

due to this line
                    QueryParameter par = new OleDbParameter("@image", OleDbType.LongVarBinary,
                        PictureByteArray.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current,
                        ImageToStream(fl_nm));

Open in new window

0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 25

Expert Comment

by:chaau
ID: 39682805
sorry, I meant this:
OleDbParameter par = new OleDbParameter....

Open in new window

Actually, change the code like this:
byte[] PictureByteArray = ImageToStream(fl_nm);
OleDbParameter par = new OleDbParameter("@image", OleDbType.LongVarBinary,
PictureByteArray.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current,
PictureByteArray);
cmd.Parameters.Add(par);

Open in new window

0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39682813
Sorry, I still get the same error
An UPDATE or DELETE query cannot contain a multi-valued field.Microsoft Access Database Engine

due to these
                    fl_nm = "c:\\inetpub\\VS2012\\App17\\" + lb_ph_file2.Text;
                    cmd = new OleDbCommand("update ite_tab set photo_file=@image where ite_abbr=@abbr", conn);

                    cmd.Parameters.Add("@abbr", OleDbType.VarChar).Value = tb_iteabbr.Text.Trim();
                    byte[] PictureByteArray = ImageToStream(fl_nm);
                    OleDbParameter par = new OleDbParameter("@image", OleDbType.LongVarBinary,
                    PictureByteArray.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current,
                    PictureByteArray);
                    cmd.Parameters.Add(par);
                    if (cmd.ExecuteNonQuery() >= 1) 
                    ...

Open in new window

0
 
LVL 21

Accepted Solution

by:
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 145 total points
ID: 39683857
The attachment data type is a type of sub recordset. Even within Access, to add image with code  you must use DAO not an update query.


From a fellow Microsoft VB MVP:

I don't know whether you can insert into an Access Attachment column using ADO.NET. The only solution that I am aware of requires DAO (Project...Add Reference...COM tab...Microsoft DAO 3.6 Object Library).

How to: Work With Attachments In DAO
at http://msdn.microsoft.com/en-us/library/bb258184%28office.12%29.aspx

Paul ~~~~ Microsoft MVP (Visual Basic)



Boyd Trimmell, Microsoft Access MVP
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question