Wednesday, November 9, 2011

How to iterate through DataRow ItemArray

If you have data row array data in a variable dr, you can use below foreach loop to iterate over ItemArray values


  foreach (DataRow data in dr)
            {
                foreach (object value in data.ItemArray)
                {
                    Console.Write("{0} ", value);
                }
                Console.WriteLine();
            
            }


Please post your comments

How to sort and filter DataTable rows in C#

To explain with an an example I am creating a DataTable


DataTable dt = new DataTable();


You can add columns in one of these two ways
            
DataColumn id=new DataColumn("ID",Type.GetType("System.Int32"));
DataColumn name=new DataColumn("Name",Type.GetType("System.String"));


or 


dt.Columns.Add(id,typeof(int));
dt.Columns.Add(name,typeof(string));


Then add rows of data


dt.Rows.Add(10,"Bharath");
dt.Rows.Add(5,"Kumar");




Here we use DataTable Select method for filtering and sorting the data. I am not providing any parameter for first parameter (filter) and I am sorting the Data by ID (by default in Ascending order). If you want in Descending order mention "ID DESC"


if you want to filter data for ids > 5 then you can mention the first parameter as "ID > 5"


DataRow[] dr=dt.Select("","ID")


You will get the sorted Row Array in dr variable. Check ItemArray of dr variable for values.


Please provide your comments

How to convert ASCII to EBCDIC binary format in C#

Extended Binary Coded Decimal Interchange Code (EBCDIC) is an 8-bit character encoding used mainly on IBM mainframe and IBM midrange computer operating systems.


EBCDIC descended from the code used with punched cards and the corresponding six bit binary-coded decimal code used with most of IBM's computer peripherals of the late 1950s and early 1960s. It is also employed on various non-IBM platforms such as Fujitsu-Siemens' BS2000/OSD, HP MPE/iX, and Unisys MCP.


Today I am going to discuss how to convert ASCII text into EBCDIC format. ASCII number 65 represents A where as it has different meaning in EBCDIC. Thats why you are reading this article. I will give you sample functions for converting ASCII into EBCDIC and EBCDIC into ASCII and conversion tables for easy use.


1) ASCII to EBCDIC


Most of the times these days, we are dealing with UNICODE characters and we need to convert them to ASCII 8-bit format unless you are working on legacy systems.


This function helps you converting given input string into ascii bytes.


  private byte[] convertToASCIIcharacter(string input)
        {
            System.Text.ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] character = ascii.GetBytes(input);
            ASCII2EBCDIC_Char(ref character);
            return character;
       
        }


ASCII2EBCDIC_Char function looks like this.


private void ASCII2EBCDIC_Char(ref byte[] input)
        {


            int[] A2E = new int[128]{0,1,2,3,55,45,46,47,22,5,37,11,12,13,14,15,
                                   16,17,18,19,60,61,50,38,24,25,63,39,28,29,30,31,
                                   64,79,127,123,91,108,80,125,77,93,92,78,107,96,75,97,
                                   240,241,242,243,244,245,246,247,248,249,122,94,76,126,110,111,
                                   124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
                                   215,216,217,226,227,228,229,230,231,232,233,74,224,90,95,109,
                                   121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
                                   151,152,153,162,163,164,165,166,167,168,169,192,106,208,161,7};


            for (int counter = 0; counter < input.Length; counter++)
            {


                byte temp = (byte)A2E[input[counter]];
                input[counter] = temp;
            }
            
        }


2) EBCDIC to ASCII


  If you want to convert EBCDIC files to ASCII file format, you need to have conversion table. Here you go.


private void EBCDIC2ASCII(ref byte[] input)
        {
            int[] E2A=new int[256] {0,1,2,3,26,9,26,241,26,26,26,11,12,13,14,15,
                                    16,17,18,19,26,26,8,26,24,25,26,26,28,29,30,
                                    31,26,26,26,26,26,10,23,27,26,26,26,26,26,5,6,7,
                                    26,26,22,26,26,26,26,4,26,26,26,26,20,21,26,26,
                                    32,26,26,26,26,26,26,26,26,26,91,46,60,40,43,33,
                                    38,26,26,26,26,26,26,26,26,26,93,36,42,41,59,94,
                                    45,47,26,26,26,26,26,26,26,26,124,44,37,95,62,63,
                                    26,26,26,26,26,26,26,26,26,96,58,35,64,39,61,34,
                                    26,97,98,99,100,101,102,103,104,105,26,26,26,26,26,26,
                                    26,106,107,108,109,110,111,112,113,114,26,26,26,26,26,26,
                                    26,126,115,116,117,118,119,120,121,122,26,26,26,26,26,26,
                                    26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,
                                    123,65,66,67,68,69,70,71,72,73,26,26,26,26,26,26,
                                    125,74,75,76,77,78,79,80,81,82,26,26,26,26,26,26,
                                    92,26,83,84,85,86,87,88,89,90,26,26,26,26,26,26,
                                    48,49,50,51,52,53,54,55,56,57,26,26,26,26,26,26};




            
            for (int counter = 0; counter < input.Length; counter++)
            {


                input[counter] = (byte)E2A[input[counter]];
            }
            
            
        }


You can call the above function something like this


 EBCDIC2ASCII(ref dump_record); and have dump_record declared as 
 byte[] dump_record


If you want to write as Binary file, open an instance of BinaryWriter class with encoding as Encoding.BigEndianUnicode.


Please leave your comments and start asking your programming questions on http://www.techielog.com

Wednesday, August 17, 2011

How to Encrypt and Decrypt passwords in LoadRunner

How to Encrypt a password in LoadRunner?

Some times when you are recording an application, LoadRunner may not do the password encryption automatically. If it does, you will see lr_decrypt(“376347drjhd….”) in the password field otherwise, password is displayed as plain text.

LoadRunner provides a tool called “Password Encoder” which can be found under
Start > All Programs > HP LoadRunner > Tools
in Windows Environment

User Interface looks like this:


Fields Description:
Password: Enter the password you want to encrypt, and then click Generate.
Encoded String: Displays the encrypted password string.
Generate: Generates an encoded string after a password is entered.
Copy: Copies the encoded password string to the clipboard.
Once the password is encrypted, the screen appears as


Note: If you click on Generate again, a new encrypted string appears for the same password.
Click on Copy.

How to view Encrypted password?
You may have to view the password sometimes for some reason, and LoadRunner doesn’t provide any tool for Decrypting the password. So, How shall we view?

The simple technique is open a Web(HTTP/HTML) protocol and type the following statement into Action action and Run.

lr_log_message("%s",lr_decrypt("4e4b0e8ff417f9f9fd06a341ec1a"));

Note: The alphanumeric code displayed above is the encrypted password. Replace it with your code.

Result: HelloWorld will be displayed which is encrypted above.

Similarly, we can view encrypted passwords in QTP also.

Data Execution Prevention error while recording in LoadRunner

What is Data Execution Prevention (DEP) in Windows?

Data Execution Prevention (DEP) helps prevent damage from viruses and other security threats that attack by running (executing) malicious code from memory locations that only Windows and other programs should use. This type of threat causes damage by taking over one or more memory locations in use by a program. Then it spreads and harms other programs, files, and even your e-mail contacts.

Unlike a firewall or antivirus program, DEP does not help prevent harmful programs from being installed on your computer. Instead, it monitors your programs to determine if they use system memory safely. To do this, DEP software works alone or with compatible microprocessors to mark some memory locations as "non-executable". If a program tries to run code—malicious or not—from a protected location, DEP closes the program and notifies you.

Is it safe to run a program again if DEP has closed it?

Yes, but only if you leave DEP turned on for that program. Windows can continue to detect attempts to execute code from protected memory locations and help prevent attacks.

How can I tell if DEP is available on my computer?
  1. To open System Properties, click Start, click Control Panel, and then double-click System.
  2. Click the Advanced tab and, under Performance, click Settings.
  3. Click the Data Execution Prevention tab.
By default, DEP is only turned on for essential Windows operating system programs and services.

How Data Execution Prevention affects recording using LoadRunner?
Sometimes when you try to record an application using LoadRunner in Windows, you get an error message looks similar to the following.


Here the application being blocked is Oracle SQL*PLUS. Since this is a trusted program, and to get around this problem, we need to disable DEP for this program or Enable DEP for rest of all Windows programs except this program.

To do that,
  1. To open System Properties, click Start, click Control Panel, and then double-click System.
  2. Click the Advanced tab and, under Performance, click Settings.
  3. Click the Data Execution Prevention tab 
  4. In this case, check Oracle SQL*PLUS
    5. Click on Ok and Ok on System Properties Window.


Try to record the same application again and this time it works.