Disabling Change Tracking in Entity Framework

This might come handy in case, you are having performance issue with Entity Framework and Change Tracking Option.

Short version :

using (SchoolEntities context = new SchoolEntities())
{
 context.Course.MergeOption = MergeOption.NoTracking;

 foreach (var course in context.Course)
 {
 // the courses we enumerate are detached state
 Console.WriteLine("{0} {1}", course.CourseID, course.Title);
 }
}
using (SchoolEntities context = new SchoolEntities())
{ 
 var query = new ObjectQuery<Course>(
 "SELECT VALUE c FROM SchoolEntities.Course AS c", 
 context, 
 MergeOption.NoTracking);

 foreach (var course in query)
 {
 // the courses we enumerate are detached state
 Console.WriteLine("{0} {1}", course.CourseID, course.Title);
 }
}

Thanks to Gil Fink’s for this post.

Read the whole article from : http://blogs.microsoft.co.il/blogs/gilf/archive/2009/02/20/disabling-change-tracking-in-entity-framework.aspx

 

Exception: Collection was modified; enumeration operation may not execute.

It’s been a while that i didn’t get this Exception, while working, and yesterday, while I was performing some test on a new module that I implemented i got this exception. I knew i did get it sometimes back last year, and managed to solve it. So here is one solution when you are dealing with this kind of issue:

[csharp]

IList<Product> productList = new List<Product>();
productList.Add(new Product(“Some product 1”);
productList.Add(new Product(“Some product 2”);
productList.Add(new Product(“Some product 3”);
productList.Add(new Product(“Some product 4”);

[/csharp]

[csharp]

for(int i=(productList.Count -1); i >= 0; i–)
{
//Perform Edit, Update, Delete Operation using for .. i loop in reverse order
}

[/csharp]

There is a list of other solutions that can be found on the following websites:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=372532&SiteID=1
http://forums.asp.net/p/1147145/1861164.aspx
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21585534.html

Paging large result sets with SQL query

I’ve been searching on the internet for several ways to perform paging on a large result set from an SQL Query, there are several solutions, that can be found some are custom and others are already provided with ASP.NET Controls. This is the solution i have been using for performing paging of large datasets for data that is being used on a current web 2.0 site.

Here is a common problem:
A website offering search capabilities for it’s users is having performance issues while getting the result from it’s database, they have more than 100K record, and users are able to perform criteria search or just a text search on it. Criteria search can take up a lot of time to process depending on how it has been implemented. Statistics have shown that the average result count is about 1000 record for text search and 500 for criteria search.

The Stored Procedure
Let’s take this SQL Query as a sample query to get result for a typical text search.

SELECT Id, ProductName, ProductDescription, ProductRating, ProductPrice
FROM tbl_Product
WHERE
ProductName LIKE '%' + @Query + '%'
OR
ProductDescription LIKE '%' + @Query + '%'

This stored procedure takes one parameter a query ( @Query ), that the website visitor types in to search for something. And the query search both the Product Name and Product Description Field. This may result in some huge result set for some key terms.

We need to optimize this query to be able to perform custom paging on the other side.

here is how the optimized code would be:

--Will calculate the number of records of this particular request
SELECT @RecordCount = COUNT(1)
FROM
tbl_Product
WHERE
ProductName LIKE '%' + @Query + '%'
OR
ProductDescription LIKE '%' + @Query + '%'

SELECT *
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY Id ASC) AS Row, Id, ProductName, ProductDescription, ProductRating, ProductPrice
FROM
tbl_Product
WHERE
ProductName LIKE '%' + @Query + '%'
OR
ProductDescription LIKE '%' + @Query + '%'
) AS TmpTbl
WHERE Row BETWEEN @RowStart AND @RowEnd
ORDER BY ProductRating

I have added 2 input parameters which are the @RowStart and @RownEnd, and 1 output parameter which is @RecordCount. With these new parameters added you will be able to lauch a query based on the number of records you want to display on a particular page. If you want to display 10 records per page your input parameters will have the following values
@RowStart = 1
@RowEnd = 10
since the query is inclusive.

You can use your prefered data control to display the data, and to build up the Paging logic in ASP.NET

Padding is invalid and cannot be removed.

Stop Google crawling WebResource.axd & ScriptResource.axd
As an ASP.NET developer i often get error message: “Padding is invalid and cannot be removed”. It’s a pretty annoying message that i have been trying to get rid of for days. It was caused by Google trying to index, crawl my WebResource.axd and session. But when the session expires you get this error message. Since Google caches the pages it visits the session on this page has already expired after it is crawled, when it tries to crawl the page again and request the WebResource.axd or ScriptResource.axd with an old key, an exception is raised.

Therefore to solve this problem the simple solution for that is to modify your robots.txt file in your root directory

and add the following at the end

Disallow: /ScriptResource.axd
Disallow:/WebResource.axd

With this no more issues regarding invalid padding.

If this does not solve your issue you can take a look at what other users propose here.

Some update on this issue, i found out while googling some days back:

On this post you can find out how to compress the webresource.axd and also somewhere in between prevent this error to occur.
http://mironabramson.com/blog/post/2007/10/New–Shiny–WebResourceaxd-compression-Module.aspx

Use the tool found at the website below to generate a machine key and a decryption key:
http://www.orcsweb.com/articles/aspnetmachinekey.aspx

Retrieving anchor value from URL

Lately i have been trying to get a grasp of the anchor value from the URL using ASP.NET. From what i learned from the forum (This might be wrong since there is a way in PHP(http://www.php.net/manual/en/function.parse-url.php) to get the anchor value.)

Case study:

e.g http://www.somesamplebigsite.com/product.aspx?q=123&u=09#description
The url above send the user to a page product and then your browser interpret the anchor and scroll down way to the description anchor. But the question is why the heck would i want to get the anchor since it’s just an anchor ? There might be several reasons for that. Here is 2 of them:

  • You might not want to pass an extra parameter in your URL query string because of Referencing issues
  • You would like to perform an action when the page reaches the anchor: Example open an AJAX popup or load some information or Execute a JavaScript in the Description Section of the product.

There might be several other reasons, but these two give you and idea of what is to be achieved.

You can’t get access to the anchor value using server side ASP.NET, it will neither be seen in the Request.Url nor the Request.RawUrl. Therefore those who are trying in vain to get access to this information, don’t bother anymore. You can just use a simple javascript that will do that, and then if you want afterward to either make a new submit to the server or do it in AJAX.

var anchorValue;
var url = document.location;
var strippedUrl = url.toString().split("#");
if(strippedUrl.length > 1)
anchorvalue = strippedUrl[1];

With these few lines of javascript you will be able to determine the value anchor that has been sent in your URL.

Fiddler – HTTP Debugging Proxy

This is a quiet nice tool, that i have been using for sometimes now.

What is Fiddler?

Fiddler is a HTTP Debugging Proxy which logs all HTTP traffic between your computer and the Internet. Fiddler allows you to inspect all HTTP Traffic, set breakpoints, and “fiddle” with incoming or outgoing data. Fiddler is designed to be much simpler than using NetMon or Achilles, and includes a simple but powerful JScript.NET event-based scripting subsystem.

The website is available here.