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();
}