Click here to Skip to main content
11,701,616 members (69,019 online)
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 28-May-13 23:23pm

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

  Print Answers RSS
0 OriginalGriff 324
1 Maciej Los 275
2 Sergey Alexandrovich Kryukov 250
3 Mika Wendelius 189
4 CHill60 110
0 OriginalGriff 8,274
1 Sergey Alexandrovich Kryukov 7,317
2 CPallini 4,912
3 Maciej Los 4,146
4 Mika Wendelius 3,556


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 29 May 2013
Copyright © CodeProject, 1999-2015
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