Click here to Skip to main content
11,934,783 members (41,538 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# SQL-Server C#4.0
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,

protected void ggvqpdetail_RowCommand(object sender, GridViewCommandEventArgs e)
           if (e.CommandName.ToUpper().ToString() == "PRINTREC")
                   // 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();
                   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

               catch(Exception ex)
                   Lblmsg.Visible = true;
                   Lblmsg.Text = ex.Message;
Posted 28-May-13 1:53am
Edited 28-May-13 3:04am
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
yloginov 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.
Member 9903922 29-May-13 0:02am
@yloginov I am getting the "Specified file Not Found" Win32 Exception
yloginov 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";

Member 9903922 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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 28 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