Labels

Friday, August 31, 2007

Working with IDENTITY COLUMN

Below is required to work with IDENTITY column in SQL-Server

set identity_insert Table_1 OFF

INSERT INTO Table_1 VALUES('AA')

SELECT * FROM Table_1

set identity_insert Table_1 ON

INSERT INTO Table_1 (ID,NAME) VALUES(1,'AA')

SELECT * FROM Table_1

DELETE FROM Table_1

DBCC CHECKIDENT (Table_1, RESEED, 0) -- To reset the value

Thanks & Regards,

Arun Manglick || Tech Lead

Tuesday, August 28, 2007

Server Side vs Client Side Comments

One common question people ask is what the difference is between using client-side HTML comments and server-side comments.

The key difference is that with client-side comments

· It is the browser which is ignoring the content within them.

· Code/controls within client-side comments will still be executed on the server and sent down to the browser.

· As such, if there is a server error caused within them it will block running the page.

With server-side comments,

  • The ASP.NET compiler ignores everything within these blocks at parse/compile time, and removes the content completely when assembling the page (like its contents weren’t there at all).
  • Consequently, any errors caused by mal-formed controls or issues with inline code or data-binding expressions within them will be ignored.
  • The page is also just as fast with controls/code within server-side comments as if there were no controls/code on the page at all (there is no runtime performance overhead to them).

Thanks & Regards,

Arun Manglick || Tech Lead

Thursday, August 23, 2007

By FxCop

Avoid ‘out’ Parameters: By FxCop

· Using out parameters might indicate a design flaw.

· Although there are legitimate times to use out parameters, their use frequently indicates a design that does not conform to the design guidelines for managed code.

How declaring methods as ‘Static’ can increase performance? By FxCop

  • Methods which do not access instance data or call instance methods can be marked as static (Shared in VB).
  • After doing so, the compiler will emit non-virtual call sites to these members which will prevent a check at runtime for each call that insures the current object pointer is non-null.
  • This can result in a measurable performance gain for performance-sensitive code.
  • In some cases, the failure to access the current object instance represents a correctness issue.

Why Default Constructor is necessary for classes/types with only static members?

  • Instances of types that define only static members do not need to be created.
  • Many compilers will automatically add a public default constructor if no constructor is specified.
  • To prevent this, adding an empty private constructor may be required."

Static holder types should be ‘sealed’: By FxCop

  • Static holder types do not provide functionality that derived instances can extend. Inheriting from such a type indicates a flawed design.

Misc:

  • Properties that return collections should be read-only so that users cannot entirely replace the backing store.
  • Users can still modify the contents of the collection by calling relevant methods on the collection.

Exception Handling using IDisposable Interface and Dispose Method().

  • Instead of implementing only Dispose() we should implement hybrid approach.
  • Instead we should call Dispose(true), then calls GC.SuppressFinalize().

Thanks & Regards,

Arun Manglick || Tech Lead

Static can increase performance

How declaring methods as ‘Static’ can increase performance? By FxCop

  • Methods which do not access instance data or call instance methods can be marked as static (Shared in VB).
  • After doing so, the compiler will emit non-virtual call sites to these members which will prevent a check at runtime for each call that insures the current object pointer is non-null.
  • This can result in a measurable performance gain for performance-sensitive code.
  • In some cases, the failure to access the current object instance represents a correctness issue.

Thanks & Regards,

Arun Manglick || Tech Lead

Monday, August 20, 2007

Using Enum as DataSource for DataList

namespace Pfx.SmartTools.Word.Common
{
public enum NewWP
{
True, False, NotExist
}
}

DropDownList1.DataSource = Enum.GetNames(GetType(NewWP))
DropDownList1.DataBind()

Thanks & Regards,

Arun Manglick || Tech Lead

Friday, August 17, 2007

Response.ContentType

Response.ContentType

The ContentType property specifies the HTTP content type for the response. If no ContentType is specified, the default is text/HTML.

Few more are:

<% Response.ContentType = "text/HTML" %>

<% Response.ContentType = "image/GIF" %>

<% Response.ContentType = "image/JPEG" %>

<% Response.ContentType = "text/plain" %>

<% Response.ContentType = "image/JPEG" %>

<% Response.ContentType = "audio/MPEG" %>

<% Response.ContentType = "application/x-cdf" %> [Channel Definition Format]

ContentType

A string describing the content type. This string is usually formatted as type/subtype.

Type : is the general content category, and

Subtype: is the specific content type.

For a full list of supported content types, see your Web browser documentation or the current HTTP 1.1 specification located at http://www.w3.org/Protocols/Specs.html and search for "content-type".

Thanks & Regards,

Arun Manglick || Tech Lead

Tinker Collections Midway

Hi,

Can you tamper the collection in between of a for loop?

The solution is two fold:

· For loop: Allows

· ForEach: Does not allow.

ArrayList:

------------------------------------------------------------------------

ArrayList array = new ArrayList();

array.Add("AA");

array.Add("AB");

array.Add("AC");

array.Add("AD");

array.Add("AE");

MessageBox.Show(array.Count.ToString()); // 5

for (int i = 0; i < array.Count; i++)

{

if (array[i].Equals("AA"))

{

array.Remove(array[i]); // Note: No need to update the counter ‘i’

}

}

// For each will not allow to tamper the collection while looping

//foreach (string str in array)

//{

// if (str.Equals("AB"))

// {

// array.Remove(str);

// }

//}

MessageBox.Show(array.Count.ToString()); // 4

------------------------------------------------------------------------

HashTable:

------------------------------------------------------------------------

string strKey, strValue;

Hashtable hash = new Hashtable();

hash.Add("AA", "AAA");

hash.Add("AB", "AAB");

hash.Add("AC", "AAC");

hash.Add("AD", "AAD");

hash.Add("AE", "AAE");

MessageBox.Show(hash.Count.ToString());

string[] arrKeys = new string[hash.Count];

hash.Keys.CopyTo(arrKeys, 0);

for (int i = 0; i < arrKeys.Length; i++)

{

strKey = arrKeys[i];

if (strKey.Equals("AB"))

{

hash.Remove(strKey);

}

}

// For each will not allow to tamper the collection while looping

//foreach (string str in hash.Keys)

//{

// if (str.Equals("AB"))

// {

// hash.Remove(str);

// }

//}

MessageBox.Show(hash.Count.ToString());

------------------------------------------------------------------------

Mind it from next time……. J

Thanks & Regards,

Arun Manglick || Tech Lead