Monday, November 26, 2012

Resolving the Validation of viewstate MAC failed error

I have been receiving the following error on the page when I clicked on the link (in the Gridview) before the Page had fully loaded. My Page has a Gridview which uses DataKeyNames. Additionally it had a lot of Javascript ads which slowed down loading of the Page.

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.





On doing some research, I found out that the due to the use of GridView and the DataKeyNames, the Framework implements a ViewState Encryption Technique for keeping the data secure. Hence, when a link is clicked on a partial page load, the encryption tag might not have been rendered on the page, causing the page to crash.  

I read this excellent post which aptly details the issue and its resolution.

Basically, it appears to be some sort of bug in the existing Framework and the blog posts a workaround.

I used Solution 3 since it still was keeping the Security aspects in place which executing the page elegantly. Basically what it did was to remove the Validation script right before the end of the form tag and place it after the beginning of the Tag. 
protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        System.IO.StringWriter stringWriter =
            new System.IO.StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
        base.Render(htmlWriter);
        string html = stringWriter.ToString();
        string[] aspnet_formelems = new string[5];
        aspnet_formelems[0] = "__EVENTTARGET";
        aspnet_formelems[1] = "__EVENTARGUMENT";
        aspnet_formelems[2] = "__VIEWSTATE";
        aspnet_formelems[3] = "__EVENTVALIDATION";
        aspnet_formelems[4] = "__VIEWSTATEENCRYPTED";
        foreach (string elem in aspnet_formelems)
        {
            //Response.Write("input type=""hidden"" name=""" & abc.ToString & """")
            int StartPoint = html.IndexOf("= 0)
            {
                //does __VIEWSTATE exist?
                int EndPoint = html.IndexOf("/>", StartPoint) + 2;
                string ViewStateInput = html.Substring(StartPoint,
                  EndPoint - StartPoint);
                html = html.Remove(StartPoint, EndPoint - StartPoint);
                int FormStart = html.IndexOf("", FormStart) + 1;
                if (EndForm >= 0)
                    html = html.Insert(EndForm, ViewStateInput);
            }
        }

        writer.Write(html);
    }

Friday, October 12, 2012

Hiding Tabs(MenuItem) in asp:Menu

I was working on the asp:Menu where I have to hide a certain Tab if there is no content for it. This is what I did. I knew the index of the Tab that Had to be removed:

//tabs_Menu is the id for asp:Menu
MenuItemCollection menuitems=tabs_Menu.Items; 
menuitems.Remove(tabs_Menu.Items[1]);

Monday, July 16, 2012

Finding Duplicates and copying Excel formula to the end of data

I have an excel file that has about 100,000 records. I wanted to find out the duplicates in a particular column before removing them. So I had to mark the rows with something as an identifier. Below is what I found for the formula. So essentially if the duplicates are in Column A, this formula should be placed in an empty column(or a new column):

=IF(COUNTIF(A:A, A1)>1, "Duplicate !","")

I had to apply this formula to all the 100,000 records, and someone over the internet posted a nice example of doing this:

http://www.handyexceltips.com/2008/01/16/dragging-a-formula-down/

Very useful in those time crunches !

Thursday, June 7, 2012

Batch Update using SQL

Though this is a fairly easy process, its worth mentioning here since it is something which cannot be thought about. Using top x in your query, you could basically choose how many records need to be updated. This is helpful for processing the records as required
UPDATE top (100)  TableName 
SET
    column1 = getdate(),
    remarks = 'batch for API Upload'
 where column2 is not null and column1 > '2012-1-1'

Monday, June 4, 2012

Extract Date from DateTime Field in SQL

I was working on SQL to create a view to get the reports for our subscribers and group them by day. This is fairly easy to implement in .NET, but I have been licking SQL candies for a while and so wanted to try it out. This is how I did:
 SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
I could have used varchar converstion and "trimmed" the time value, but I had to further sort the resultset based upon the date obtained, so it was important for me to have it as a date type.

Saturday, May 12, 2012

Using Trim() in LINQ to SQL on an Empty string

This is kind of a silly thing I did, now that I think about it, but I must admit I was ignorant.

I didnt realize that you don't necessarily need a string utility to "Trim()" an Empty string of whitespaces, becuause, well, it doesn't matter.

To SQL, ' ' is same as '          '.

What I realized when using Trim() function, was this function was adding an overload of ltrim(rtrim(emptystringfield)) to my already resource intensive query, which in effect was increasing my query execution time. So use Trim() only when you need it, especially when using expensive queries.

Wednesday, April 18, 2012

Searching for numeric character(s) in Column of a Table in SQL

For Reference:

select * from [YourTable] where 
patindex('%[0-9]%',[yourcolumn]) <> 0

That was kinda easy!

Friday, March 23, 2012

Visual Studio Themes

I tweaked my IDE to have a black background v/s the default white, and also changed some other color schemes to have contrasting colors, but here's a website that gives you a good amount of cool themes to apply to. Check out the link below. I am going to try them soon!

http://www.hanselman.com/blog/VisualStudioProgrammerThemesGallery.aspx

Wednesday, March 7, 2012

Deleting duplicate records using SQL

I have been working on SQL lately and its worth mentioning in the blog as to how easily find and delete duplicate records in the database.

Here's a nice solution posted by Pinal Dave on his blog:

http://blog.sqlauthority.com/2007/03/01/sql-server-delete-duplicate-records-rows/


NJoy!

Tuesday, February 21, 2012

Maintaining Scrollbar postion of listBox inside of UpdatePanel

I have this multiple selected ListBox where I would want to maintain the scroll position of the ListBox. This is how I did, following the instructions from the blog post here

Please make sure this code is after the ScriptManager tag on the page: