Pass encrypted querystring

You need to download the following files on your project bin folder which will help to generate the encrypted queryString.
TSHAK.Components

after downloading the file you have to change the .doc extension to .dll extension. WordPress does not allow user to upload certain extension of file. If the file does not work after changing the extension, feel free to contact with me.

The main page code for redirect will be the following code:

Byte[] bt = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 8 };
TSHAK.Components.SecureQueryString qs = new TSHAK.Components.SecureQueryString(bt);
qs["Myname"] = "John";
qs["MyAge"] = "5";
qs["textBoxValue"] = this.txtQuery.Text.Trim();
Response.Redirect("QueryString2.aspx?data=" + HttpUtility.UrlEncode(qs.ToString()));

To catch the Querystring you have to use the following code, which will extract the exact value from QueryString


Byte[] bt = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 8 };
        TSHAK.Components.SecureQueryString qs = new TSHAK.Components.SecureQueryString(bt,Request["data"]);

        Response.Write("Orginal :" + Request["data"].ToString());
        Response.Write("");

        Response.Write("My Name" + qs["MyName"].ToString());
        Response.Write("");

        Response.Write("My age" + qs["MyAge"].ToString());
        Response.Write("");
        if (qs["textBoxValue"] != null)
        {
            Response.Write("My Text Box" + qs["textBoxValue"].ToString());
            Response.Write("");
        }

Represent IIS log in nice presentable way

I needed to represent the IIS log in nice, presentable way in short time. I go through few and find Analog is a easy way to do the work. But the UI of analog log view is not the one I was looking for. “Awstats” is also a good open source option for presenting iis logs, but configuring that require time. Finally, I found this site
http://www.iislogs.com/steveschofield/log-analsyis-software-for-iis and found “SmarterTools” has the fastest solution for my work. Although, for some degree of use you have to pay certain amount of money, But in you require it to use in a single server…you probably don’t have to worry about paying (see the payment section before download). The setup of this is easy and they have awesome UI. I can also access the log from web :) . So if you need to analyse your visitor’s data quick and nice way…you can think about giving this nice tool a try. It will take maximum 1 hour to download and setup your site for IIS logging.In addition to more than 30 reports that they have already for you, you can make your own custom reports within a minute :) . Click on the link below, to know more about this tool:

IIS Web Log Analysis

Forcing client browser to refresh js and css file

Forcing a refresh

But how do you tell a web browser to grab a new version of the CSS script after you make an update to it? Yes you could rename the CSS file each time you update it, but that is silly. Not only would you need to ensure you update all of the references to the script in your html, but it just isn’t necessary. Simply add a querystring element to the file name in the html reference like so:

<link rel="stylesheet" type="text/css" href="lightbox.css?v=2" media="all" />

Here I added a ‘?v=2′ string to the end of the file name but it can be a datestamp or whatever you want basically. This is the equivalent to changing the the file name, without the need to change the physical file name. The web browser just interprets it as this script (the lightbox.css in this case) as needing to process the v=2 you are passing through the querystring. Since your CSS isn’t actually doing anything with the query values the script doesn’t care if the querystring is present. The only thing that matters is the browser thinks something is different.

So for instance, each time you update a .css or .js script you could change the querystring to increment like v=3, v=4, etc. This also allows you to update the scripts you have on your production website and see the changes (by performing a shift+reload on your browser) while silently keeping the changes from showing up for your other visitors until you change or add the querystring value; since most browsers will cache the CSS file your site visitors would not notice the change unless until you update the querystring.

reference: http://www.unc.edu/~pgale/information-technology/blog/4

Configuring ELMAH on IIS7

To configure elmah on iis7, read this post first:

http://code.google.com/p/elmah/wiki/DotNetSlackersArticle

If setting does not work… copy and paste these lines in right place


<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</modules>
<handlers>
<add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</handlers>
<system.webServer>

Looping through all control in one page

This code will help you to loop through all control and child control in one page. To call the Following function you can use something like SetTextBoxBackColor(this.Page,Color.Yellow) in your code behind.

 private void SetTextBoxBackColor(Control Page, Color clr)
    {
        foreach (Control ctrl in Page.Controls)
        {
            if (ctrl is TextBox)
            {
                ((TextBox)(ctrl)).BackColor = clr;
            }
            else
            {
                if (ctrl.Controls.Count > 0)
                {
                    SetTextBoxBackColor(ctrl, clr);
                }
            }
        }
    }

Reference:

http://steveorr.net/faq/controltreerecursion.aspx

Request format is invalid: application/json; charset=utf-8

System.InvalidOperationException: Request format is invalid: application/json; charset=utf-8.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

it seems the server rejects application/json; charset=utf-8 as content type.

Today I faced the above problem, when I migrated my asp.net 3.5 application to asp.net 4.0 application. My web.config was simple and I copied the web.config content from a new asp.net 4.0 template. I searched for this solution and didn’t find good post to solve my issue. Finally, I have to change my web.config to solve the issue. If you ever face this problem all you have to do is, add the following line under <system.webServer > < handlers>

<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

Happy Coding
Ashfaq

Integrate Bangla in ASP.NET website

I know this is a very simple task for a lot of people. But I had been looking for the easiest way to do this task for long. Youtube helped me to finding the easiest way to this task. So I am not gonna write a lot of text. I will just paste the you tube link. And then few lines of code which will provide the nice way to change from english to bangla(Both Way). Watch the youtube video first:
Localization in asp.net

My IIS/VS2008 didn’t have bn-BD, so I worked with bn-IN culture. There are way to use bn_BD in iis 5.1, but it is easy on IIS-7. I am not going details on that.

Suppose you have file name “Contact.aspx”, so you have to have Contact.aspx.resx and Contact.aspx.bn-IN.resx in App_Localresources folder.

I have a dropdown in my .aspx page which has en-US for english and bn-IN for Bangla, changing the dropdown will call IntializeCulture function. If you have set up your browser according to instruction from youtube. You will be able to see bangla. I am sure using this loginc you will be able to change your page as per your requirement.

  protected override void InitializeCulture()
    {
        if (Request.Form["DropDownList1"] != null)
        {

            string selectedLanguage = Request.Form["DropDownList1"];
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
        }
        base.InitializeCulture();
    }

Change to .rar after download(Right click to download): Bangla Solution File

Delete all foreign key refernce for a Table using one T-SQL statement

I was looking for a t-sql query which will delete all foreign key reference for a Table. My table has referenced by 13 different table. I needed to Truncate the Table named Table_A. Suppose I was Truncating Table_A, which was referenced by 13 table. So I ran the T-SQL query first to list all reference for Table_A. Then I used the 2nd Query which would delete all reference from Table_A. Once the foreign key reference has been removed from the table, You can easily run a Truncate command to restart the index of Primary column. After Truncate, I used the list generated by first query to re-establish the reference.

Here is the first Query which will list all the reference that is used in your Database or for a Specific Table.


SELECT RC.CONSTRAINT_NAME FK_Name
, KF.TABLE_SCHEMA FK_Schema
, KF.TABLE_NAME FK_Table
, KF.COLUMN_NAME FK_Column
, RC.UNIQUE_CONSTRAINT_NAME PK_Name
, KP.TABLE_SCHEMA PK_Schema
, KP.TABLE_NAME PK_Table
, KP.COLUMN_NAME PK_Column
, RC.MATCH_OPTION MatchOption
, RC.UPDATE_RULE UpdateRule
, RC.DELETE_RULE DeleteRule
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KF ON RC.CONSTRAINT_NAME = KF.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KP ON RC.UNIQUE_CONSTRAINT_NAME = KP.CONSTRAINT_NAME
Where KP.TABLE_NAME ='Table_A'


Then I used the second query to delete reference from Table_A

DECLARE @database nvarchar(50)
DECLARE @table nvarchar(50)
set @database = 'DBNAME'
set @table = 'TABLE_A'
declare @schema nvarchar(128), @tbl nvarchar(128), @constraint nvarchar(128)
DECLARE @sql nvarchar(255)
declare cur cursor fast_forward for
select distinct cu.constraint_schema, cu.table_name, cu.constraint_name
from information_schema.table_constraints tc
join information_schema.referential_constraints rc on rc.unique_constraint_name = tc.constraint_name
join information_schema.constraint_column_usage cu on cu.constraint_name = rc.constraint_name
where tc.constraint_catalog = @database and tc.table_name = @table
open cur
fetch next from cur into @schema, @tbl, @constraint
while @@fetch_status  -1
begin
select @sql = 'ALTER TABLE ' + @schema + '.' + @tbl + ' DROP CONSTRAINT ' + @constraint
exec sp_executesql @sql
fetch next from cur into @schema, @tbl, @constraint
end
close cur
deallocate cur 

T-SQL For Folder Path

This is a excellent query to find out out Folder Path, or Subordinate under Manager using SQL Temporary table concept. It can save lot of hard T-SQL query work if needed.


WITH  RecFolder
        AS (
            SELECT  FolderID, [FolderName], ParentID,
            CAST(([FolderName]) AS VARCHAR(1000)) AS "Path",  CAST((FolderID) AS VARCHAR(1000)) AS "PathID"
            FROM    DOC_TB_PrmDocFolder
            WHERE   ParentId IS NULL
            UNION ALL
                      --recursive member
            SELECT  t.FolderID, t.[FolderName], t.ParentID,
            CAST((a.path + '/' + t.FolderName) AS VARCHAR(1000)) AS "Path",
            CAST((isnull(a.PathID,'') + '/' + convert(varchar,t.FolderID) ) AS VARCHAR(1000)) AS "PathID"
            FROM    DOC_TB_PrmDocFolder AS t
            JOIN RecFolder AS a
            ON t.ParentId = a.FolderID
        )
        SELECT * FROM RecFolder Where ParentID is not null

Class that easily encrypt/decrypt string

The following class is used to encrypt and decrypt a string. It is useful in lot of cases and should be put in a separate class in the system so that user can easily reference the class and do desired work.

using System.Web.Security;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;

public class Crypt
{

string myKey;
TripleDESCryptoServiceProvider cryptDES3 = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider cryptMD5Hash = new MD5CryptoServiceProvider();

public Crypt()
{

myKey = "somekeyhere";
}

private string Decrypt(string myString)
{
cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey));
cryptDES3.Mode = CipherMode.ECB;
ICryptoTransform desdencrypt = cryptDES3.CreateDecryptor();
byte[] buff = Convert.FromBase64String(myString);
return ASCIIEncoding.ASCII.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
}

private string Encrypt(string myString)
{
cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey));
cryptDES3.Mode = CipherMode.ECB;
ICryptoTransform desdencrypt = cryptDES3.CreateEncryptor();
var MyASCIIEncoding = new ASCIIEncoding();
byte[] buff = ASCIIEncoding.ASCII.GetBytes(testo);
return Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
}

}

Follow

Get every new post delivered to your Inbox.