Wednesday, December 17, 2008

Encrypting query string

Class for encrypting and Decrypting:------------------------------------------------using System;using System.Data;using System.Configuration;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;using System.Text;using System.Security.Cryptography;using System.IO;
/// /// Summary description for ClassQueryEncrypt/// public class ClassQueryEncrypt{ #region "--Fields--"
private static byte[] key = { };
private static byte[] IV = { 38, 55, 206, 48, 28, 64, 20, 16 };
private static string stringKey = "!5663a#AK";
#endregion
#region "--Public Methods--" public static string Encrypt(string text) {
try {
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Encoding.UTF8.GetBytes(text); MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,des.CreateEncryptor(key, IV), CryptoStreamMode.Write); cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
string retval= Convert.ToBase64String(memoryStream.ToArray()); return retval; }
catch (Exception ex) {
// Handle Exception Here
}

return string.Empty;
}
public static string GetEncryptedQueryString(string QueryStringToBeEncypted) {
string key = Encrypt(QueryStringToBeEncypted);
StringWriter writer = new StringWriter(); HttpContext.Current.Server.UrlEncode(key, writer); return writer.ToString();
}

public static string Decrypt(string text) {
try {
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));

DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Convert.FromBase64String(text);

MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,des.CreateDecryptor(key, IV), CryptoStreamMode.Write);

cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();

return Encoding.UTF8.GetString(memoryStream.ToArray());
}
catch (Exception ex) {
// Handle Exception Here
}

return string.Empty;
}

#endregion

public ClassQueryEncrypt() { // // TODO: Add constructor logic here // }}
--On Redirecting Page-----
Response.Write(ClassQueryEncrypt.GetEncryptedQueryString("122") + "
");
Response.Redirect("EncreptedQuery.aspx?Id=" + ClassQueryEncrypt.GetEncryptedQueryString("122"));
--on Decrypting Page::--
string sId = Request.QueryString["Id"].ToString();
Response.Write(sId + "
Decrepted To:: ");
Response.Write(ClassQueryEncrypt.Decrypt(sId));

javascript while using Script Manager

'--reloads the parent page
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "reload", "window.opener.location.reload();", True)

Thursday, December 11, 2008

Creating errorlogs in Asp.net

Public Sub WriteErrorsInLog(ByVal ErrorString As String)
Dim sDirecPath As String
sDirecPath = "~/ErrorDirectory" 'variable: directory path for error log.
'for checking that directory exists , if not then create it.
If Directory.Exists(HttpContext.Current.Server.MapPath(sDirecPath)) = False Then
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(sDirecPath))
End If
Dim fpath As String 'variable: file path
fpath = Path.Combine(sDirecPath, Format(Today, "yyyyMMdd").ToString() & ".txt")
'for checking file exists or not . if not then create the file and append the file
If File.Exists(HttpContext.Current.Server.MapPath(fpath)) = False Then
File.WriteAllText(HttpContext.Current.Server.MapPath(fpath), " ")
WriteInErrorLogFile(HttpContext.Current.Server.MapPath(fpath), ErrorString)
Else
WriteInErrorLogFile(HttpContext.Current.Server.MapPath(fpath), ErrorString)
End If
End Sub
'''
''' To append the file for entry of errors into the file.
'''

''' path for file location
''' errors in string format
'''
Private Sub WriteInErrorLogFile(ByVal fpath As String, ByVal ErrorString As String)
Dim wr As System.IO.StreamWriter
Try
wr = New System.IO.StreamWriter(fpath, True)
Dim str As String = ""
str = System.DateTime.Now.ToString() & " ---- " & ErrorString
str = str & System.Environment.NewLine
wr.WriteLine(str)
wr.Flush()
wr.Dispose()
Catch ex As Exception
End Try
End Sub

trimming in javascript

function valTxtSiteSearch()
{
var str=document.getElementById(TxtSiteSearchID).value;
tempstr = str.split(' ');
var endIndex = 0;
var startIndex=0;
var lTrimStr='';
var rTrimStr='';
for(i=0;i<=tempstr.length-1;i++)
{
if (lTrimStr=='')
{
if(tempstr[i]!='')
{
lTrimStr +=tempstr[i];
}
}
}
for(i=tempstr.length-1;i>=0;i--)
{
if (rTrimStr=='')
{
if(tempstr[i]!='')
{
rTrimStr +=tempstr[i];
}
}
}
if ( lTrimStr.length==0)
{
alert("Enter search. ");
return false ;
}
else
{
//var startchar = lTrimStr.substring(0,1)
startIndex= str.indexOf(lTrimStr);
lTrimStr=str.substring(startIndex);
}
alert(lTrimStr);
document.getElementById(TxtSiteSearchID).value=lTrimStr
//right trimming
if (rTrimStr.length>0)
{
s=rTrimStr.substr(rTrimStr.length-1);
ss=str.lastIndexOf(s)+1;
endIndex=ss;
//endIndex= str.lastIndexOf(rTrimStr.substr(rTrimStr.length-1)) + 1;
rTrimStr=str.substring(0,endIndex);
}
alert(rTrimStr);
document.getElementById(TxtSiteSearchID).value=rTrimStr
//trimming both left and right.
if (rTrimStr.length>0 && lTrimStr.length>0)
{
str=str.substring(startIndex,endIndex)
}
alert(str);
document.getElementById(TxtSiteSearchID).value=str;
}
function leftTrim(str)
{
tempstr = str.split(' ');
var startIndex=0;
var lTrimStr='';
for(i=0;i<=tempstr.length-1;i++)
{
if (lTrimStr=='')
{
if(tempstr[i]!='')
{
lTrimStr +=tempstr[i];
}
}
}
if ( lTrimStr.length==0)
{
//alert("Enter search. ");
//return false ;
}
else
{
startIndex= str.indexOf(lTrimStr);
lTrimStr=str.substring(startIndex);
}
return lTrimStr;
}
function rightTrim(str)
{
tempstr = str.split(' ');
var endIndex = 0;
var startIndex=0;
var rTrimStr='';
for(i=tempstr.length-1;i>=0;i--)
{
if (rTrimStr=='')
{
if(tempstr[i]!='')
{
rTrimStr +=tempstr[i];
}
}
}
//right trimming
if (rTrimStr.length>0)
{
s=rTrimStr.substr(rTrimStr.length-1);
ss=str.lastIndexOf(s)+1;
endIndex=ss;
rTrimStr=str.substring(0,endIndex);
}
return rTrimStr;
}

Friday, December 5, 2008

Creating ProgressBar in asp.net

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ProgressBar.ascx.vb" Inherits="ProgressBar" %><%@ Control Language="VB" AutoEventWireup="false" CodeFile="ProgressBar.ascx.vb" Inherits="ProgressBar" %>------------------------------------html--------------
  1. <%@ Control Language="VB" AutoEventWireup="false" CodeFile="ProgressBar.ascx.vb" Inherits="ProgressBar" %><%@ Control Language="VB" AutoEventWireup="false" CodeFile="ProgressBar.ascx.vb" Inherits="ProgressBar" %>

    ---------------------------------------------------
    Partial Class ProgressBar Inherits System.Web.UI.UserControl
    Private _colFillColor As Drawing.Color Private _colBackcolor As Drawing.Color Private _colBorderColor As Drawing.Color = Drawing.Color.Black
    Private _intBorder As Integer = 1 Private _intCellspacing As Integer = 1 Private _intCellpadding As Integer = 1 Private _intHeight As Integer = 10 Private _intWidth As Integer = 100
    Private _intBlockNumber As Integer = 5 Private _intValue As Integer = 0 Private _tblBlock As TableRow
    Private _caption As String
    Public Property BGColor() As Drawing.Color Get Return _colBackcolor End Get Set(ByVal value As Drawing.Color) _colBackcolor = value End Set End Property Public Property FillColor() As Drawing.Color Get Return _colFillColor End Get Set(ByVal value As Drawing.Color) _colFillColor = value End Set End Property Public Property BorderColor() As Drawing.Color Get Return _colBorderColor End Get Set(ByVal value As Drawing.Color) _colBorderColor = value End Set End Property Public Property BorderSize() As Integer Get Return _intBorder End Get Set(ByVal value As Integer) _intBorder = value End Set End Property Public Property Cellpadding() As Integer Get Return _intCellpadding End Get Set(ByVal value As Integer) _intCellpadding = value End Set End Property Public Property CellSpacing() As Integer Get Return _intCellspacing End Get Set(ByVal value As Integer) _intCellspacing = value End Set End Property Public Property Blocks() As Integer Get Return _intBlockNumber End Get Set(ByVal value As Integer) _intBlockNumber = value End Set End Property
    Public Property Value() As Integer Get Return _intValue End Get Set(ByVal value As Integer) _intValue = value End Set End Property
    Public Property Caption() As String Get Return _caption End Get Set(ByVal value As String) _caption = value End Set End Property
    Public Property Height() As Integer Get Return _intHeight End Get Set(ByVal value As Integer) _intHeight = value End Set End Property Public Property Width() As Integer Get Return _intWidth End Get Set(ByVal value As Integer) _intWidth = value End Set End Property
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender Dim intBlocks As Integer
    ' add a new row to the table _tblBlock = New TableRow() ' create cells and add to the row For intBlocks = 1 To Me.Blocks Dim tblCell As New TableCell tblCell.Text = " " If intBlocks <= Math.Floor((Me.Value * Me.Blocks / 100)) Then tblCell.BackColor = Me.FillColor End If _tblBlock.Cells.Add(tblCell) Next tblProgressBar.Rows.Add(_tblBlock) 'set the progress bar properties tblProgressBar.CellPadding = Me.Cellpadding tblProgressBar.CellSpacing = Me.CellSpacing tblProgressBar.Width = Me.Width tblProgressBar.Height = Me.Height tblProgressBar.BackColor = Me.BGColor tblProgressBar.BorderColor = Me.BorderColor tblProgressBar.Caption = Me.Caption tblProgressBar.ToolTip = Me.Value & "% " End Sub End Class