# Thursday, 28 October 2010

ASP.NET applications run under an account with limited privileges that doesn’t have the right to connect to a network printer.  The normal answer for this is to give rights to the user running your application or impersonate a user during execution that does have the rights.

We found another way that doesn’t require changing the user or privileges of the existing account that ASP.NET apps run under.

I can hear someone asking “Why would you want to do this anyway?  Can’t the end user choose a printer?”  There might be situations where this isn’t sufficient enough.  We had to control the printer line by line in order to print on the form that is being used and a particular printer had to be used.  

We added the network printer as a printer on the server where our app was running.  When we setup the printer we chose local not networked but then on the options to use a printer port we didn’t use LPT1.  Instead we created a new port with the IP address of the device.  In our code we then call the printer by the name we gave it on our server.

port

printer 

The PrintDocument class in the .NET Framework allows for control of a printer.

VB Version:

Public Class SpecialReportPrintJob 
  Inherits Printing.PrintDocument 
 
Protected Overrides Sub OnBeginPrint(ByVal ev as Printing.PrintEventArgs) 
  MyBase.OnBeginPrint(ev) 
 
  Me.PrinterSettings.PrinterName = "SCLaser2" 
 
  'setup rest of stuff.... 
End Sub   
End Class 
'And we then call it like so 
Dim printSpecialReport as new SpecialReportPrintJob() 
printSpecialReport.Print()
C# Version:
public class SpecialReportPrintJob : Printing.PrintDocument
{

    protected override void OnBeginPrint(Printing.PrintEventArgs ev)
    {
        base.OnBeginPrint(ev);

        this.PrinterSettings.PrinterName = "SCLaser2";

        //setup rest of stuff.... 
    }
}
//And we then call it like so
SpecialReportPrintJob printSpecialReport = new SpecialReportPrintJob();
printSpecialReport.Print();

PrintDocument is pretty powerful.  It can control just about anything for your printer.  We passed in a text file of our data to print and our code setup the x, y coordinates of each item as it related to where on the form it needed to be.

All this without having to muck around with permissions.  Always a good thing!

Happy Coding!

Posted 10.28.2010  #    Comments [0]  | 
# Wednesday, 20 October 2010

We had a situation with one of our applications were the team felt it was easier to use straight html to do some work in a form.  All went pretty well until we created a checkbox input array:

<input type="checkbox" name="CheckboxArray" />
<input type="checkbox" name="CheckboxArray" />
<input type="checkbox" name="CheckboxArray" />
<input type="checkbox" name="CheckboxArray" />

and we retrieved them via the Request.Form(“CheckboxArray”).

image

We discovered when we tried to retrieve the array only the checked items were being returned and their position in the array is lost. 

image

Which ones are on? We can’t assume it’s the first and second one.

Solution:

Give the checkboxes a value that uniquely identifies it.

<input type="checkbox" name="CheckboxArray" value="1" />
<input type="checkbox" name="CheckboxArray" value="2" />
<input type="checkbox" name="CheckboxArray" value="3" />
<input type="checkbox" name="CheckboxArray" value="4" />

Now when you retrieve the array it returns the values of the checkboxes that are checked.

image

What if you want to return all the checkboxes whether they are on or off?  A little more work is needed in this case.  We’ll add a hidden field array for each of our checkboxes and will have these fields keep the status of our checkboxes.

<input type="checkbox" name="CheckboxArray2" /><input type="hidden" name="CheckboxFix" value="false" />
<input type="checkbox" name="CheckboxArray2" /><input type="hidden" name="CheckboxFix" value="false" />
<input type="checkbox" name="CheckboxArray2" /><input type="hidden" name="CheckboxFix" value="false" />
<input type="checkbox" name="CheckboxArray2" /><input type="hidden" name="CheckboxFix" value="false"/>

We’ll use jQuery to help with keeping the status.

<script type="text/javascript">
    $(document).ready(function () {
      $("[name=CheckboxArray2]").click(function () {
        $(this).next("[name=CheckboxFix]").val($(this).attr("checked"));
    });
}); 
</script>

Now in the code behind we’ll use the CheckboxFix instead of using the CheckboxArray2 in the Request.Forms call.

image

In our case we went with this second option because we were using jQuery on the client to enable the user to add, delete and reorder the rows of information.  Using the latter technique allowed us to easily work with the submitted data in a similar manner with the rest of the items in each row.

Happy Coding!

Posted 10.20.2010  #    Comments [0]  |