0

I am using Adobe PDF Reader to view scanned paper from my computer and try to save it to database in my windows form application.

I select the PDF file by using open file dialog , Then I need to save it in my database. I am using the following code:

private void btnPDF_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                PDFViewer.src = ofd.FileName;
            }
        }
private void btnsave_Click(object sender, EventArgs e)
        {
            MemoryStream msins = new MemoryStream();
            MemoryStream msid = new MemoryStream();
            pictureInsurance.Image.Save(msins, pictureInsurance.Image.RawFormat);
            pictureIDIQAMA.Image.Save(msid, pictureIDIQAMA.Image.RawFormat);
            byte[] byteInsurance = msins.ToArray();
            byte[] byteId = msid.ToArray();
            FileStream fStream = File.OpenRead("C:\\myfile.pdf");
            byte[] Doccontents = new byte[fStream.Length];
            fStream.Read(Doccontents, 0, (int)fStream.Length);
            fStream.Close();

            if (update == 1)
            {
                patient.ADD_PATIENT(textName.Text,
                                Convert.ToInt32(textAge.Text),
                                textMobile.Text,
                                textEmail.Text, textaddress.Text,
                                Convert.ToInt32(combogender.SelectedValue),
                                textIDNO.Text,
                                Convert.ToInt32(comboNat.SelectedValue), 
                                byteInsurance,byteId,Doccontents);
                MessageBox.Show("Patient Added Successfully", "ADD PATIENT", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

When i use the fixed path its save the content in the database :

FileStream fStream = File.OpenRead("C:\\myfile.pdf");

but when i use the name of Adobe PDF reader to save the selected PDF file its show the following error "An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: URI formats are not supported."

This is the code with error :

FileStream fStream = File.OpenRead(PDFViewer.src);

And with this fix path it saved without error:

FileStream fStream = File.OpenRead("C:\\myfile.pdf");

This is the part code for my void parameters calling stored procedure and varbinary column is it correct ?

public void ADD_PATIENT(string DocContent )
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DAL.open();
            SqlParameter[] param = new SqlParameter[11];

            param[10] = new SqlParameter("@DocContent", SqlDbType.VarBinary);
            param[10].Value = DocContent;

            DAL.ExecuteCommand("ADD_PATIENT", param);
            DAL.close();

        }

What is the changes in code i need to do to save selected file instead of PDFViewer.src or how can i use byte[] method and memory stream like images to save PDF in the SQL server database ?

Abdullah
  • 983
  • 12
  • 26
  • I believe some of these links might shed some insight into what you're trying to do. https://stackoverflow.com/questions/3548401/how-to-save-image-in-database-using-c-sharp http://csharphelper.com/blog/2018/05/save-bitmap-files-in-wpf-and-c/ https://learn.microsoft.com/en-us/dotnet/api/system.io.memorystream?view=netframework-4.7.2 https://learn.microsoft.com/en-us/dotnet/api/system.io.filestream?view=netframework-4.7.2 While most of the samples are for WPF, you can still use most of the c# code in your winForm app. – nocturns2 Jan 05 '19 at 20:07

1 Answers1

1

The argument of File.OpenRead() requires a string. If PDFViewer.src is not of the type string but of the type URI (or similar) then you get the problem which you are mentioning. Make sure that PDFViewer.src is a string (containing a valid path and filename) or cast it to a string, if possible:

 FileStream fStream = File.OpenRead((PDFViewer.src).ToString());

or

 FileStream fStream = File.OpenRead((string)PDFViewer.src);
Codigo
  • 366
  • 2
  • 9
  • I added string as you post but i got same error URI formats are not supported. thanks for help – Abdullah Jan 05 '19 at 19:43
  • @Abdullah you have to check whether the PDFViewer.src contains valid characters and a valid path+filename. Sometimes there is a problem with // and \ and / as directory separator char. Those is dependent on the Culture and the operating system of your computer. – Codigo Jan 08 '19 at 19:50