Sunday, September 4, 2011

Reverse of a string by words C# example

string-reversal-example


Creating a program to generate the reverse of a string variable in C#, VB .Net, or C++ programming is pretty a easy job. Please note that we are talking about a word by word string reversal, for example the string given below 


Hello How Are You

Becomes the following string when read backwards

You Are How Hello


Reversal of characters inside a word is a different story, which is not covered here.
Some people find word order reversal difficult, that's why I'm putting the C# example code here.

Thursday, August 4, 2011

Nested conditional statement other than switch case in C#, C++, Java, PHP, JavaScript

nested-conditional-statement


Switch statements are nice, sometimes we need to just return some value based on an input variable. For example in our case I needed to get the key field of an LDAP object class we preferred to use in our software. So I had a simple C# function which went something like this


public static string getKeyByObjectClass(string strObjectClass){
switch(strObjectClass){
    case "user":
    return "samAccountName";
    case "user-mailbox":
    return "samAccountName";
    case "user-mail":
    return "samAccountName";
    case "contact":
    return "CN";
    case "contact-mail":
    return "CN";
    case "group":
    return "samAccountName";
    default:
    return "";
 }
}

I thought it is possible to simplify this switch statement with a nested conditional statement. And following was the result of some deliberation ;)








public static string getKeyByObjectClass(string strObjectClass){
              return (strObjectClass == "user") ? "samAccountName" : (strObjectClass == "user-mailbox") ?"samAccountName" :(strObjectClass == "user-mail") ? "samAccountName" : (strObjectClass == "contact") ? "CN" :(strObjectClass == "contact-mail") ? "CN" : (strObjectClass == "group") ? "samAccountName" : "";

You can definitely use || operator, minimize the code, and refine the crude version of nested conditional statement we created earlier, let's just do that

public static string getKeyByObjectClass(string strObjectClass){
              return (strObjectClass == "user" || strObjectClass == "user-mailbox" || strObjectClass == "user-mail") ? "samAccountName" :                                      (strObjectClass == "contact" || strObjectClass == "contact-mail" ) ? "CN" : "";
}

But now we definitely have some compromise on flexibility, maybe we'll want to use a different key for an object class say "user-mail" and then we'll have to modify this code in tiny bit different way(maintenance hell anyone?) ;)
I'm sure many intelligent folks have already done this or even a better variation but I also know that there are dumb guys and gals just like me out there who would most probably yell "Eurika!!!" on understanding the magic of nested conditional statements.
Its not just and only C# stuff, in fact the code will work with JavaScript, PHP, Java, C++ and many others with little or no variation.

Saturday, July 30, 2011

Cannot rename htaccess.txt to .htaccess in cPanel File Manager

Just today I was trying to fix the 404 broken link problem with the Joomla installation on my upcoming website, http://www.ldapfilters.com which will contain certain knowledge base about LDAP and directory servers in general, along with discussion forums.
So, they say WARNING! Apache users only! Rename htaccess.txt file to .htaccess before activating.
I opened the File Manager from CPanel, scrolled down to the htaccess.txt file right clicked and renamed it and the File Manager finally said "No! You can't rename, a file already exists." I tried and retried several gimmicks but it didn't work.
Solution: Then I decided to try legacy file explorer, I opened the CPanel Legacy File Manager, located htaccess.txt, clicked it, selected Rename from the menu, typed in .htaccess and it worked well ;)

A Confusion: I got confused and start thinking, shall I rename htaccess.txt to htaccess.htaccess or just remove the .txt from end of file name, and put a dot at the start of file name? Well the answer is yes, I had to take the second option. Therefore, just rename it to .htaccess that means a file with no name but just an extension.
And one more thing, I renamed htaccess.txt to htaccess_.txt and then tried to access my website, even the home page of my Joomla based website didn't show up let alone the broken links which I was trying to fix :p
Therefore, be very careful when you touch these configuration files, they are absolutely sensitive. Some times you catch the bug right after saving that file and refreshing that page but at some other time the setting you've just changed has got a much deeper effect which gets unleashed by some twisted user action sequence, so here's the GOLDEN ADVICE

Back those files up!!!

Monday, March 7, 2011

System.DirectoryServices A referral was returned from the server Exception

System.DirectoryServices showed " A referral was returned from the server " Exception just today, and it left me wondering what's wrong?
I was trying to pull data from a Microsoft Active Directory Group, and while attempting to query the data the aforementioned exception was occurring. An eagle eye debugging showed the cause and it was simple but stupid.

Following is the correct format of LDAP connection string

LDAP://abc.xyz.pqr.lab:389/CN=TheGroup,
CN=Users,DC=abc,DC=xyz,DC=pqr,DC=lab





But our dear programmer had done it a little differently, I think it was just a type, the wrong format of LDAP connection string was following

LDAP://abc.xyz.pqr.lab/CN=TheGroup,
CN=Users,DC=abc,DC=xyz,DC=pqr,DC=lab:389

This was causing the system to show the Active Directory related exception in C# .Net on Visual Studio 2008.









Notice the wrong positioning of LDAP port number, people some times make absolutely silly mistakes.

Keep in mind that 389 is default port for insecure communication with LDAP directory server. For secure exchange of data use LDAPS, default port for LDAPS is 636.

These port numbers are recommended, and LDAP servers use these out of the box. But not all LDAP servers always use port 389 and 636. It is possible to change the default LDAP port numbers, please check your directory server software's documentation for details.

I'm working as a freelance programmer through ODesk.com, I have a strong profile over there. Feel free to ping me if you need an Android app.

https://www.odesk.com/users/~012d73aa92fad47188



Friday, February 18, 2011

Using Excel 2003 File As Data Source Of Visual Studio .Net Data Driven Unit or Functional Test

First of all this will be the attribute tag for setting up data access of a data driven Unit or Function test in Visual Studio, I am using Visual Studio 2008 but I think same will work in Visual Studio 2010

[DataSource("System.Data.OleDb", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\FunctionalTestsDataFiles\\Smoke_Cred.xls;Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", "ldapdest$", DataAccessMethod.Sequential), TestMethod]

Put the name of your .xls file in place of "Smoke_Cred.xls" and put the name of your excel sheet in place of "ldapdest$", don't forget to put the $ sign after the name of your Excel sheet.

Be very careful with the "System.Data.OleDb" because the people at Microsoft have made a case sensitive comparison in their code which wasted so many of my hours, and gave me too much tension.
If you write "System.Data.OleDB" instead, it will fail in functional/unit test but it will work fine in other connection string testing utilities.


Second thing, the syntax of spread sheets inside excel files is different from syntax used for Access databases. You need to put a $ character after the name of the sheet you are trying to access.
I found an article on MSDN about running data driven test cases with Ms Access, but there was no mention of these little pieces of crap which I am sure must have been killing time of many poor developers.
Instead of solving problems, you have to run after crappy case sensitive and idiotic connection strings, what an irony!!!

If you use "System.Data.OleDB" mistakenly, following error will be displayed

Error details: Unable to find the requested .Net Framework Data Provider.  It may not be installed.

And if you somehow luckily fix this, and don't know that you need to put a $ sign after the name of that excel sheet, you'll get following error

Error details: The Microsoft Jet database engine could not find the object 'ldapdest'.  Make sure the object exists and that you spell its name and the path name correctly.









One more twist, the TestContext used by some s of b in MSDN article is not found anywhere by default, and you need to define it before using it. I wonder what Microsoft's trying to do with us developers.

 private TestContext testContextInstance; 
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}

The link of the piece of crap MSDN article is following:
I know I'm using rash and bad language but what else do you expect from someone on 4:30 AM? When you come to know that only problem with your code was that you used "B" instead of "b" and you must be intelligent enough to put a $ in the end of table name of excel sheets here. I mean why in the world Microsoft idiots cant just fix these tiny issues??? I'm too much pissed right now... literally pissed...


RECOMMENDATION : Don't use Excel files as a data source for your dynamic tests, instead use .csv provider. It works well on both 32 and 64 bit machines. Excel will only increase the pain.