SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Printing from CLR throws an exception


Printing from CLR throws an exception

Author
Message
Gunjan
Gunjan
Old Hand
Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)

Group: General Forum Members
Points: 372 Visits: 309
Hi,

I am trying to print the byte array which I am receiving from reporting service's render method.
I have used PrintDocument class in my CLR stored proc. I have also addded the system.drawing assembly. here is my code

PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();

public String PrintImage(string printerDestination, byte[] image)
{
try
{


string status = "";
_documentToPrint = image;

printDoc.PrinterSettings.PrinterName = @"\\printserver\HR Canon iR5070"; //printerDestination;

//PrintingPermission b = new PrintingPermission(PrintingPermissionLevel.DefaultPrinting);

strErrMsg = "Setting Print Controller->";
PrintController standard = new StandardPrintController();
printDoc.PrintController = standard;
//wire event
//printDoc.PrintPage += new PrintPageEventHandler(this.printDoc_PrintPage);
printDoc.PrintPage+=new PrintPageEventHandler(printDoc_PrintPage);
strErrMsg = strErrMsg + "Calling Print Method | " + printDoc.PrinterSettings.PrinterName + "|";

printDoc.Print();
//status = "File Printed";
return strErrMsg;
}
catch (Exception ex)
{
_documentToPrintCurrentPage = 0;
// strErrMsg = strErrMsg + ex.Message;
strErrMsg = strErrMsg + ex.Message + "->" + ex.InnerException.Message + "->" + _documentToPrint.Length.ToString() + "-> Print Image Method";
//status = strErrMsg;
// strErrMsg = "";

}
}

private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
strErrMsg = strErrMsg + "inside printDoc_PrintPage | ";
MemoryStream ms = new MemoryStream(_documentToPrint);
strErrMsg += "--lenght:" + ms.Length.ToString() +".";
Bitmap bm = (Bitmap)Bitmap.FromStream(ms); // Throwing an exception at this line
Graphics g = e.Graphics;


try
{
int pagesFound = bm.GetFrameCount(FrameDimension.Page);

if (pagesFound > 0)
{
bm.SelectActiveFrame(FrameDimension.Page, _documentToPrintCurrentPage);
_documentToPrintCurrentPage++;
if (_documentToPrintCurrentPage >= pagesFound)
{
e.HasMorePages = false;
_documentToPrintCurrentPage = 0;
}
else
e.HasMorePages = true;

g.DrawImageUnscaled(bm, 0, 0);
}

}
catch (Exception ex)
{
_documentToPrintCurrentPage = 0;
throw ex;
}
}

Any ideas?

Thanks.

Thanks.

Gunjan.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)

Group: General Forum Members
Points: 119158 Visits: 45554
Um..

The CLR proc runs on the server, within the database server. Where do you think it's going to print to?

What exception is it throwing?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Gunjan
Gunjan
Old Hand
Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)

Group: General Forum Members
Points: 372 Visits: 309
It should print it to network printer.
and the exception is

System.NullReferenceException: Object reference not set to an instance of an object.
System.NullReferenceException:at StoredProcedures.BRCOffer_Process(String DataBaseName)
.

Thanks.

Gunjan.
Jonathan Kehayias
Jonathan Kehayias
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3768 Visits: 1807
You are the 3rd person to try this, and it is not possible to do. This is akin to trying to use a MessageBox.Show() in a clr routine in SQL. This is application code and SQLCLR exists only to extend existing functionality. This is why the approved classes for SAFE assemblies is limited. Just because you can load the assembly doesn't guarantee it will execute.

Move this outside of SQL server where it belongs.

Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
My Blog | Twitter | MVP Profile
Training | Consulting | Become a SQLskills Insider
Troubleshooting SQL Server: A Guide for Accidental DBAs
RBarryYoung
RBarryYoung
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19652 Visits: 9518
I believe that you have to explicitly catalogue System.Drawing.dll as UNSAFE in order to get it into SQL.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
RBarryYoung
RBarryYoung
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19652 Visits: 9518
Jonathan: which is the part that cannot be done? Just the Bitmap stuff or do you mean anything from System.Drawing.dll?

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Gunjan
Gunjan
Old Hand
Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)

Group: General Forum Members
Points: 372 Visits: 309
I have already done that.

Same error

Thanks.

Gunjan.
Gunjan
Gunjan
Old Hand
Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)Old Hand (372 reputation)

Group: General Forum Members
Points: 372 Visits: 309
I am only trying to print it out.

and this line throwing an exception

Bitmap bm = (Bitmap)Bitmap.FromStream(ms);

Thanks.

Gunjan.
Jonathan Kehayias
Jonathan Kehayias
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3768 Visits: 1807
I am not in a position to check immediately but I would venture a guess that the HPA's on the Assembly may preclude it from use in SQL completely. At best ( or worst depending on which side of the SQLCLR fence you sit on) you will only be able to use part of the classes in the Assembly.

SQL Server is a database server, not an application, image processing, or print server. If it does have to do with validating/manipulating/reading/updating data it has no business in SQL server. You just can't convince .NET developers of this for some reason. Is there a DBA anywhere involved with this project/development?

Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
My Blog | Twitter | MVP Profile
Training | Consulting | Become a SQLskills Insider
Troubleshooting SQL Server: A Guide for Accidental DBAs
GilaMonster
GilaMonster
SSC Guru
SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)SSC Guru (119K reputation)

Group: General Forum Members
Points: 119158 Visits: 45554
Jonathan Kehayias (2/3/2009)
If it does not have to do with validating/manipulating/reading/updating data it has no business in SQL server.


I think you left a not out of that statement

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search