Friday, September 14, 2007

Web.Config Encryption and decryption

We are providing the connection strings in the web configuration file, in our connection string we are providing all the information of our database server like server name, user name, password etc. this information can be easily viewed by other users, so we required to encrpty that information.
I am providing a little code for encrytpion and decryption of web.config file.

In web config file my connection string is looks like

and my Encryption and Decryption function is

public void EncryptConfig(bool IsEncrypt)
string path = "/tipsandtrics";

// your application name

Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
ConfigurationSection appSettings = config.GetSection("connectionStrings");

if (IsEncrypt)

now just call this function if you want to encrypt connection string like


and for decryption


that's it..

here i used "DataProtectionConfigurationProvider" encryption method, for more methods of encryption, please refer msdn site.

Highlight Row in gridview

Following is the code, which will highligh the row, when user will click on any row.
logic for this is very simple.
I created a one javascript function,which will finds the row and changes the color of that row.
I called javascript function from row created event of the gridview.

the code for this as follows.

my HTML Code is ...

1. Javascript Function

function ChangeRowColor(rowID)
var count=document.form1.rowCount.value;
var i=0;
for(i=0;i {
var id="GridView1_row"+i;
document.getElementById(id).style.backgroundColor = '#8080FF';
document.getElementById(id).style.backgroundColor = 'white';


2. My Gridview
asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_RowCreated"

input type="hidden" id="rowCount" runat="server" /

and my code behind is as

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class highlight : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("guid");
DataColumn dc1 = new DataColumn("UserName");
DataColumn dc2 = new DataColumn("password");
for (int i = 0; i < 5; i++)
DataRow dr = dt.NewRow();
dr["guid"] = i;
dr["UserName"] = "abc" + i.ToString();
dr["password"] = "pwd" + i.ToString();

GridView1.DataSource = dt;

rowCount.Value = dt.Rows.Count.ToString();
// storing the number of rows in the grid in hidden field
// this is used in javascript


protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
string rowID = String.Empty;

if (e.Row.RowType == DataControlRowType.DataRow)
e.Row.ID = "row" + e.Row.RowIndex;
rowID = GridView1.ID + "_row" + e.Row.RowIndex;

//if for each gridview row id is not created, then use this
//e.Row.Attributes.Add("id", GridView1.ID + "row" + e.Row.RowIndex);

e.Row.Attributes.Add("onclick", "ChangeRowColor(" + "'" + rowID + "'" + ")");