"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Monday, September 20, 2010

Difference between ExecuteReader,ExecuteNonQuery and ExecuteScalar.

Hi frienz,
i got a bit for ur nxt interview :). here i giving the keyhole difference b/w ExecuteReader,ExecuteNonQuery and ExecuteScalar.

* ExecuteReader : Use for accessing data. It provides a forwardonly,
read-only, connected recordset.

* ExecuteNonQuery : Use for data manipulation, such as Insert,
Update, Delete.

* ExecuteScalar : Use for retriving 1 row 1 col. value., i.e.
Single value. eg: for retriving aggregate function. It is faster
than other ways of retriving a single value from DB.

Hope it help u someways...
Have a nice day... 'N happy Coding :)

Wednesday, September 15, 2010

defining css with javascript as per criteria

 Hi,

I came across a requirement where the stylesheets referenced by a page had to be listed. The user was then given the ability to disable the stylesheet he did not want. Here’s how we can use the document.styleSheets collection to retrieve the collection of styleSheet objects that are referenced using a link; and then disable the stylesheet the user does not desire. A portion of the code is as given here:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="CSS/FloatDes.css" rel="stylesheet"
    title="Float layout" type="text/css" />
    <link href="CSS/RoundCorners.css" rel="stylesheet"
    title="Rounded Corners" type="text/css" /> 

    <script type="text/javascript">
        for (i = 0; i < document.styleSheets.length; i++) {
            alert(document.styleSheets[i].title);
            alert(document.styleSheets[i].href);
        }
    </script>
</head>
<body>

</body>
</html>



Similarly, if you have imported stylesheets using @import, you can access the style sheets using document.styleSheets[i].imports.
To disable a stylesheet programmatically, use this piece of code:
document.styleSheets[i].disabled = true;
To check if the styleSheet collection is supported on your browser, use this :
if (typeof document.styleSheets != "undefined")
 
If it is broken change the css  using javascript 
 This javascript demonstrates how you can programmatically 
reference a CSS link on a page. 
   <script type="text/javascript">
        function addCSS() {
            var headtg = document.getElementsByTagName('head')[0];
            if (!headtg) {
                return;
            }
            var linktg = document.createElement('link');
            linktg.type = 'text/css';
            linktg.rel = 'stylesheet';
            linktg.href = 'CSS/RoundCorners.css';
            linktg.title = 'Rounded Corners';
            headtg.appendChild(link);
        }
    </script>
</head>
<body onload="addCSS()"> 


Have a nice day... 'N happy Coding :)

Wednesday, September 1, 2010

64 Bit encryption in asp.net

Hi frienz,
2day i'm showing 64 bit encryption and decryption using a Key in asp.net.for that u need to import these name space's,


using System.Security.Cryptography;
using System.Text;
using System.IO;


Encryption



    private const string DEFAULT_KEY = "#kl?+@<z";



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }

    }
    protected void txt_string_TextChanged(object sender, EventArgs e)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        MemoryStream MemoryStream = new MemoryStream();
        CryptoStream Cryptostream;
        string key="#kl?+@<z";
        CheckKey(ref key);
        des.Key = HashKey(key, des.KeySize / 8);
        des.IV = HashKey(key, des.KeySize / 8);

        string stringToEncrypt = txt_string.Text.Trim();

        byte[] inputBytes = Encoding.UTF8.GetBytes(stringToEncrypt);

        Cryptostream = new CryptoStream(MemoryStream, des.CreateEncryptor(), CryptoStreamMode.Write);
        Cryptostream.Write(inputBytes, 0, inputBytes.Length);
        Cryptostream.FlushFinalBlock();
        string result = Convert.ToBase64String(MemoryStream.ToArray());
        Label1.Text = result.ToString();
       
    }

    private static void CheckKey(ref string keyToCheck)
    {
        keyToCheck = keyToCheck.Length > 8 ? keyToCheck.Substring(0, 8) : keyToCheck;
        if (keyToCheck.Length < 8)
        {
            for (int i = keyToCheck.Length; i < 8; i++)
            {
                keyToCheck += DEFAULT_KEY[i];
            }
        }
    }
    private static byte[] HashKey(string key, int length)
    {
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();

        // Hash the key
        byte[] keyBytes = Encoding.UTF8.GetBytes(key);
        byte[] hash = sha1.ComputeHash(keyBytes);

        // Truncate hash
        byte[] truncatedHash = new byte[length];
        Array.Copy(hash, 0, truncatedHash, 0, length);
        return truncatedHash;
    }


Decryption

private const string DEFAULT_KEY = "#kl?+@<z";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }
    protected void txt_String_TextChanged(object sender, EventArgs e)
    {

        string key = "#kl?+@<z";
        string txt = txt_String.Text.Trim();
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream;

        // Check whether the key is valid, otherwise make it valid
        CheckKey(ref key);

        des.Key = HashKey(key, des.KeySize / 8);
        des.IV = HashKey(key, des.KeySize / 8);
        byte[] inputBytes = Convert.FromBase64String(txt);

        cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(), CryptoStreamMode.Write);
        cryptoStream.Write(inputBytes, 0, inputBytes.Length);
        cryptoStream.FlushFinalBlock();

        Encoding encoding = Encoding.UTF8;
        lbl.Text= encoding.GetString(memoryStream.ToArray());
    }

    private static void CheckKey(ref string keyToCheck)
    {
        keyToCheck = keyToCheck.Length > 8 ? keyToCheck.Substring(0, 8) : keyToCheck;
        if (keyToCheck.Length < 8)
        {
            for (int i = keyToCheck.Length; i < 8; i++)
            {
                keyToCheck += DEFAULT_KEY[i];
            }
        }
    }
    private static byte[] HashKey(string key, int length)
    {
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();

        // Hash the key
        byte[] keyBytes = Encoding.UTF8.GetBytes(key);
        byte[] hash = sha1.ComputeHash(keyBytes);

        // Truncate hash
        byte[] truncatedHash = new byte[length];
        Array.Copy(hash, 0, truncatedHash, 0, length);
        return truncatedHash;
    }




Have a nice day... 'N happy Coding :)