Wednesday, April 13, 2011

How do I Turn negative numbers red?

I am working with a dataset, and will be returning both positive and negative currency figures. I already have a parenthesis around it. How do I get only the negative numbers in the gridview to show up in red? Can I do this on the HTML side?

From stackoverflow
  • There are many ways to do this, two spring to mind:

    1. Apply a CSS class that will set the element's color to red.
    2. Set the Color property on the WebControl you are using to render the number.

    Without some code it will be difficult to give you specific examples.

  • Without going to a third party control that has formatting rules, I would use the Row's data binding event and color the text of the cell in question when it is negative. This adds a slight bit of weight to the UI layer, but not enough it will be noticeable until you are delivering thousands and thousands of rows. If you are delivering thousands and thousands of rows, you probably have an architecture problem.

  • I would set the CssClass of the control to something that styles the text the way you want if the number is negative. The reason for using CssClass instead of FontColor is that you may change this in the future and it will be easier to just change the CSS style rather than any code that uses it.

    <asp:BoundField runat="server"
        DataField="Value"
        HeaderText="value"
        ItemStyleCssClass='<% (double)Eval("Value") < 0 ? "negative-number" : "" %>' />
    
  • protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    {
    
       myCell = new TableCell();       
       myCell.Text =   e.Row.DataItemIndex.ToString();
       myCell.Style["color"] = decimal.Parse(myCell.Text)<0?"Red":"OtherColor";
       e.Row.Cells.Add(myCell);
    
    
    }
    

    or something like that

  • You could try styling with jQuery:

    jQuery:

    $("span:contains('-')").css("color", "#f00");
    

    Of course, change span for whatever tag your number is in (and consider going into something more specific than just span like span.number-container for example.

WCF and Object

I am trying to pass an object into a WCF web service, the object I am passing in is a Server object, I then want to be able to call TestConnection();

The issue I am having is that Server is the base class and there are several derived classes of Server, i.e. SqlServer2005Server, OracleServer and ODBCServer that I want to use

I want to be able to pass in a Server Object and then determine its type, cast it and then use the method

public string TestServerConnection(Server server)
{
    if (server.ConnectionType == "SqlServer")
    {
        SqlServer2005Server x = (SqlServer2005Server)server;
        // Tests connection to server and returns result
        return x.TestConnection();
    }

    return "";
}

'Server' the base class implements IServer

I am unable to cast it, can you advise?

Much Appreciated

Phill

From stackoverflow
  • You need to add the KnownType declaration to your service contract for each derived class. There are ways to automate this (since it obviously convolutes code and breaks inheritance), but they require a lot of work.

  • Does the object you're passing represent a "live" connection to a DBMS? If the answer is yes, there is no hope of this ever working. Keep in mind that despite the pretty wrapper, the only thing your web service is getting from the caller is a chunk of xml.

  • As Daniel Pratt said, in the end, you are only shuttling XML (not always the case, but most of the time you are) across the wire.

    If you used a proxy generator to generate the definition of the Server type, then you aren't going to be able to make calls on the methods of Server, because only properties (semantically at least) are used in the proxy definition. Also, you can't cast to the derived types because your instance is really a separate type definition, not the actual base.

    If the Server type is indeed the same type (and by same, I mean a reference to the same assembly, not just in name and schema only), then you can do what Steve said and use the KnownType attribute on the Server definition, adding one attribute for each derived class.

    However, like he said, that convolutes your code, so be careful when doing this.

    I thought that using inversion of control would work here, but you run into the same situation with generic references to specific providers.

Using Cups Reverse Orientation on a Postscript file

When using the lp -o orientation-requested=6 in CUPS to print a postscript file, the first page is reversed 180 degrees and then the next page is back to 0 orientation. Third page has the 180 degree rotation and fourth page back to 0 and so on. I need each page in the file to be reversed 180 degrees. Any suggestions on what to try would be most appreciated.

From stackoverflow
  • If your PostScript file does not conform strictly to the PostScript DSC (document structuring convention), and the file is being sent to a PostScript printer, then whatever extras that CUPS may add to the pages may be getting overridden by ill-behaved per-page stuff in your document. Unfortunately there's no particularly good fix for overcoming arbitrary ill-behaved PostScript code. (The rotate-every-other-page or rotate-every-page-an-extra-180 behavior supports this via.

    If it's your own application that's generating the PostScript file, you might benefit from comparing what you pipe into the spooler against what CUPS actually sends to the printer (you could capture it with netcat).

A regular expression to retrieve the previous line in a log file

My log files contain the following:

2009-03-12T12:44:27+0000 something was logged
2009-03-12T12:45:36+0000 127.0.0.1 127.0.0.1 <auth.info> last message repeated 2 times

I can write a regular expression that retrieves the line with the "last message repeated..." statement, however, that line is meaningless without also retrieving the line that precedes it.

With that being said, does anyone know of a regular expression that would allow me to retrieve both lines whenever the "last message repeated..." statement is detected?

From stackoverflow
  • I would do it this way. Try to search for pattern that includes two groups. First group is a line followed by another group containing another line with "last message repeated" text. Then the content of the first group is the text you are looking for.

    Something like this (this is overly simplified regex):

    \n(.*)\n(.*)last message repeated
    

    Now first group value contain the line you are interested in.

    Huuuze : What would that regex look like?
    goldenmean : @David: Can u give the regex with two groups as u said?
    David Pokluda : Regexes added to the answer. Those are simple but you get the idea. They work - I verified them in Regex Buddy.
  • Edited to be 2 group matching regex. You can give it a shot at: RegexLib

    Less then optimized but this:

    ([\r\n].*?)(?:=?\r|\n)(.*?(?:last message repeated).*)
    

    Should work to get results out of something like this:

    2009-03-12T12:44:27+0000 something1 was logged
    2009-03-12T12:44:27+0000 something2 was logged
    2009-03-12T12:45:36+0000 127.0.0.1 127.0.0.1 <auth.info> last message repeated 2 times
    2009-03-12T12:44:27+0000 something3 was logged
    2009-03-12T12:44:27+0000 something4 was logged
    2009-03-12T12:44:27+0000 something5 was logged
    2009-03-12T12:45:36+0000 127.0.0.1 127.0.0.1 <auth.info> last message repeated 2 times
    

    Resulting in:

    Matches
    First Match, First Group: 2009-03-12T12:44:27+0000 something2 was logged
    First Match, Second Group: 2009-03-12T12:45:36+0000 127.0.0.1 127.0.0.1 <auth.info> last message repeated 2 times
    Second Match, First Group: 2009-03-12T12:44:27+0000 something5 was logged 
    Second Match, Second Group: 2009-03-12T12:45:36+0000 127.0.0.1 127.0.0.1 <auth.info> last message repeated 2 times
    
  • Does it have to be regex? grep allows you to get a context before and after match (-B NUM and -A NUM options)

    Huuuze : Good answer, but, yes, it has to be a regex.
  • The pattern ^.*$ matches a whole line. Translation: Start Of Line, followed by any number of characters, followed by End Of Line. So perhaps you can search for "any line, followed by" (the pattern you have there).

how do I install apache portable runtime

Hi,..I am trying to install subversion on a linux machine and get I get and error saying that I don't have APR installed

My question is: how do I install APR and link with with my Apache HTTP server (i have 2.2 running)?

I have looked for documentation for about 2 hours now,...can't find anything...i would also like to mention that i am pretty big unix newb.

Pointing me in the right direction would be greatly appreciated.

Andrew

From stackoverflow
  • Apache Portable Runtime is installed alongside apache server. all you have to do is make sure your LD_LIBRARY_PATH contains path for <APACHEINSTALLDIR>/lib and it will be detected by your svn.

    Andrew : what about apr-util?
    Andrew : nevermind. thanks.

What does the csrss.exe process do?

What is the purpose of the csrss.exe (Client/Server Runtime Server Subsystem) on Windows?

Maybe someone could give a good explanation or pointers to documentation? Unfortunately Google results are pretty noisy when searching a core process of Windows.

The reason I'm asking is that I got a BSOD from my service application which seems to be related to the csrss.exe process, at least this is what the analysis of the memory dump shows:

PROCESS_OBJECT: 85eeeb70

IMAGE_NAME:  csrss.exe

DEBUG_FLR_IMAGE_TIMESTAMP:  0
MODULE_NAME: csrss
FAULTING_MODULE: 00000000 
PROCESS_NAME:  PreviewService.
BUGCHECK_STR:  0xF4_PreviewService.
DEFAULT_BUCKET_ID:  DRIVER_FAULT
CURRENT_IRQL:  0
LAST_CONTROL_TRANSFER:  from 80998221 to 80876b40

STACK_TEXT:  
f5175d00 80998221 000000f4 00000003 85eeeb70 nt!KeBugCheckEx+0x1b
f5175d24 8095b1be 8095b1fa 85eeeb70 85eeecd4 nt!PspCatchCriticalBreak+0x75
f5175d54 8082350b 00000494 ffffffff 051bf114 nt!NtTerminateProcess+0x7a
f5175d54 7c8285ec 00000494 ffffffff 051bf114 nt!KiFastCallEntry+0xf8
051bf114 00000000 00000000 00000000 00000000 ntdll!KiFastSystemCallRet

STACK_COMMAND:  kb
FOLLOWUP_NAME:  MachineOwner
FAILURE_BUCKET_ID:  0xF4_PreviewService._IMAGE_csrss.exe
BUCKET_ID:  0xF4_PreviewService._IMAGE_csrss.exe

Followup: MachineOwner

EDIT: Thanks already for the good answers, but I actually don't need help concerning my service, I just would like to get some basic understanding of what the purpose of this service is.

From stackoverflow
  • csrss is the user-mode portion of the Win32 subsystem -- the user-mode analog to the kernel-mode win32.sys. As for what's going on with your particular service, it's hard to say without some better hints about what your service is doing.

  • CSRSS hosts the server side of the Win32 subsystem. It is considered a system critical process, and if it is ever terminated you'll get a blue screen. More data is necessary, but you need to find out if some process is terminating csrss, or if it is crashing due to a bug.

    Windows Internals is a great book for stuff like this. Wikipedia also has an article on CSRSS.

  • One thing it does is provide the default console window for console apps like cmd.exe. At least according to jdeBP:

    http://stackoverflow.com/questions/2807303/on-windows-how-does-console-window-ownership-work/2831185#2831185

    It would appear that when a console app process is created without being passed handles to stdin, stdout and stderr, then some interprocess comms asks csrss to create a console window in a new thread and give its IO handles to the console process.

what is the right way to validate if an object exists in a django view without returning 404?

basically just verify if an object exists and return the object. then based on that perform actions. I'm wondering whats the right way to do it without returning a 404?

try:
    listing = RealEstateListing.objects.get(slug_url = slug)
except:
    listing = None

if listing:
From stackoverflow
  • If you want a page to return 404 Not Found if an object doesn't exist, you can use django.shortcuts.get_object_or_404:

    listing = get_object_or_404(RealEstateListing, slug_url=slug)
    

    This will return the object with the given ID, or raise Http404 if it's not found.

    If you want to do something other than raise an Http404 exception, you could do something like this:

    try:
        listing = get_object_or_404(RealEstateListing, slug_url=slug)
    except Http404:
        # Do something else
    
    mipadi : I provided the second code snippet for that case. ;)
    Rasiel : oops.. sorry did not notice that somehow
  • I would not use the 404 wrapper if you aren't given a 404. That is misuse of intent. Just catch the DoesNotExist, instead.

    try:
        listing = RealEstateListing.objects.get(slug_url=slug)
    except RealEstateListing.DoesNotExist:
        listing = None
    
    Tiago : +1: I'd use this instead of 404 wrapper too.
    Carl Meyer : +1: Yes, this is a better solution than the accepted one, if you don't want the 404.
    Rasiel : yap, this seems to be the better solution