Search

Sep 30, 2008

Error: Validation of viewstate MAC failed

This is common error generally working with UpdatePanel.

On your screen you see message "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."

You can fix it in two ways, setting in web.config or in page it self.

In web.config:

<pages enableEventValidation="false" viewStateEncryptionMode ="Never" />



Or in page you write following line in @Page directive.




<%@ Page EnableEventValidation="false" ViewStateEncryptionMode="never" ...

Sep 25, 2008

Silverlight 2 Release Candidate Now Available

Hello All,

Silverlight 2 is now available, please read this post of Scott Guthre

Sep 24, 2008

with operator in JavaScript

Hello Friends,

I found one good thing in JavaScript, there is with operator in JavaScript, which you can say same as with operator in VB.NET. Its always helpful to define short name of big variable or replacement of such big variable; which lead us to make code more readable and maintain too.

In general when we are dealing with XML datasrouce in JavaScript; there always have long long variable name, of the value is so deep inside.

xmlDoc.childNodes[1].childNodes[0].childNodes[3].childNodes[0].text
xmlDoc.childNodes[1].childNodes[0].childNodes[4].childNodes[0].text

We have to write more lines of code to get all the details. Here we can use with, as we know part of path to reach final value is common which is xmlDoc.childNodes[1].childNodes[0], so we can put it into with keyword.

with(xmlDoc.childNodes[i].childNodes[0])
{
name = childNodes[1].childNodes[0].text;
add1 = childNodes[2].childNodes[0].text;
add2 = childNodes[3].childNodes[0].text;
email = childNodes[4].childNodes[0].text;
.
.
.
}

Sep 22, 2008

How to get Property list only of Derived class?

Hi All,

Recently I seen one requirement on ASP.Net Forum, some one posted question about How to get property list of Derived class.

Generally if we want to get all the property of class we use GetProperties() method. Which gives you the PropertyInfo[]. Lets see with example. We have following class Hierarchy.

class A
{
public int Id { get; set; }

public virtual void PrintMe()
{
Console.WriteLine("Id : " + Id);
}

}

class B : A
{
public string Name { get; set; }
public string Address { get; set; }
public override void PrintMe()
{
base.PrintMe();
Console.WriteLine("Name : " + Name);
}
}



In A there is only one property and in B we have 2 more property, our goal to get only two property declared in class B. Lets check with GetProperties().




PropertyInfo[] properties = typeof(B).GetProperties(); 


Console.WriteLine(properties.Count()); //will print 3



You can see its printing 3, that means it consider properties which are in parent and child too. There is one overload of GetProperties() which has BindingFlags, but with that also we are not achieving our goal.



So here is the solution using LINQ.




var prop =
from
p in typeof(B).GetProperties()
where p.DeclaringType == typeof(B)
select p;

Console.WriteLine(prop.Count()); //Will print 2



We are filtering on DeclaringType; which should be typeof(B). You can see now its returning 2, that what we needed.

Sep 21, 2008

Windows live writer for server 2003

Hello All,

This post is related to people who are creating blog using Window Live Writer.

It took me long to get the Windows Live Writer for Server 2003. Here is the link to download the Windows Live Writer for Server 2003

Sep 18, 2008

User Group Meeting on 20-Sep-2008

Hi All,

This is to announce that there is next meeting of Ahmedabad_SQLServer_UserGroup on Saturday, September 20, 2008.

I would like you to join this and meeting inperson and gain lots of knowledge shared by MVPs, there always a QA session at the end of each meeting followed by ICE-CREAM.

The topic of this meeting will be CTE and Pinal Dave [MVP] will be the speaker of this meeting.

To Register: Click Here.
To View Meeting Details : Click Here.
Not member of this group?: Click here

In previous meeting we explore the SQL Server Transaction Isolation Levels, which was presented by Jacob Sebastian [MVP, MSDN moderator, Owner of groups, and lots more], that was wonderful session along with live demos. If you miss that event then you can go here

Visual Studio IDE Support for SQL Server

Hello,

As we all know Visual Studio is now supporting Database project, which allows you to write query, procedure etc... from IDE only.

Recently I found the error while connecting SQL2008 from VS2008.

This server version is not supported. Only servers up to Microsoft SQL Server 2005 are supported.

I found after doing some googling; that there is no patch available which allos me to connect SQL2008 to VS2008!!!.

I found few comments which says that it will come soon.

Somasegar's WebLog
Euan Garden's BLOG

However there is Sservice Pack for Visual Studio 2008 Team Foundation Server which has ability to Support for SQL Server 2008, you can download that form Microsoft Download Center

Let me add that there is Service Pack for Visual Studio 2005 Support for SQL Server 2008, which you can download it from Microsoft Download Center

Sep 16, 2008

Reducing page load times with UpdatePanels and timers

Hi all,

Some time we have requirement like, on single page we are having 5-6 controls, which at the same tiem only single is visible and by navigation we can view all other controls.

This is very specific to Ajax Tab Implementation. Where we are having 4-5 tabs, in which we are having server controls, so on Page load all the controls get loaded and page become heavy also slow.

There is one technique to optimize this. As we all know we have to show single tab once page is get loaded, put all the controls into Asp:Panel and except one which we have to show on default; make other to visible false. The enable of all visible panel will be done using timer control. Time control will make enable the Asp:Panel one by one. As all the Asp:Panel will be in UpdatePanel will page will not get refreshed.

I found the same code in one of the post of Glavs Blog.

He provides the code along with video. Have a look at them and make optimize your page. I used this and found very good, even we can now use this on same page.

Sep 14, 2008

Why doesn't C# support multiple inheritance?

Hi all,

I was always searching for the answer of "Why doesn't C# support multiple inheritance?".

After some googling I found the answer from MSDN.

Please have a look at the link.

Sep 10, 2008

Finding occurrences of character in string [LINQ]

Hello All,

This is normal requirement when you need to find the occurrence of single character with in the string. Here is the example which uses LINQ to achive this.

string strString = "AA BRA KA DABRA";

var grp = from c in strString.ToCharArray()
group c by c into m
select new { Key = m.Key, Count = m.Count() };

foreach (var item in grp)
{
Console.WriteLine(
string.Format("Character:{0} Appears {1} times",
item.Key.ToString(), item.Count));
}

Sep 9, 2008

Extended URL rewriting

I was having one requirement for my .net application. In which I want to parse the following url. Handling request with out extension.

http://mysite.com/imran/
http://mysite.com/armaan/

For these url we can't user ASP.NET URL Re-Writing, as there is no aspx extension added to this url.

When you found such request and its not exists then IIS will give 404 error, but we need to parse such url and do some validation and give them proper output. In this case lets say I would like to see the profile of user imran, then I make request to http://mysite.com/imran/ which gives me the profile of user imran.

This is the normal requirement, we can do it easily in php and java. But in .net this is bit tricky to get such functionality working.

If we use http handler, then also we are not able to identify the request as it has no extension. As its is not reaching to the application, IIS itself reject this request, no global.ascx or web.config

I found very easy solution for this solution. This requires some setting on virtual directory and code in our system to handle such request.

As I mention eariler when you send request to http://mysite.com/imran/ which not exist; IIS will raise 404 error, what we do is, we trap this error and get the requested page or url and do some work and give the result.

IIS Setting
In IIS we have to change default 404 error handling to aspx page, bye default 404 page is C:\WINDOWS\help\iisHelp\common\404b.htm with Message Type as File. See the image



We need to change this to forward all such request to our application, once we got control in aspx page then we can do anything on such request.

For this we need to change Message Type, set it to URL and the URL will be /MySite/Handle404.aspx. See the image



Code
We have to write few lines of code in Handle404.aspx to handel this request.

protected override void OnInit(EventArgs e)
{

if (Request.Url.ToString().Contains("404;"))
{
string[] urlInfo404 = Request.Url.Query.ToString().Split(';');

if (urlInfo404.Length > 1)
{
string strRequestUrl = urlInfo404[1].Replace(":" + Request.Url.Port + "/", "/");

if (!strRequestUrl.EndsWith("/"))
{
strRequestUrl = strRequestUrl.Insert(strRequestUrl.Length, "/");
Response.Redirect(strRequestUrl);
}
string[] urlInfoFavAddress = strRequestUrl.Split('/');
string strUser = urlInfoFavAddress[urlInfoFavAddress.Length - 2];

Server.Transfer(string.Concat("~/Profile.aspx?usr=", strUser));

}
}
base.OnInit(e);
}
Here we are just grabbing the trailing string, in this case its imran or armaan.

Once I get the key its get redirected to Profile page, as its Server.Transfer which does not change the url.

Sep 5, 2008

Load method of DataTable

I found very interesting method of DataTable. There is Load method which loads the IDataReader directly to DataTable. Well this is not interestring I know lolz.

The case is like....

My query or procedure generates 4 different result set, and I have 4 different DataTable. As IDataReader has 4 result set, fatching all result set I have to use NextResult method which put my reader to next result set. like this. [Dont know this??? Have a look at this.]

DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
DataTable dt3 = new DataTable();
DataTable dt4 = new DataTable();
DataTable dt5 = new DataTable();

using (SqlConnection con = new SqlConnection("CONNECTION_STRING"))
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from TABLE1;select * from TABLE1;select * from TABLE2;select * from TABLE3";

con.Open();
IDataReader idr = cmd.ExecuteReader();

dt1.Load(idr);
idr.NextResult(); // Put DataReader to next result set
dt2.Load(idr);
idr.NextResult(); // Put DataReader to next result set
dt3.Load(idr);
idr.NextResult(); // Put DataReader to next result set. ERROR
dt4.Load(idr);
}

But this get fails at NextResult, why?

Because when you call Load method on DataTable, it automatical set pointer to next result set, we dont need to call NextResult here.

This is interesting thing, lets add few more on it. First we remove the NextResult method.

DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
DataTable dt3 = new DataTable();
DataTable dt4 = new DataTable();
DataTable dt5 = new DataTable();

using (SqlConnection con = new SqlConnection("CONNECTION_STRING"))
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from TABLE1;select * from TABLE2;select * from TABLE3;select * from TABLE4";

con.Open();
IDataReader idr = cmd.ExecuteReader();

dt1.Load(idr);
dt2.Load(idr);
dt3.Load(idr);
dt4.Load(idr);
dt5.Load(idr);
}


Well this is working fine, can you see the dt5? I am trying to load 5th result set, but there is no such result set produced by my Sql statements, its still working!!!!, this is really interesting. Well its not succesfull operation; but its will make empty DataTable.

Sep 3, 2008

Make temporary redirection to another page

Hello All,

I got one question is asp.net forums, some time people require to change the location automatically or the best example is like, when you shifted your site to new location and you dont want people [who bookmarked your site] request to old site locaiton and get page not found error!

This case what you can do is, you can add automatic redirection to the new site, just adding the meta tag.

You can create one html file or you can write meta tag in older site's default page, here is the meta tag, you just have to place in head tag

<meta http-equiv="refresh" content="5;url='http://knowledgebaseworld.blogspot.com/'" />

http-equiv is the command, which says refresh, so it have to do refresh, and in the content we have to set new locatoin and the time in second. Here its 5 so after 5 second it will automatically redirected to http://knowledgebaseworld.blogspot.com.

We can put some thing like, the page you are looking for its moved, please wait, we are redirecting to new page, book mark this page bla bla bla.....

This way we stop loosing our valuable users of site.

Creating Rad Menu using LINQ

Hello all,

I created sample code to generate Dynamic RadMenu with use of LINQ. The controls like RadMenu, AspMenu which accept XML as the datasource to generate the output.

I use LINQ to generate XML, you can read here to generate XML using LINQ.

You can find the simple project at Code Project. You can modify the code with your use, its dynamically creates the node also it uses the recursion to generate n level menu item.