How to copy a pdf file with the amount of copies in the database as the value using C#?

86 Views Asked by At

I need to get the amount of copies from my local database and use that value to make copies of the pdf file.

         public Boolean getAmountOfCopies()
         {
             string connectionString = null;
             MySqlConnection cnn;
             connectionString = "databaseInformation";
             cnn = new MySqlConnection(connectionString);
             try
             {
                 cnn.Open();
                 Console.WriteLine("Connection Open");
                 const string sql = "SELECT numberofcopies FROM orders";
                 MySqlCommand cmd = new MySqlCommand(sql, cnn);
                 MySqlDataReader rdr = cmd.ExecuteReader();
        
                 while (rdr.Read())
                 {
                     if (rdr[0] == null)
                     {
                         Console.WriteLine("There is no value, it is null.");
                         return false;
                     }
                     else
                     {
                         Console.WriteLine(rdr[0]);
                         var ImageFiles = Directory.EnumerateFiles(Directory.GetCurrentDirectory()).Where(f => f.EndsWith(".jpg") || f.EndsWith(".JPG") || f.EndsWith(".jpeg") || f.EndsWith(".jfif"));
                         List<string> images = ImageFiles.ToList();
                         PdfDocument[] copyOfPDF = new PdfDocument[images.Count()];

                         // Code goes here to make the copy
                     }
                 }
                 cnn.Close();
                 Console.WriteLine("Connection Closed");
                 return true;
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex);
                 return false;
             }
         }

The rdr[0] is the value being taken from the database in numberofcopies column. I need to use that value to make the amount of copies based on that value.

Hopefully it is not too confusing, if you need clarification please let me know and i will provide further information.

Thank you.

1

There are 1 best solutions below

0
On

Made it work by adding this.

while (rdr.Read())
                 {
                     //Checks if the value is null
                     if (rdr[0] == null)
                     {
                         //If it is null execute the Console.WriteLine
                         Console.WriteLine("There is no value, it is null.");
                         return false;
                     }
                     else
                     {
                         //Converting object to int
                         int numberOfCopies = Convert.ToInt32(rdr[0]);
                         //Source directory for the PDF
                         string sourceDir = @"C:/Orders/0931617841_13_05_22/";
                         //Source directory for the new folder directory
                         string backupDir = @"C:/Orders/AmountOfCopies";

                         //If not null
                         try
                         {
                             //Gets the file composite.pdf inside the 'sourceDir' variable, which is instantiated above
                             string[] pdfList = Directory.GetFiles(sourceDir, "*.pdf");

                             // Copy pdf file.
                             foreach (string f in pdfList)
                             {
                                 //For loop to change the file name using 'i'
                                 for (int i = 0; i < numberOfCopies; i++)
                                 {
                                     string oldPath = @"C:/Orders/0931617841_13_05_22/composite.pdf";
                                     string newpath = @"C:/Orders/AmountOfCopies/";
                                     string newFileName = "composite" + i;
                                     FileInfo f1 = new FileInfo(oldPath);
                                     if(f1.Exists)
                                     {
                                         if(!Directory.Exists(newpath))
                                         {
                                             Directory.CreateDirectory(newpath); 
                                         }
                                         f1.CopyTo(string.Format("{0}{1}{2}", newpath, newFileName, f1.Extension));
                                     }
                                 }
                             }
                         }
                        //If no directory as that name is found execute exception
                         catch (DirectoryNotFoundException dirNotFound)
                         {
                             Console.WriteLine(dirNotFound.Message);
                         }
                     }
                 }