Solved

Problem to save picture file

Posted on 2013-11-27
6
678 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
  • 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 24

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 24

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now