Click here to Skip to main content
15,891,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a MS SQL DB Table which stores the File Name and the Data of that file. In the Client-Side I want the client to fetch the file from the database and print it with the Printer Selected from the List of Installed Printers from the Dropdownlist, I had tried in many options and Code snippets for reading the file and printing it but it is not working Can anyone help me out with this ?

I tried with the Following Code,

C#
protected void ggvqpdetail_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           if (e.CommandName.ToUpper().ToString() == "PRINTREC")
           {
               try
               {
                   // Set the printer to a printer in the dropdown box when the selection changes.
                   PrintDocument printDoc = new PrintDocument();
                   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();
                   if (ddlprint.SelectedIndex != -1 && dt.Rows.Count > 0)
                   {
                       // The dropdown box's Text property returns the selected item's text, which is the printer name.
                       printDoc.PrinterSettings.PrinterName = ddlprint.Text;

                       ProcessStartInfo printJob = new ProcessStartInfo();
                       printJob.FileName = dt.Rows[0]["Filename"].ToString();

                       printJob.UseShellExecute = true;
                       printJob.Verb = "printto";
                       printJob.CreateNoWindow = true;
                       printJob.WindowStyle = ProcessWindowStyle.Hidden;
                       printJob.Arguments = ddlprint.Text;//Getting Which Printer

                       Process.Start(printJob);
                       if(dr.Read())
                       {

                       }

                   }
               }
               catch(Exception ex)
               {
                   Lblmsg.Visible = true;
                   Lblmsg.Text = ex.Message;
               }
           }
       }
Posted
Updated 28-May-13 2:04am
v2
Comments
CHill60 28-May-13 8:01am    
You say "it is not working" but you have not posted the code that you have tried, nor said how it is not working. Use the improve question link to provide the missing detail
Yuriy Loginov 28-May-13 10:42am    
are you getting an exception when you run that code? If so, you should post it so people can understand what "not working" means.
Rajesh Kumar Sowntararajan 29-May-13 0:02am    
@yloginov I am getting the "Specified file Not Found" Win32 Exception
Yuriy Loginov 29-May-13 7:57am    
Are you sure the file exists?
For this line of code
printJob.FileName = dt.Rows[0]["Filename"].ToString();
try hardcoding the file name to the one that you know exists for sure and test your code again. So change the code above to something like this,
printJob.FileName = @"c:\my_test_file.txt";

Rajesh Kumar Sowntararajan 29-May-13 8:06am    
@yloginov I am very much sure that file exists, instead of using FileName I had tried in with Storing the retrivied data in a Temporary file and Printing (the may be PDF or Word) but PDF Reader or MS Word App showed file not found error I had posted the Error in http://www.codeproject.com/Questions/599728/ProblemplusofplusPrintingplustheplusfileplusRetrie

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900