Monday, May 11, 2009

Web page loading twice problem

We had a problem when working on a web project (C# ASP .Net). The problem was, the page was loading twice for every request made. This was happening only for GET requests. And it was working fine for POST requests. After doing a long research on it I found the issue why it was loading 2 times.

We had a master page with 2 images defined with empty (#) src attributes.

The exact code we had was

<img src='#' width='1000' height='392' alt='you need to install flash or turn on javascript' />

<img src='' width='1000' height='300' alt='' class='hide' />

The attribute src must be set other than empty or #. After fixing this we noticed that the page is loading only once.

Due to that bug all the pages which are using this master page were having the problem. After the fix every page worked fine with no issues.

For more details about this issue please check.

http://www.velocityreviews.com/forums/t21131-aspnet-page-load-twice-problem.html

Wednesday, May 6, 2009

table variables in sql server 2005

Table variable is almost similar to the temporary table in sql server 2005. Table variables are replacement to temporary tables. The following syntax shows how to define a table variable.

declare @Customer table
(
CustomerID int identity(1,1),
CustomerName varchar(100),
CompanyName varchar(100),
Address varchar(250)
)

You can notice the syntax to define a table variable is similar to the syntax to normal table definition. declare keyword is used in place of create keyword. And table name is prefixed with '@' as all tsql variables do.
  1. Table variables are stored in memory rather than in database.
  2. Querying table variables is very fast as there are no disk reads needed.
  3. The scope the table variables is same as other tsql variables. i.e, within statements batch or sproc
  4. Table variables cannot be dropped as they are automatically disappeared when they reach out scope
  5. As explained above all the data in table variables is stored in server's memory. So if there is huge data then it is not recommended to use table variables to avoid memory overhead.
  6. If the number of records is more than 100 then it is recommended to use temporary tables. To learn more about temporary tables please readi this blog post http://cherupally.blogspot.com/2009/05/how-to-create-temporary-table-in-sql.html

Shout it

How to create temporary table in sql server 2005?

The syntax to create a temporary table is exactly same as the syntax to create a normal table. But the difference is the table name is prefixed with '#' (pound). By looking at the prefix '#' the sql server understands that it is a temporary table. The following code demonstrates how to create a temporary variable.

CREATE TABLE #Customer
(
CustomerID int identity(1,1),
CustomerName varchar(100),
CompanyName varchar(100),
Address varchar(250)
)

Important points about temporary tables:
  1. It is almost similar to normal table with very few exceptions.
  2. The scope of the temp table is the current connection session. It can be accessed from any where in same connection session.
  3. This table is automatically dropped when the connection session is closed.
  4. Different connection sessions creating temporary table with same name will create different copies of the temporary table with the same name. Actually internally the table name is appended with a unique number to support many connection sessions creating tables with same name. You can notice how the table name is uniquely maintained by sql server by running this query. select * from information_schema.tables where table_name like '%customer%'
  5. foreign key relations cannot be applied to temp tables.
  6. Optionally you may drop the table at the end of it's use. It is a good practice to drop any temporary table after use. drop table #Customer

When you create a temporary table it will be created in tempdb. At the time of table creation the tempdb is locked and hence there is some overhead involved using this. If there are less than 100 records then using temporary tables is not recommended. In such cases you can use table variables which are stored in memory rather than in database(on disk). To learn more about table variables please read this
http://cherupally.blogspot.com/2009/05/table-variables-in-sql-server-2005.html
Shout it

Monday, May 4, 2009

Implementing Custom Paging, Sorting and Dynamic query in sql

This post explains and demonstrates how to implement following things
  1. Custom Paging: Returning a set of records for the given page number and page size
  2. Dynamic query: Querying based on the where string passed thru parameter
  3. Dynamic sorting: Sorting based on the sort expression passed thru parameter
  4. SP_ExecuteSql: Executing sql statements included within parameters
  5. Common Table Expression: Similar to table variable
  6. function row_number(): returns the current row number in a result set
The following code is to create the stored procedure GetCustomersByPage. This stored procedure accepts the parameters page size, page number to return the page, where string (It must start with 'and' keyword if a condition is added. Ex: " and CompanyName like '%soft%' " or empty is accepted but applies no filter), sort expression to get the sorted records and total count which is useful when implementing the paging logic in user interface.

The sample sproc is written for NorthWind database. If you have NorthWind database installed then you can run this sproc and see how it works.

create procedure GetCustomersByPage
@PageSize int,
@PageNumber int,
@WhereString varchar(max),
@SortExpression varchar(200),
@TotalCount int output
as
begin

declare @FirstRecord int
declare @LastRecord int


set @FirstRecord = (((@PageNumber - 1) * @PageSize) + 1)
set @LastRecord = (@PageNumber * @PageSize)

declare @sql nvarchar(max)
DECLARE @ParmDefinition NVARCHAR(500)
SET @ParmDefinition=N'@TotalCount int OUTPUT'

set @sql = 'select @TotalCount = count(*) from dbo.Customers where 1=1 '+@WhereString+';
with PagedCustomers as (select row_number() over(order by '+@SortExpression+') as RowNumber, * from dbo.Customers where 1=1 '+@WhereString+')
SELECT * from PagedCustomers where RowNumber between '+cast(@FirstRecord as varchar)+'and '+cast(@LastRecord as varchar)

exec sp_executesql @sql, @ParmDefinition, @TotalCount=@TotalCount output

return @TotalCount
end
Shout it

How to post html code in blogspot / blogger

I had problem when posting an article on blogspot. I was trying to show some sample html/xml code in that article. But I faced problems when I wrote the core html code. The problem was that the html code was rendering as html controls and breaking the post content. I tried many times and got frustration. Finally I searched in google for this problem and I got the below useful link.

http://www.simplebits.com/cgi-bin/simplecode.pl

If you follow the above link you will notice 2 textareas in which the first one is for input the html code and second one is for output of the changed html code which fits into the blogspot post and renders as expected.

Whoever wants to post the html code in blogger, please go to the link and paste your html code in first text box control and click the process button. Then you will notice the second textbox updated with the processed html code. Then copy the complete code from second text box and paste it in your blogspot post. It works not only in blogspot but also in all blogs which don't allow direct html code in posts.

Friday, May 1, 2009

Code to Serialize Object in C# .Net

Please use the below method if you want to serialize any object. This method returns you a string which is XML consists of all public properties and public data members of the object passed. It perfectly works for any type of reference type object.


String SerializeObject(Object obj)
{
try
{
String XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();

XmlSerializer xs = new XmlSerializer(obj.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

xs.Serialize(xmlTextWriter, obj);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}

catch (Exception e)
{
System.Console.WriteLine(e);
return null;
}
}