cancel
Showing results for 
Search instead for 
Did you mean: 

Uploading files to SAP via SDK error

Former Member
0 Kudos

Hi,

I am using SAP B1 8.8.

I have a error when i trying upload files to SAP via SDK. When i trying to upload file new.pdf  i got  error message : Source file does not exist  , 'C:\old.pdf' . old.pdf this file that i was added before.

This a code that i am using for uploading file :

public string uploadToSap(string file, int docEntry,string tablename)

        {

            string error = null;

            try

            {

                SAPbobsCOM.Attachments2 att;

                SAPbobsCOM.Recordset RS;

                if (oCompany.Connected == false)

                    oCompany.Connect();

                att = (SAPbobsCOM.Attachments2)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oAttachments2);

                RS = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

                RS.DoQuery("select [AtcEntry] from " + tablename + " where [DocEntry] = '" + docEntry + "'");

                int key = Convert.ToInt32(RS.Fields.Item(0).Value);

                if (key != 0)

                {

                    if (att.GetByKey(key) == false)//getting Attachments2 object condition

                    {

                        throw new Exception("can't upload file (cant't get by key Attachments2 object)");

                    }

                    att.Lines.Add();

                }

                att.Lines.FileExtension = Path.GetExtension( file).Replace(".", "");

                att.Lines.FileName = Path.GetFileNameWithoutExtension( file);

                att.Lines.SourcePath = Path.GetDirectoryName( file);

                att.Lines.Override = BoYesNoEnum.tYES;

                int err;

                if (key != 0)

                    err = att.Update();

                else err = att.Add();

                if (err == 0 && key == 0)

                {

                    string mkey = "";

                    int absoluteEntry;

                    ((ICompany)oCompany).GetNewObjectCode(out mkey);

                    absoluteEntry = Int32.Parse(mkey);

                    sapcon.ExNONQuery("UPDATE " + tablename + " SET [AtcEntry] = " + mkey + " WHERE [DocEntry] = '" + docEntry + "'", constr);

                }

                else if (err != 0)

                {

                    log.writeToLog("Upload to SAP error Adding / Updating Attachemts2 tbl. ");

                    error = getLastError();

                }

            }

            catch (Exception Ex)

            {

                log.writeError("Upload to SAP exeption: " + Ex.Message);

                throw;

            }

            return error;

     }

Hope for your help.

Thanks

Accepted Solutions (0)

Answers (1)

Answers (1)

Johan_H
Active Contributor
0 Kudos

Hi Pavel,

This may be a rights issue. Could you please try to upload your file from your My Documents folder, instead of directly from C:\ ?

Regards,

Johan

Former Member
0 Kudos

Hi Johan,

I uploaded different files from different folders , I getting this error once in a hundred files.

Johan_H
Active Contributor
0 Kudos

Hi Pavel,

How are the files created? What I mean is, is it possible that the process that creates or downloads the file, is not done yet at the moment that your code tries to upload the file ?

Or is it possible that the file is in use by another process ? Could you test writing a repeater in your code ? In pseudo code:

1. Upload the file

2. On error wait half a second, and then go to 1

In step 2 you could build in a separate check that the file really does exist.

Regards,

Johan

Former Member
0 Kudos

Hi Johan,

Before a trying to upload file to sap i call this function(see below) that check if file exists and she  trying to copy file into sap attachment folder , if this function is finished without any exception i call uploadToSap function

private void fileCopyTest(List<string> files)

        {

            try

            {

                foreach (string file in files)// try to copy file

                {

                    System.IO.File.Copy(file, filePathInSap + Path.GetFileName(file));

                    if (!System.IO.File.Exists(filePathInSap + Path.GetFileName(file)))

                        throw new Exception("File after copy not exist");

                }

                foreach (string file in files)// delete temp files

                {

                    System.IO.File.Delete(filePathInSap + Path.GetFileName(file));

                    if (System.IO.File.Exists(filePathInSap + Path.GetFileName(file)))

                        throw new Exception("File after delete exist");

                }

            }

            catch (Exception Ex)

            {

                log.writeError("fileCopyTest exeption: " + Ex.Message);

                throw;

            }

        }

Johan_H
Active Contributor
0 Kudos

Hi Pavel,

System,.IO.File has a tendency to hold on to a file at Windows level, even after a method is done.

Could you please test using System.IO.FileInfo to check that a file exists ?

Regards,

Johan

Former Member
0 Kudos

Hi Johan,

Thanks ,i will try it .