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)
{
appSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
else
{
appSettings.SectionInformation.ProtectionProvider.Name.ToString();
appSettings.SectionInformation.UnprotectSection();
}
config.Save();
}

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

EncryptConfig(true);

and for decryption

EncryptConfig(false);

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;
if(id==rowID)
{
document.getElementById(id).style.backgroundColor = '#8080FF';
}
else
{
document.getElementById(id).style.backgroundColor = 'white';
}
}

}

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

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");
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["guid"] = i;
dr["UserName"] = "abc" + i.ToString();
dr["password"] = "pwd" + i.ToString();
dt.Rows.Add(dr);
}

GridView1.DataSource = dt;
GridView1.DataBind();

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 + "'" + ")");

}
}
}

Sunday, August 19, 2007

Gird View Sorting and Paging


Sorting of Gridview

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds=bindata();
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
private DataSet bindata()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["deoConnectionString1"].ToString());
con.Open();
string cmd = "select id,name, employee.desgid,designation, employee.deptid,deptname,address from employee left outer join" +
" dept on (employee.deptid=dept.deptid)" +
" left outer join " +
" designation on (employee.desgid=designation.desgid)";
SqlDataAdapter da = new SqlDataAdapter(cmd,con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataView dv = sortedView(false);
GridView1.DataSource = dv;
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["col"] = e.SortExpression;
DataView dv= sortedView(true);
GridView1.DataSource = dv;
GridView1.DataBind();
}
private DataView sortedView(Boolean isSort)
{
DataTable dt = bindata().Tables[0];
DataView dv = new DataView(dt);
if (ViewState["order"] == null ViewState["order"].ToString() == "asc")
{
if (isSort)
{
dv.Sort = ViewState["col"].ToString() + " Desc";
ViewState["order"] = "desc";
}
else
{
if(ViewState["order"] != null)
dv.Sort = ViewState["col"].ToString() + " asc";
}
}
else
{
if (isSort)
{
dv.Sort = ViewState["col"].ToString() + " asc";
ViewState["order"] = "asc";
}
else
{
if(ViewState["order"] != null)
dv.Sort = ViewState["col"].ToString() + " desc";
}
}
return dv;
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
DataView dv = sortedView(false);
GridView1.DataSource = dv;
GridView1.DataBind();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataView dv = sortedView(false);
GridView1.DataSource = dv;
GridView1.DataBind();
}

this code will sort the gridview by ascending and desceding order.