Alternating colors on DropDownList, ListBox and other ListControl.

My former colleague asked me if it is possible to attach an alternating color to a DropDownList control, similar with the GridView’s alternating rows.
I found out that you can achieve that, by setting the background-color of the ListItem using  “style” attributes.

Here is the code.
    /// <summary>
    /// A method that attach alternating colors
    /// to a ListControl. e.g (DropDownList, ListBox,
    ///
    /// </summary>
    /// <param name="listControl"></param>
    /// <param name="itemColor"></param>
    /// <param name="alternatingColor"></param>

    private void AttachColor(ListControl listControl,string itemColor,string alternatingColor)
    {       
        string color = itemColor;

        foreach (ListItem li in listControl.Items)
        {
            color = (color == itemColor) ? alternatingColor : itemColor;
            li.Attributes["style"] = " background-color: " + color;
        }       
    }


You can use it in your existing ListControl.

AttachColor(myListBox, "white", "lightblue");
AttachColor(myDropDownList, "white", "lightblue");

 

Sample ScreenShot:

Posted by n.ocampo with no comments
Filed under: , , ,

System.IO.Directory.GetFiles() does not support multiple search pattern

I have a task that will delete files in a certain directory, specifically a file with “*.csv” and *.log” extensions. Unfortunately System.IO.Directory.GetFiles(string,string) doesn't support multiple search pattern.

To be able to achieve my requirement I created a method that accepts an array of strings as parameter to hold the extensions.


public void DeleteFiles(List<string> extensions)
{
    foreach (string extension in extensions)
    {
        foreach(string file in System.IO.Directory.GetFiles(@"C:\MyFolder",extension))
        {               
           File.Delete(file);                           
        }
    }
}

I wish that on the next release of the framework it would have another overload for System.IO.Directory.GetFiles()

something like System.IO.Directory.GetFiles(string,string[]) or System.IO.Directory.GetFiles(string, List<string>)

 

UPDATE: Check out Keith's approach on searching a directory for files using multiple search patterns and using it in with Extension Methods

 

Posted by n.ocampo with 3 comment(s)
Filed under: ,

Oracle PL/SQL: Function or Procedure will not work when used in dynamic query

I came across this error ORA-00904: "GetTotalItem": invalid identifier when I tried to call a local function inside a dynamic query. I am currently working with Oracle database (we’re using Oracle 9i). My task is to add additional field (TotalItem) in the existing query in the StoredProc1 procedure.

//Package definition

CREATE OR REPLACE MyPackage AS

TYPE RefCursorType IS REF CURSOR;

PROCEDURE StoredProc1(myRefCursor OUT RefCursorType);

PROCEDURE StoredProc2();

..      

       FUNCTION GetTotalItem(productId IN NUMBER) RETURN NUMBER;                 

END;

 

//package body

CREATE OR REPLACE PACKAGE BODY MyPackage AS       

        PROCEDURE StoredProc1(myRefCursor OUT RefCursorType)

        IS

             MyQuery VARCHAR2(300);

        BEGIN

              myQuery := 'SELECT field1, field2, field3, GetTotalItem(productId) as TotalItem FROM Sometable'

 

OPEN myRefCursor FOR myQuery; //this will raise error ORA-00904

        END;

 

 .. .. // other codes omitted

END;

 

 

To fix the error, the function should be fully qualified with the package name.

 

'SELECT field1, field2, field3, MyPackage.GetTotalItem(producId) as TotalItem FROM Sometable'

... (currently learning Oracle) Geeked .

Posted by n.ocampo with no comments
Filed under: , ,

Short-circuit evaluation in C# and VB.NET

In my early years of college as a CS student I learned about short-circuit evaluation.  

Short-circuit evaluation denotes that the second argument is only evaluated if the first argument does not suffice the value of the expression. I found out that short-circuit evaluation in C# is different from VB.NET. Consider the following codes.

Here, if MyFunctionA() returns false and MyFunction() will not be executed.

if (MyFunctionA() && MyFunctionB())
{              

}

Same with this, if MyFunctionA() returns true, MyFunctionB() will not be evaluated.

if (MyFunctionA() || MyFunctionB())
{
 
//Some codes here         
}

In VB.NET, it’s different. Even if MyFunctionA() returns false, MyFunctionB() will also evaluated. There’s no short-circuit evaluation happened.

If (MyFunctionA() And MyFunctionB()) Then
 'Some codes here

End If

 
Same with this,

If (MyFunctionA() Or MyFunctionB()) Then
 'Some codes here

End If

 
Here’s the catch, to be able to achieve the short-circuit evalution in VB.NET use
AndAlso and OrElse keyword.

If (MyFunctionA() AndAlso MyFunctionB()) Then

If (MyFunctionA() OrElse MyFunctionB()) Then

 

I wonder why the keywords (instead of using And only and Or for VB.NET) are different from these 2 languages considering they were both from MS....Confused

Posted by n.ocampo with 7 comment(s)
Filed under: , ,

Debugging JavaScript code in ASP.NET

 

I am currently working with conversion of ASP Classic web application to ASP.Net 2.0 and I have to deal with a lot of client-side script. Before, I used to insert an alert(sampleValue) to be able to view the current value of a variable until I found out that about “debugger” keyword in JavaScript.

 
Here’s how to enable client debugging in ASP.NET.

 

  1. Uncheck the “Disable Script Debugging (Internet Explorer)“ options in Internet Explorer.

          IE Setting 

  1. Create a simple Web Form.

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientScriptDebugging.aspx.cs" Inherits="ClientScriptDebugging" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
    <title>Client-Side Debugging</title>
<
script language="javascript" type="text/javascript">
<!--
function
btnOk_onclick() {
    var ColorCode;
    var ColorDescription;
    var Name;   
      
    debugger;   
    Name = document.getElementById("txtName").value;
    ColorCode = document.getElementById("drpColor").value;       
    for(i = 0; j < document.getElementById("drpColor").options.length; j++)
    {
        if(document.getElementById("drpColor").options[j].value == ColorCode)
        {
           ColorDescription = document.getElementById("drpColor").options[j].text;
            break;
        }
    }       
    alert("Hi, " + Name + ".\n You're favorite color is " + ColorDescription);   
}
 

// -->

</script>
</
head>
<
body>
    <form id="form1" runat="server">
    <div>
        <select id="Select1" name="drpColor">
            <option selected="selected" value="0">Red</option>
            <option value="1">Blue</option>
            <option value="2">Yellow</option>
            <option value="3">Green</option>
            <option value="4">Orange</option>
            <option></option>
        </select>
        <input id="btnOk" style="width: 66px" type="button" value="OK" language="javascript" onclick="return btnOk_onclick()" />
        <input id="txtName" type="text" /></div>
    </form>
</
body>
</
html


            Once you run the application and the compiler hits the debugger keyword, VS will automatically breaks on this line. You can now use the rich debugging tools of Visual Studio that we usually use when debugging server side codes.

 

       Watch Window

  

Posted by n.ocampo with 1 comment(s)
Filed under: , ,

technical skills != opportunity

I came from an interview last weekend for a possible job in SG after a colleague of mine told me about the opening. It is an Indian firm that recently opens their Global delivery center in Singapore. The interview was held at the Renaissance Hotel in Makati. It was a 2-step process, first was the technical interview and if you passed you will be interviewed by the HR Manager.

 
The technical interview goes smooth, first the interviewer asked me to answer a 4 set of problems ( 2 T-SQL Task and 2 .net Coding) when I read the problems, I was very excited because it's not that hard. It's a simple joining of tables and filtering out the active records and a method to retrieve that in .Net. After solving that problem (I solved it right in-front of him) he reviewed my solutions and then proceed to some technical questions.

 
After the interview I came back to the waiting area and wait for the advice of their HR assistant. Luckily, the HR assistant told me that I passed the technical interview and advises to wait again for the interview with their HR Manager. 10 minutes later, my name was called. I was interviewed by their HR Manager, it was a usual interview of the HR people. Later on, he started to ask why I have a lot of short span of employment. I told him that it was a project basis (I worked as a contractual before). After the interview he instructed me to stay in the waiting area and wait for further advice of their HR assistant. 5 minutes later the HR assistant told me "Sorry, the HR Manager found out that you are not suitable for the position you are applying for".

 
Even though I’ve passed the tech interview, it’s not enough. It seems to me that the reason why they don’t consider me for the position is my “job hopping” experiences.

Posted by n.ocampo with 32 comment(s)
Filed under:

Finally, breaking my silence.

Welcome to my tech blog.

I'll be blogging about .net, devt tools, career and my software development experience. I've been here for about more than 1 year reading those great blogs before I finally break my silence to put up my own tech blogs. I hope this is a way to improve myself and share my experiences to everyone.

 
Feel free drop a note.

 

Posted by n.ocampo with 5 comment(s)
Filed under: