Archive for March, 2007

Displaying Summary Information in the GridView’s Footer

Posted in Net 2.0 on March 29, 2007 by bimal4u

protected void ProductsInCategory_RowDataBound
    (object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      … <i>Increment the running totals</i> …
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
      // Determine the average UnitPrice
      decimal avgUnitPrice = _totalUnitPrice / (decimal) _totalNonNullUnitPriceCount;
      // Display the summary data in the appropriate cells
      e.Row.Cells[1].Text = “Avg.: ” + avgUnitPrice.ToString(“c”);
      e.Row.Cells[2].Text = “Total: ” + _totalUnitsInStock.ToString();
      e.Row.Cells[3].Text = “Total: ” + _totalUnitsOnOrder.ToString();
    }
}

Sql paging through large amounts of data

Posted in Sql server 2005 on March 29, 2007 by bimal4u

DECLARE @PageSize int
DECLARE @PageNumber int
DECLARE @iBeginRecord int
DECLARE @iEndRecord int
SET @PageNumber = 1
SET @PageSize = 20
SET @iBeginRecord= ((@PageNumber 1) * @PageSize) ;
SET @iEndRecord = @iBeginRecord + @PageSize;
SELECT PriceRank, ProductName, UnitPrice
FROM(  SELECT ProductName, UnitPrice, ROW_NUMBER() OVER(ORDER BY UnitPrice DESC) AS PriceRank FROM Products )  AS ProductsWithRowNumber 
WHERE PriceRank > @iBeginRecord AND PriceRank <= @iEndRecord

what’s the maximum size varbinary in Sql Server ?

Posted in Sql Server 2000, Sql server 2005 on March 29, 2007 by bimal4u

In Microsoft SQL Server 2000 and earlier versions, the varbinary data type had a maximum limit of 8,000 bytes. To store up to 2 GB of binary data the image data type needs to be used instead. With the addition of MAX in SQL Server 2005, however, the image data type has been deprecated. It’s still supported for backwards compatibility, but Microsoft has announced that the image data type will be removed in a future version of SQL Server.

What is the difference between a.Equals(b) and a == b?

Posted in .net 1.0 on March 29, 2007 by bimal4u

Value Types:
For value types, “==” and Equals() works same way : Compare two objects by VALUE
Example:
int i = 5;
int k= 5;
i == k > True
i.Equals(k) > True

Reference Types:
For reference types, both works differently :
“==” compares reference – you can say identity of objects and returns true if and only if both references point to the SAME object while
Equals method compares object by VALUE and it will return true if the references refers object which are equivalent
Example:
StringBuilder sb1 = new StringBuilder(“Mahesh”);
StringBuilder sb2 = new StringBuilder(“Mahesh”);
sb1 == sb2 > False
sb1.Equals(sb2) > True

But now look at following issue:
String s1 = “Mahesh”;
String s2 = “Mahesh”;
In above case the results will be,
s1 == s2 > True
s1.Equals(s2) > True
String is actually reference type : its a sequence of “Char” and its also immutable but as you saw above, it will behave like Value Types in this case.
Exceptions are always there ;)


Now another interesting case:

int i = 0;
byte b = 0;

i == b > True
i.Equals(b) > False

So, it means Equals method compare not only value but it compares TYPE also in Value Types.

Recommendation:
For value types: use “==”
For reference types: use Equals method.

Some interesting facts:
Why to prefer Equals method in especially VB.Net?
Consider following snippet:
Public Sub doSomething()
        Dim s1 As String = “Mahesh”
        Dim s2 As String = “Mahesh”
        If s1 = s2 Then
            MessageBox.Show(“Same!”)
        End If
        If s1.Equals(s1) Then
            MessageBox.Show(“Not same!”)
        End If
End Sub

Now if you will compile this stuff, and check generated IL code [Use ILDASM.exe ships with .Net Framework], you will find,
in doSomething section, “=” will create,
IL_0010:  call  int32 [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType::StrCmp(string,string,bool)

And, Equals method will create,
 IL_0026:  callvirt   instance bool [mscorlib]System.String::Equals(string)

The second approach, using Equals uses mscorlib and its smaller and faster than the “=” because “=” uses VB.NET specific string comparison logic instead of core .Net implementation.
Single comparison operation may not give you performance hit but looping or at some critical part of you code, it can.
So Equals just give some efficient code.

More interesting thing that in C#, for the above code snippet, “==” and “Equals” both will result in same IL code. so no ambiguity in C#.
But if you used to code in both the language, then its obviously preferable to use Equals method.

How to build assembly for different modules ?

Posted in C# 2.0 on March 29, 2007 by bimal4u

You can generate modules as output as shown following
csc /target:module firstmodule.cs
this will create “firstmodule.netmodule”
Note: creating modules are not supported in Visual studio environment.
Or
csc first.cs /out:secondmodule.netmodule /target:module secondmodule.cs
This will compile first.cs and create output file first.exe, as well as build secondmodule.cs and create module output file secondmodule.netmodule:
So like this, you can generate different modules for different code files and then as per your choice you can add these modules to any dll or exe you want.
Note:
1. You can not include assembly manifest in .netmodule
2. You can generate multiple output in one compilation but one output should be .exe or .winexe or .dll and others can be .netmodule type