Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Printing from CLR throws an exception Expand / Collapse
Author
Message
Posted Tuesday, February 3, 2009 12:10 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 2:37 PM
Points: 78, Visits: 284
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.
Post #649184
Posted Tuesday, February 3, 2009 12:18 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 5:42 AM
Points: 40,396, Visits: 36,837
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 2008, MVP
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

Post #649194
Posted Tuesday, February 3, 2009 12:21 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 2:37 PM
Points: 78, Visits: 284
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.
Post #649199
Posted Tuesday, February 3, 2009 12:33 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, November 3, 2014 2:42 PM
Points: 1,683, Visits: 1,798
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
Post #649216
Posted Tuesday, February 3, 2009 12:39 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 1:19 PM
Points: 9,294, Visits: 9,491
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."
Post #649222
Posted Tuesday, February 3, 2009 12:43 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 1:19 PM
Points: 9,294, Visits: 9,491
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."
Post #649225
Posted Tuesday, February 3, 2009 12:46 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 2:37 PM
Points: 78, Visits: 284
I have already done that.

Same error


Thanks.

Gunjan.
Post #649226
Posted Tuesday, February 3, 2009 12:47 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 2:37 PM
Points: 78, Visits: 284
I am only trying to print it out.

and this line throwing an exception

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



Thanks.

Gunjan.
Post #649229
Posted Tuesday, February 3, 2009 1:05 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, November 3, 2014 2:42 PM
Points: 1,683, Visits: 1,798
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
Post #649255
Posted Tuesday, February 3, 2009 1:41 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 5:42 AM
Points: 40,396, Visits: 36,837
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 2008, MVP
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

Post #649310
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse