Wednesday, August 25, 2010

Improving ASP.NET website Performance


(1)

Decrease image size in various ways using reducing resolution, high compress rate or lower image quality.
(2) Use image path as simply replace it with static image link like this “/img/expand.gif”.
(3) Ajax.net framework – Use more JavaScript files via ScriptManager, Instead of adding entire js file only include the js files that is necessary to your application.
(4) CSS should be created, instead of applying individual control’s style.
(5) Keep entire CSS in separate files as per your page, so it will catch only that css file which is required.
(6) Duplicated or near to similar definition- Instead of repeating more than one function that behave near to similar functionality the only difference between them are Source ControlID. So, merge those all lengthy instances into one simple function and merge them together. That would be really simple way to reduce page size.
(7) Very deep server side controls hierarchy and very lengthy ID and Name strings, keep your parent control’s ID shorter So control ID does affect your page size a lot, especially the controls at the top level!.
· Make your hierarchy “flat”. Do you introduce container controls (such as Panel, server side DIV, etc) into your control hierarchy if not really necessary.
· Make your control’s ID shorter, so when HTML generated it’s page size become small.
(8) Cache header should be properly configured. There are lots of static resource files do not have proper caching header settings setup your expiration header to avoid this kind of unnecessary round trip to server! This is simply an IIS configuration change.
(9) Gzip/Deflate compression needs to be turned on.
(10) Make your Ajax call more efficient and keep ViewState size in control.
(11) Reduce ViewState size at any cost!!!
· Disable control’s ViewState one by one, make sure your app is still functioning though.
· Keep your control hierarchy “flat” and keep your ID short, as described above because controls ID will be part of the ViewState string in an encrypted way.
· Replace your server side controls with simple pure HTML tags, if possible. No server side controls if not needed. The fewer server side controls you use, the shorter the ViewState is in general.
(12) Use WebMethod to avoid page refresh.
(13) Use client site validation as much as possible instead of server site validation.


==============================================
Hiren Raval|Senior Software Engineer

WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in

Wednesday, August 18, 2010

Freeze Gridview’s header and column similar to excel



This article describes how to fix GridView’s columns and header in .Net 2008 or below version.

There are many good articles available to fix columns and header, here are some of them.

http://www.dotnetspider.com/resources/38294-How-Fix-Header-Column-GridView.aspx

http://mattberseth.com/blog/2007/09/freezing_gridview_column_heade_1.html

If we have less number of records on the screen we can use anyone of them but if we want to display thousands of records on the screens, none of these will be helpful.

After spending many hours I found solution that I have explained here, I hope that you would like it.

Step 1: Divide Grid in to two part
Part 1: Display Freeze columns
Part 2: Column which will be scrolled.
Step 2: Now GridView’s Header will not be attached with grid, divide them in different table.


Fix Col

Col 1Col 2Col 3Col 4…
0 0
1 0
2 0
3 0
4 0
5 0

0 10 20 30 4
1 11 1 31 4
2 1 2 22 32 4
3 13 23 33 4
4 14 24 34 4
5 15 25 35 4
Live Demo

Note: Here in blogger we are not able to give exact demo which will be viewed in your asp.net page, but I hope you would love it when you  see magic in your asp.net site.

Name
Vimal
Crystal
Nirman
Maulik
Sandip
Abhilash
Ashish
Adam
Carlos
Daniel
Francisco
Hallvard

PhoneEmailAddressStreetCityStateCountryPincode

Aspx Code:


<%@Page Language="C#" AutoEventWireup="true"CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.divScrollbar
{
background-color: #FFFFFF;
scrollbar-shadow-color: #FFFFFF;
scrollbar-highlight-color: #FFFFFF;
scrollbar-face-color: #FFFFFF;
scrollbar-3dlight-color: #FFFFFF;
scrollbar-darkshadow-color: #FFFFFF;
scrollbar-track-color: #FFFFFF;
scrollbar-arrow-color: #FFFFFF;
}
.Header
{
padding-left: 1px;
font-weight: bold;
background-color: #B5D1F0;
}
.font
{
font-family: Arial,Helvetica, sans-serif;
font-size: 8pt;
}
.GridHeight
{
height: 137px;
}
.gridData, .gridDatatrtd, .gridDatatr th
{
border: solid 1px #80B0E6;
}
</style>


<script type="text/javascript">
function OnScrollGV(div) {
var div2 = document.getElementById("left");
div2.scrollTop = div.scrollTop;
div2 = document.getElementById("divHeader");
div2.scrollLeft = div.scrollLeft;
}


functionOnScrollFreeze(div) {
var div2 = document.getElementById("right");
div2.scrollTop = div.scrollTop;
}
</script>
</head>
<body>
<formid="form1" runat="server">
<div class="font">
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<table cellpadding="0" cellspacing="0" class="Header" rules="all" style="height: 30px; width: 100%">
<tr style="text-align: center;">
<th>
Name
</th>
</tr>
</table>
<div id="left" style="overflow-x: auto; overflow-y: hidden; width: 100%; vertical-align: top" class="divScrollbar GridHeight" onscroll="OnScrollFreeze (this)">
<asp:GridView ID="gvFreeze" runat="server" AutoGenerateColumns="False" CssClass="gridData" EnableViewState="False" ShowHeader="False" ShowFooter="True" Width="130%" DataSourceID="LinqPerson">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
<FooterStyle Wrap="True"></FooterStyle>
</asp:GridView>
</div>
</td>
<td>
<div id="divHeader" style="overflow: auto; overflow-x: hidden; text-align: left; width: 100%; height: 30px;">
<table cellpadding="0" cellspacing="0" class="Header" rules="all" style="width: 130%; height: 30px">
<tr>
<td>
<table cellpadding="0" cellspacing="0" class="Header" rules="all" style="width: 100%; height: 30px">
<tr style="text-align: center;">
<th style="width: 10%;">
Phone
</th>
<th style="width: 10%;">
Email
</th>
<th style="width: 20%;">
Address1
</th>
<th style="width:20%;">
Address2
</th>
<th style="width: 10%;">
Street
</th>
<th style="width: 5%;">
City
</th>
<th style="width: 5%;">
State
</th>
<th style="width: 10%;">
Country
</th>
<th style="width: 10%;">
Pincode
</th>
</tr>
</table>
</td>
<td style="width: 20px">
&nbsp
</td>
</tr>
</table>
</div>
<div id="right" style="overflow-y: scroll; width: 100%;" onscroll="OnScrollGV (this)" class="GridHeight">
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" CssClass="gridData"
EnableViewState="false" Width="130%" ShowHeader="false" ShowFooter="true" FooterStyle-Wrap="true"
DataSourceID="LinqPerson">
<Columns>
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" FooterStyle-Width="10%" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" FooterStyle-Width="10%" />
<asp:BoundField DataField="Address1" HeaderText="Address1" SortExpression="Address1" FooterStyle-Width="20%" />
<asp:BoundField DataField="Address2" HeaderText="Address2" SortExpression="Address2" FooterStyle-Width="20%" />
<asp:BoundField DataField="Street" HeaderText="Street" SortExpression="Street" FooterStyle-Width="10%" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" FooterStyle-Width="5%" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" FooterStyle-Width="5%" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" FooterStyle-Width="10%" />
<asp:BoundField DataField="Pincode" HeaderText="Pincode" SortExpression="Pincode" FooterStyle-Width="10%" />
</Columns>
<FooterStyle Wrap="True"></FooterStyle>
</asp:GridView>
</div>
</td>
</tr>
</table>
</div>
<asp:LinqDataSource ID="LinqPerson" runat="server" ContextTypeName="DataClassesDataContext" TableName="Persons">
</asp:LinqDataSource>
</form>

</body>
</html>



==============================================
Shradhdha Zalavadiya |Senior Software Engineer

WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in

Sunday, August 8, 2010

Cross Apply In SQL Server


I will be introducing you with magic feature from Microsoft SQL-Server 2005, Called CROSS APPLY. If you want to get all Geo information where city are separated by “,” you can use udf or sub query for CROSS APPLY.

Create a Geo table.

Geo
CityState
Rajkot, Ahmedabad, Vadodara, Surat, JamnagarGujarat
Mumbai, Pune, Nagpur, NasikMaharashtra
Bangalore, Mysor, MangaloreKarnataka


Expected Result

To retrieve all Geo information with separated city.
CityStateOrgCity
AhmedabadGujaratRajkot, Ahmedabad, Vadodara, Surat, Jamnagar
JamnagarGujaratRajkot, Ahmedabad, Vadodara, Surat, Jamnagar
RajkotGujaratRajkot, Ahmedabad, Vadodara, Surat, Jamnagar
SuratGujaratRajkot, Ahmedabad, Vadodara, Surat, Jamnagar
VadodaraGujaratRajkot, Ahmedabad, Vadodara, Surat, Jamnagar
BangaloreKarnatakaBangalore, Mysor, Mangalore
MangaloreKarnatakaBangalore, Mysor, Mangalore
MysorKarnatakaBangalore, Mysor, Mangalore
MumbaiMaharashtraMumbai, Pune, Nagpur, Nasik
NagpurMaharashtraMumbai, Pune, Nagpur, Nasik
Nasik< MaharashtraMumbai, Pune, Nagpur, Nasik
PuneMaharashtraMumbai, Pune, Nagpur, Nasik


(1) Create a function that accept string value and return table, after splitting with special character.

Create Function[dbo].[fnSplit]
(
@sInputList VARCHAR(200)
)
RETURNS @List TABLE (item VARCHAR (8000))
BEGIN

DECLARE @sDelimiter varchar(2)
SET @sDelimiter =',' --Set Delimiter value

DECLARE @sItem VARCHAR(8000)

WHILE CHARINDEX(@sDelimiter, @sInputList, 0) <> 0
BEGIN
SELECT @sItem = RTRIM(LTRIM(SUBSTRING(@sInputList, 1, CHARINDEX(@sDelimiter, @sInputList, 0)-1))), @sInputList=RTRIM(LTRIM (SUBSTRING(@sInputList, CHARINDEX(@sDelimiter, @sInputList, 0)+LEN(@sDelimiter),LEN(@sInputList))))

IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
END

IF LEN(@sInputList) > 0
INSERT INTO @List SELECT @sInputList -- Put the last item in

RETURN

END

How to execute above function

Select * from dbo.fnSplit('Rajkot, Ahmedabad, Vadodara, Surat, Jamnagar')
orderby item

Result:
Ahmedabad
Jamnagar
Rajkot
Surat
Vadodara

(2) To retrieve above Expected Result use following query

SELECT Item as City, [State], City as OrgCity
FROM Geo
CROSS APPLY [dbo].[fnSplit](Geo.city)
Orderby [State], Item

==============================================
Nirman Doshi|Senior Software Engineer

WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in

Friday, August 6, 2010

Html / Aspx Bookmark Page


Html <a> tag is very useful for creating bookmark. Html bookmark tag link navigates the web page to the targeted location of same of different page.

Default Page will be load on top, we can load page on certain top position using <a> attribute.

Following example is given in asp.net.

<body>
<form id="form1" runat="server">
<div style="height: 300px; background-color: red; font-size:300%">
First Div
</div>
<a id="BookMark2">
<div style="height: 300px; background-color: Green; font-size: 300%">
Second Div
</div>
</a>
<a id="BookMark3">
<div style="height: 300px; background-color: Blue; font-size: 300%">
Third Div
</div>
</a>
</form>
</body>

When you load page it will display as below


Now if page load using #BookMark2 using http://localhost:59495/Desktop1/Default.aspx#BookMark2

It will display as below.









==============================================
Nirman Doshi|Senior Software Engineer

WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in

Thursday, August 5, 2010

What is extension Method?



Microsoft provides very powerful and useful feature in .Net 3.0 and above, one of them is Extension Method.

After declaring variable when we use that variable name and dot(“.”) we can find properties, these properties will be different for each data type.

If you declare int Count;

And write Count. You can see CompareTo, Equals, GetType...

These properties will be different for string or other datatype. We can create these properties and write customize code as per our requirement that called extension method.














Now, let’s create an extension method for int datatype.

Create and implement extension method.


To extend method, your class and property should be static. So create a new static class.

public static class ExtensionMethods
{
public static bool IsZeroOrNegative(this int value)
{
return (value <= 0) ? true: false ;
}
}


Here we check if value is less then or equals to 0 we return true other-wise false. This method will be extended with integer data type (we pass this int so it extend with int data type), now we can find extended property using Count.IsZeroOrNegative(); Here you can see, extension method is











==============================================
Hiren Raval |Senior Software Engineer

WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in

Tuesday, August 3, 2010

Relational Database Normalization Process

Illogically or inconsistently stored data can cause a number of problems. In a relational database, a logical and efficient design is just as critical. A poorly designed database may provide erroneous information, may be difficult to use, or may even ill to work properly.

Most of these problems are the result of two bad design features called: redundant data and anomalies. Redundant data is unnecessary reoccurring data (repeating groups of data). Anomalies are any occurrence that weakens the integrity of your data due to irregular or inconsistent storage (delete, insert
and update irregularity, that generates the inconsistent data).

Basically, normalisation is the process of efficiently organising data in a database. There are two main objectives of the normalization process: eliminate redundant data (storing the same data in more than one table) and ensure data dependencies make sense (only storing related data in a table).
Both of these are valuable goals as they reduce the amount of space a database consumes and ensure that data is logically stored.

The process of designing a relational database includes making sure that a table contains only data directly related to the primary key, that each data field contains only one item of data, and that redundant
(duplicated and unnecessary) data is eliminated. The task of a database designer is to structure the data in a way that eliminates unnecessary duplication(s) and provides a rapid search path to all necessary information. This process of specifying and defining tables, keys, columns, and relationships in order
to create an efficient database is called normalization.

Normalisation is part of successful database design.


Without normalisation, database systems can be inaccurate, slow, and inefficient and they might not produce the data you expect.


We use the normalization process to design efficient and functional databases. By normalizing, we store data where it logically and uniquely belongs. The normalization process involves a few steps and each step is called a form. Forms range from the first normal form (1NF) to fifth normal form (5NF). There is also one higher level, called domain key normal form (DK/NF) and BCNF.

When normalising a database you should achieve four goals:

  1. Arranging data into logical groups such that each group describes a small part of the whole
  2. Minimizing the amount of duplicated data stored in a database
  3. Building a database in which you can access and manipulate the data quickly and efficiently without compromising the integrity of the data storage
  4. Organising the data such that, when you modify it, you make the changes in only one place
Normalization is a complex process with many specific rules and different intensity levels. In its full definition, normalization is the process of discarding repeating groups, minimizing redundancy, eliminating composite keys for partial dependency and separating non-key attributes.

A properly normalised design allows you to:

  • Use storage space efficiently
  • Eliminate redundant data
  • Reduce or eliminate inconsistent data
  • Ease the database maintenance burden
A bad database design usually include:

  • Repetition of information
  • Inability to represent certain information
  • Loss of information
  • Difficulty to maintain information
When you normalise a database, you start from the general and work towards the specific, applying certain tests (checks) along the way. Some users call this process  decomposition. It means decomposing (dividing/breaking down) a ‘big’ un-normalise table (file) into several smaller tables by:

  • Eliminating insertion, update and delete anomalies
  • Establishing functional dependencies
  • Removing transitive dependencies
  • Reducing non-key data redundancy


Un-normalised data

Un-normalised data have repeating groups, inconsistent data, delete and insert anomalies.
The following table is un-normalized:

Un-normalised data
Project Color Size
P1 Red Large
P1 Green Large
P1 Red Medium
P1 Green Medium
P1 Red Small
P1 Green Small
P2 Black Large
P2 Black Small


1NF meets the following criteria:

  1. ELIMINATE REPEATING GROUPS (make a separate table for each set of related attributes and give each table a primary key).
  2. A table is in 1NF if there are no duplicated rows in the table.
  3. Each table contains all atomic data items, no repeating groups, and a designated primary key (no duplicated rows)
The following tables are in 1NF of above given un-normalized data :

Project Table
ProjectID Project Color
1 P1 Red
2 P1 Green
3 P2 Black


Detail Table
DetailId ProjectId Size
1 1 Large
2 2 Large
3 1 Medium
4 2 Medium
5 1 Small
6 2 Small
7 3 Large
8 3 Small



Second Normal Form (2NF)


ELIMINATE REDUNDANT DATA (if an attribute depends on only part of multi-valued key, remove it to a separate table) . A table is in 2NF if it met all database requirements for 1NF, and if each non-key attribute is fully functionally dependent on the whole primary key;
2NF meets the following criteria:
  1. Each table contains all atomic data items, no repeating groups, and a designated primary key (no duplicated rows)
  2. Each table has all non-primary key attributes fully functionally dependant on the whole primary key
Project Table
ProjectID Project Color
1 P1 Red
2 P1 Green
3 P2 Black


Size Table
SizeID Size
1 Large
2 Medium
3 Small


Detail Table
DetailId ProjectId SizeID
1 1 1
2 2 1
3 1 2
4 2 2
5 1 3
6 2 3
7 3 1
8 3 3



Third Normal Form (3NF)


ELIMINATE COLUMNS NOT DEPENDANT ON KEY (if attributes do not contribute to a description of the key remove them to a separate table) . A table is in 3NF if it met all database requirements for both 1NF and 2NF, and if all transitive dependencies are eliminated (each column must depend directly on the primary key; all attributes that are not dependant upon the primary key must be eliminated (e.g. attributes that can be derived from data contained in other fields and tables must be removed)).
3NF meets the following criteria:
  1. Each table contains all-atomic data items, no repeating groups, and a designated primary key
  2. Each table has all non-primary key attributes fully functionally dependant on the whole primary key
  3. All transitive dependencies are removed from each table
Project Table
ProjectID Project
1 P1
2 P2


Color Table
ColorID Color
1 Red
2 Green
3 Black


Size Table
SizeID Size
1 Large
2 Medium
3 Small


Detail Table
DetailId ProjectId ColorId SizeId
1 1 1 1
2 1 2 1
3 1 1 2
4 1 2 2
5 1 1 3
6 1 2 3
7 2 3 1
8 2 3 3



Fourth Normal Form (4NF)


ISOLATE INDEPENDENT MULTIPLE RELATIONSHIPS (no table may contain two or more 1:M or M:M relationships that are not directly related) . This applies to only 1:M and M:M relationships. To be in Fourth Normal Form (4NF).
Project Table
ProjectID Project
1 P1
2 P2



Color Table
ColorID Color
1 Red
2 Green
3 Black


Size Table
SizeID Size
1 Large
2 Medium
3 Small


DetailProjectColor
DPCID ProjectId ColorId
1 1 1
2 1 2
3 2 3


DetailProjectSize
DPSID DPCID SizeId
1 1 1
2 2 1
3 1 2
4 2 2
5 1 3
6 2 3
7 3 1
8 3 3



Fifth Normal Form (5NF)


The normal forms discussed so far required that the given relation R if not in the given normal form be decomposed in two relations to meet the requirements of the normal form. In some rare cases, a relation can have problems like redundant information and update anomalies because of it but cannot be decomposed in two relations to remove the problems. In such cases it may be possible to decompose the relation in three or more relations using the 5NF.
The fifth normal form deals with join-dependencies which is a generalisation of the MVD. The aim of fifth normal form is to have relations that cannot be decomposed further. A relation in 5NF cannot be constructed from several smaller relations.
A relation R is in 5NF (or project-join normal form, PJNF) if for all join dependencies at least one of the following holds.
(a) (R1, R2, ..., Rn) is a trivial join-dependency (that is, one of Ri is R) (b) Every Ri is a candidate key for R.
An example of 5NF can be provided by the example below.


Project Table
ProjectID Project
1 P1
2 P2


Color Table
ColorID Color
1 Red
2 Green
3 Black


Size Table
SizeID Size
1 Large
2 Medium
3 Small


DetailProjectColor
DPCID ProjectId ColorId
1 1 1
2 1 2
3 2 3


DetailProjectSize
DPSID ProjectId SizeId
1 1 1
2 1 2
3 1 3
4 2 1
5 2 3


Detail Table
DetailId DPCID DPSID
1 1 1
2 2 1
3 1 2
4 2 2
5 1 3
6 2 3
7 3 4
8 3 5



==============================================
Maulik Bhatt |Senior QA Engineer

WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in