Search This Blog

Sunday, August 28, 2011

How to Encrypt Connection String

For security reasons we need to encrypt the connection strings.
Encrypting and decrypting the connection string is very easy. Follow the Below steps to encrypt the Connection strings.
Encrypt Connection Strings:
  • Open visual studio command prompt.
  • type the given command  aspnet_regiis -pef  "connectionStrings"  "YourApplicationFullPath where your web.config file is present"  and then press Enter.


Decrypting Connection String:
  • aspnet_regiis -pdf  "connectionStrings" "YourApplicationFullPath where your web.config file is present"  and then press Enter.


Note : You need to use pef or pdf only if your website is created as a file system. If Your website is created as a IIS website then use pe or pd for encrypting or decrypting.

Thursday, August 18, 2011

is and as operators in C#

is Operator:
  • is operator Checks if an object is compatible with a given type 
  • An is expression evaluates to true if the provided expression is non-null, and the provided object     can  be cast to the provided type without causing  an exception to be thrown.
as Operator:
  • The as operator is used to perform conversions between compatible reference types      
  • The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception
Ex:

public class Exam
{
    public string GetSubjectName()
    {
        return "C#";
    }
}
public class UnitTest
{
    public int GetTestNumber()
    {
        return 1;
    }
}

public partial class CSHARPSAMPLES_Keywords : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Exam e1 = new Exam();
    }
    public void Test(object o)
    {
        if (o is UnitTest)
        {
            UnitTest u = o as UnitTest;
            u.GetTestNumber();
        }
        else if (o is Exam)
        {
            Exam e = o as Exam;
            e.GetSubjectName();
        }
    }
}

Ternary Operators in C#

Ternary operators can be use full when we have only one simple if else condition.
Ternary Operator Syntax:

condition ? first_expression : second_expression;
Ex:
public class TernaryEx
{
    public int GetBigNum(int a, int b)
    {
        return a > b ? a : b;
        //in the above syntax if a is greater than b
        //the above expression returns a else it returns b.
    }
}
Note: SignUp for JobGuaranteed Training