Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server C#4.0
Hi,
In my Web Application project I am retrieving the File Data Stored in the database storing that data in a temporary file and printing that Temporary File (the file will be either PDF or Word File While I try to print the file the Application Adobe reader or MS Word tells that file is not found, Can any one say what is the Problem in my Code which I have mentioned below,
 
 protected void ggvqpdetail_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToUpper().ToString() == "PRINTREC")
            {
                
                try
                {
                    string a = TextBox1.Text + TextBox2.Text + TextBox3.Text;
                    DataTable dt = new DataTable();
                    IDataReader dr = ExamManagement.SP.Eval_QP_PrintSelect(a).GetReader();
                    dt.Load(dr);
                    dr.Close();
                    dr.Dispose();
 
                    Byte[] data = (Byte[])dt.Rows[0]["Data"];
                   
                    if (ddlprint.SelectedIndex != -1 && dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["Type"].ToString() == "application/pdf")
                        {
                           PrintFormPdfData(data);
                        }
                        else if (dt.Rows[0]["Type"].ToString() == "application/vnd.ms-word")
                        {
                            PrintFormWordData(data);
                        }
 
                    }
                }
                catch(Exception ex)
                {
                    ClientMessaging(ex.Message);
                }
            }
        }
        private void PrintFormPdfData(byte[] formPdfData)
        {
            string tempFile;
 
            tempFile = Path.GetTempFileName();
 
            using (FileStream fs = new FileStream(tempFile, FileMode.Create))
            {
                fs.Write(formPdfData, 0, formPdfData.Length);
                fs.Flush();
            }
 
            try
            {
                string gsArguments;
                string gsLocation;
                ProcessStartInfo gsProcessInfo;
                Process gsProcess;
 
                gsArguments = string.Format("/h /t \""+ddlprint.SelectedItem.ToString()+"\" \"{0}\"", tempFile);
                gsLocation = @"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe";
 
                gsProcessInfo = new ProcessStartInfo();
                gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
                gsProcessInfo.FileName = gsLocation;
                gsProcessInfo.Arguments = gsArguments;
 
                gsProcess = Process.Start(gsProcessInfo);
                gsProcess.WaitForExit();
                gsProcess.Kill();
            }
            finally
            {
                
                File.Delete(tempFile);
            }
        }
        private void PrintFormWordData(byte[] formWordData)
        {
            string tempFile;
 
            tempFile = Path.GetTempFileName();
 
            using (FileStream fs = new FileStream(tempFile, FileMode.Create))
            {
                fs.Write(formWordData, 0, formWordData.Length);
                fs.Flush();
            }
 
            try
            {
                string gsArguments;
                string gsLocation;
                ProcessStartInfo gsProcessInfo;
                Process gsProcess;
 
                gsArguments = string.Format("/h /t \"" + ddlprint.SelectedItem.ToString() + "\" \"{0}\"", tempFile);
                gsLocation = @"C:\Program Files\Microsoft Office\OFFICE11\WINWORD.exe";
 
                gsProcessInfo = new ProcessStartInfo();
                gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
                gsProcessInfo.FileName = gsLocation;
                gsProcessInfo.Arguments = gsArguments;
 
                gsProcess = Process.Start(gsProcessInfo);
                gsProcess.WaitForExit();
                gsProcess.Kill();
            }
            finally
            {
 
                File.Delete(tempFile);
            }
        }
Posted 29-May-13 0:23am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 29 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100