Friday, May 24, 2013

WCF-CommunicationException-This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

I got this error when trying to send a list of 5000 items (large list). Setting maxItemsInObjectGraph="2147483647" didn't help.

An error occurred while receiving the HTTP response to http://localhost:51923/Service1.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

SOLUTION
=========
Added <httpRuntime maxRequestLength ="262144" executionTimeout="103600"/> in the <system.web> section. It worked.

Hope this helps you.

Wednesday, May 22, 2013

Query to Find second highest/Nth highest salary in SQL Server and Oracle

SQL Sever
========
select top 1 a.* from (
select top 4 salary from table1 order by salary DESC ) a order by a.salary ASC;

Now in generic terms... Lets say for Nth salary.

select top 1 a.* from (
select top N salary from table1 order by salary DESC ) a order by a.salary ASC;

Just replace N with the number you want to find the salary. (N = 1 for first, N =2 for second, N= 3 for third.. so on..).

Oracle
======
select * from (
select tb1.*, rownum Rnum from (
select * from table1 order by salary DESC) tb1 where rownum <= 2) where Rnum >= 2;

Now to make it generic.. Lets say for Nth salary.

select * from (
select tb1.*, rownum Rnum from (
select * from table1 order by salary DESC) tb1 where rownum <= N) where Rnum >= N;

Just replace N with the number you want to find the salary. (N = 1 for first, N =2 for second, N= 3 for third.. so on..).

Tuesday, April 30, 2013

Reuse a StringBuilder object after clearing the TEXT - Simplest way - in older .net frameworks..

StringBuilder doesn't have a Clear() to clear the text for .net framework 2.0. There are different ways to clear the text, which are following:

1. Use Remove() method and give full length of text to remove.
2. Use Replace() method and replace the characters with "".


3. And the EASIEST SOLUTION is just set the builder.length = 0;

check it out.

Friday, April 19, 2013

Operation is not valid due to the current state of the object - LinQ to Entities - table without a primary key

You get this error when trying to insert into a table without a primary key. If you open the edmx file in XML view and take a look into the EntitySet it will be having a DefiningQuery tag (see below).

<EntitySet Name="Table1" EntityType="Model2.Store.Table1" store:Type="Tables" store:Schema="TABDATA" store:Name="Table1">
<DefiningQuery>
 SELECT "Table1"."Column1" as "Column1",
"Table1"."Column2" as "Column2",
"Table1"."Column3" as "Column3",
"Table1"."Column4" as "Column4",
"Table1"."Column5" as "Column5",
"Table1"."Column6" as "Column6",
"Table1"."Column7" as "Column7"
from "TABDATA"."Table1" "Table1"
</DefiningQuery>
</EntitySet>

The solution is to remove the DefiningQuery tag for that EntitySet as below:

<EntitySet Name="Table1" EntityType="Model2.Store.Table1" store:Type="Tables" store:Schema="TABDATA" store:Name="Table1">
</EntitySet>

You can insert new records now without any error.

Friday, April 5, 2013

How to Put a website into maintanence/redirect to maintanence page (for changes/updates) in shortest way possible - maintanence page display


Create a file named app_offline.htm. This file can have the content you want to show when your site goes into maintanence mode. You can add nice images and good content to show it to users, whenever your site goes down for maintanence.

You can bring up the site after updates/maintanence by just renaming this file (app_offline.htm) to some other name. Yes, the name is really checked by ASP.net, which internally brings the site down.

Try it out....

Interview Question for you to think - Puzzle - HOW DO YOU DO THAT???

You have a 5 litres and a 3 litres cup. Make use of this 5 litres and 3 litres cup to exactly get 4 litres of water. Yes, I really mean exactly 4 litres of water..... HOW DO YOU DO THAT????

You have lots of water available and throw away any amount of water, it doesn't matter. Remember, you have only these two cups available. No other cups/measuring items available..

Think and post you answer.....

Deprecated Method for adding a new object to the [object] EntitySet. Consider using the .Add method of the associated ObjectSet property instead.

This is a message shown when using old method to Add objects to entity.

The old way (deprecated) of inserting a new record to database using Entity object was as below:
using (Entities entities = new Entities(entityConnStr))
{
   //....
  //.....
  entities.AddToMyTable1(myTableObject);
  entities.SaveChanges(); 
}

New way to Insert/Add records is as below:
using (Entities entities = new Entities(entityConnStr))
{
//....
//.....
entities.AddObject("MyTable1",myTableObject);
entities.SaveChanges();
}